cuelang.org/go@v0.10.1/encoding/jsonschema/constraints.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  	"cuelang.org/go/cue"
    19  )
    20  
    21  // TODO: skip invalid regexps containing ?! and foes.
    22  // alternatively, fall back to  https://github.com/dlclark/regexp2
    23  
    24  type constraint struct {
    25  	key string
    26  
    27  	// phase indicates on which pass c constraint should be added. This ensures
    28  	// that constraints are applied in the correct order. For instance, the
    29  	// "required" constraint validates that a listed field is contained in
    30  	// "properties". For this to work, "properties" must be processed before
    31  	// "required" and thus must have a lower phase number than the latter.
    32  	phase int
    33  
    34  	// Indicates the draft number in which this constraint is defined.
    35  	draft int
    36  	fn    constraintFunc
    37  }
    38  
    39  // A constraintFunc converts a given JSON Schema constraint (specified in n)
    40  // to a CUE constraint recorded in state.
    41  type constraintFunc func(key string, n cue.Value, s *state)
    42  
    43  func p0(name string, f constraintFunc) *constraint {
    44  	return &constraint{key: name, fn: f}
    45  }
    46  
    47  func p1d(name string, draft int, f constraintFunc) *constraint {
    48  	return &constraint{key: name, phase: 1, draft: draft, fn: f}
    49  }
    50  
    51  func p1(name string, f constraintFunc) *constraint {
    52  	return &constraint{key: name, phase: 1, fn: f}
    53  }
    54  
    55  func p2(name string, f constraintFunc) *constraint {
    56  	return &constraint{key: name, phase: 2, fn: f}
    57  }
    58  
    59  func p3(name string, f constraintFunc) *constraint {
    60  	return &constraint{key: name, phase: 3, fn: f}
    61  }
    62  
    63  // TODO:
    64  // writeOnly, readOnly
    65  
    66  var constraintMap = map[string]*constraint{}
    67  
    68  func init() {
    69  	for _, c := range constraints {
    70  		constraintMap[c.key] = c
    71  	}
    72  }
    73  
    74  // Note: the following table is ordered lexically by keyword name.
    75  // The various implementations are grouped by kind in the constaint-*.go files.
    76  
    77  var constraints = []*constraint{
    78  	p1d("$comment", 7, constraintComment),
    79  	p1("$defs", constraintAddDefinitions),
    80  	p0("$id", constraintID),
    81  	p0("$schema", constraintSchema),
    82  	p1("$ref", constraintRef),
    83  	p1("additionalItems", constraintAdditionalItems),
    84  	p3("additionalProperties", constraintAdditionalProperties),
    85  	p2("allOf", constraintAllOf),
    86  	p2("anyOf", constraintAnyOf),
    87  	p1d("const", 6, constraintConst),
    88  	p1("contains", constraintContains),
    89  	p1d("contentEncoding", 7, constraintContentEncoding),
    90  	p1d("contentMediaType", 7, constraintContentMediaType),
    91  	p1("default", constraintDefault),
    92  	p1("definitions", constraintAddDefinitions),
    93  	p1("dependencies", constraintDependencies),
    94  	p1("deprecated", constraintDeprecated),
    95  	p1("description", constraintDescription),
    96  	p1("enum", constraintEnum),
    97  	p1("examples", constraintExamples),
    98  	p1("exclusiveMaximum", constraintExclusiveMaximum),
    99  	p1("exclusiveMinimum", constraintExclusiveMinimum),
   100  	p0("id", constraintID),
   101  	p1("items", constraintItems),
   102  	p1("minItems", constraintMinItems),
   103  	p1("maxItems", constraintMaxItems),
   104  	p1("maxLength", constraintMaxLength),
   105  	p1("maxProperties", constraintMaxProperties),
   106  	p2("maximum", constraintMaximum),
   107  	p1("minLength", constraintMinLength),
   108  	p2("minimum", constraintMinimum),
   109  	p1("multipleOf", constraintMultipleOf),
   110  	p2("oneOf", constraintOneOf),
   111  	p1("nullable", constraintNullable),
   112  	p1("pattern", constraintPattern),
   113  	p2("patternProperties", constraintPatternProperties),
   114  	p1("properties", constraintProperties),
   115  	p1d("propertyNames", 6, constraintPropertyNames),
   116  	p2("required", constraintRequired),
   117  	p1("title", constraintTitle),
   118  	p1("type", constraintType),
   119  	p1("uniqueItems", constraintUniqueItems),
   120  }