cuelang.org/go@v0.10.1/encoding/jsonschema/constraints_string.go (about)

     1  // Copyright 2019 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package jsonschema
    16  
    17  import (
    18  	"regexp"
    19  
    20  	"cuelang.org/go/cue"
    21  	"cuelang.org/go/cue/ast"
    22  	"cuelang.org/go/cue/token"
    23  )
    24  
    25  // String constraints
    26  
    27  func constraintContentEncoding(key string, n cue.Value, s *state) {
    28  	// TODO: only mark as used if it generates something.
    29  	// 7bit, 8bit, binary, quoted-printable and base64.
    30  	// RFC 2054, part 6.1.
    31  	// https://tools.ietf.org/html/rfc2045
    32  	// TODO: at least handle bytes.
    33  }
    34  
    35  func constraintContentMediaType(key string, n cue.Value, s *state) {
    36  	// TODO: only mark as used if it generates something.
    37  }
    38  
    39  func constraintMaxLength(key string, n cue.Value, s *state) {
    40  	max := s.number(n)
    41  	strings := s.addImport(n, "strings")
    42  	s.add(n, stringType, ast.NewCall(ast.NewSel(strings, "MaxRunes"), max))
    43  }
    44  
    45  func constraintMinLength(key string, n cue.Value, s *state) {
    46  	min := s.number(n)
    47  	strings := s.addImport(n, "strings")
    48  	s.add(n, stringType, ast.NewCall(ast.NewSel(strings, "MinRunes"), min))
    49  }
    50  
    51  func constraintPattern(key string, n cue.Value, s *state) {
    52  	str, _ := n.String()
    53  	if _, err := regexp.Compile(str); err != nil {
    54  		if s.cfg.Strict {
    55  			s.errf(n, "unsupported regexp: %v", err)
    56  		}
    57  		return
    58  	}
    59  	s.add(n, stringType, &ast.UnaryExpr{Op: token.MAT, X: s.string(n)})
    60  }