cuelang.org/go@v0.13.0/encoding/jsonschema/constraints_meta.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  // Meta constraints
    22  
    23  func constraintID(key string, n cue.Value, s *state) {
    24  	// URL: https://domain.com/schemas/foo.json
    25  	// anchors: #identifier
    26  	//
    27  	// TODO: mark anchors
    28  
    29  	// Resolution is relative to parent $id
    30  	// https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.2
    31  	u := s.resolveURI(n)
    32  	if u == nil {
    33  		return
    34  	}
    35  
    36  	if u.Fragment != "" {
    37  		// TODO do not use StrictFeatures for this. The specification is clear:
    38  		// before 2019-09, IDs could contain plain-name fragments;
    39  		// (see https://json-schema.org/draft-07/draft-handrews-json-schema-01#rfc.section.5)
    40  		// afterwards, $anchor was reserved for that purpose.
    41  		if s.cfg.StrictFeatures {
    42  			s.errf(n, "$id URI may not contain a fragment")
    43  		}
    44  		return
    45  	}
    46  	s.id = u
    47  }
    48  
    49  // constraintSchema implements $schema, which
    50  // identifies this as a JSON schema and specifies its version.
    51  func constraintSchema(key string, n cue.Value, s *state) {
    52  	if !s.isRoot && !s.schemaVersion.is(vfrom(VersionDraft2019_09)) {
    53  		// Before 2019-09, the $schema keyword was not allowed
    54  		// to appear anywhere but the root.
    55  		s.errf(n, "$schema can only appear at the root in JSON Schema version %v", s.schemaVersion)
    56  		return
    57  	}
    58  	str, ok := s.strValue(n)
    59  	if !ok {
    60  		// If there's no $schema value, use the default.
    61  		return
    62  	}
    63  	sv, err := ParseVersion(str)
    64  	if err != nil {
    65  		s.errf(n, "invalid $schema URL %q: %v", str, err)
    66  		return
    67  	}
    68  	s.schemaVersionPresent = true
    69  	s.schemaVersion = sv
    70  }
    71  
    72  func constraintTODO(key string, n cue.Value, s *state) {
    73  	if s.cfg.StrictFeatures {
    74  		s.errf(n, `keyword %q not yet implemented`, key)
    75  	}
    76  }
    77  
    78  // constraintIgnore represents a constraint that we're deliberately
    79  // ignoring, by contrast with [constraintTODO] that represents
    80  // a constraint that we're definitely intending to implement
    81  // at some point.
    82  func constraintIgnore(key string, b cue.Value, s *state) {
    83  }