cuelang.org/go@v0.10.1/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 identifiers.
    28  
    29  	// Draft-06 renamed the id field to $id.
    30  	if key == "id" {
    31  		// old-style id field.
    32  		if s.schemaVersion >= versionDraft06 {
    33  			if s.cfg.Strict {
    34  				s.warnf(n.Pos(), "use of old-style id field not allowed in schema version %v", s.schemaVersion)
    35  			}
    36  			return
    37  		}
    38  	} else {
    39  		// new-style $id field
    40  		if s.schemaVersion < versionDraft07 {
    41  			if s.cfg.Strict {
    42  				s.warnf(n.Pos(), "use of $id not allowed in older schema version %v", s.schemaVersion)
    43  			}
    44  			return
    45  		}
    46  	}
    47  
    48  	// Resolution must be relative to parent $id
    49  	// https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.2
    50  	u := s.resolveURI(n)
    51  	if u == nil {
    52  		return
    53  	}
    54  
    55  	if u.Fragment != "" {
    56  		if s.cfg.Strict {
    57  			s.errf(n, "$id URI may not contain a fragment")
    58  		}
    59  		return
    60  	}
    61  	s.id = u
    62  	s.idPos = n.Pos()
    63  }
    64  
    65  func constraintSchema(key string, n cue.Value, s *state) {
    66  	// Identifies this as a JSON schema and specifies its version.
    67  	// TODO: extract version.
    68  
    69  	s.schemaVersion = versionDraft07 // Reasonable default version.
    70  	str, ok := s.strValue(n)
    71  	if !ok {
    72  		// If there's no $schema value, use the default.
    73  		return
    74  	}
    75  	sv, err := parseSchemaVersion(str)
    76  	if err != nil {
    77  		s.errf(n, "invalid $schema URL %q: %v", str, err)
    78  		return
    79  	}
    80  	s.schemaVersionPresent = true
    81  	s.schemaVersion = sv
    82  }