| 1 | // Copyright 2025 The JSON Schema Go Project Authors. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package jsonschemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/ |
| 6 | |
| 7 | import ( |
| 8 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 9 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 11 | "hash/maphashscip-go gomod github.com/golang/go/src go1.25.0 `hash/maphash`/" |
| 12 | "iterscip-go gomod github.com/golang/go/src go1.25.0 iter/" |
| 13 | "mathscip-go gomod github.com/golang/go/src go1.25.0 math/" |
| 14 | "math/bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/" |
| 15 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 16 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 17 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 18 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 19 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/" |
| 20 | ) |
| 21 | |
| 22 | // The value of the "$schema" keyword for the version that we can validate. |
| 23 | const draft202012scip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/draft202012. = "https://json-schema.org/draft/2020-12/schema" |
| 24 | |
| 25 | // Validate validates the instance, which must be a JSON value, against the schema. |
| 26 | // It returns nil if validation is successful or an error if it is not. |
| 27 | // If the schema type is "object", instance can be a map[string]any or a struct. |
| 28 | func (rs *Resolvedscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#) Validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#Validate().(instance any) error { |
| 29 | if s := rs.rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#root..Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Schema.; s != "" && s != draft202012scip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/draft202012. { |
| 30 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("cannot validate version %s, only %s", s, draft202012scip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/draft202012.) |
| 31 | } |
| 32 | st := &statescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#{rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs.: rs} |
| 33 | return st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(instance), st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#root., nil) |
| 34 | } |
| 35 | |
| 36 | // validateDefaults walks the schema tree. If it finds a default, it validates it |
| 37 | // against the schema containing it. |
| 38 | // |
| 39 | // TODO(jba): account for dynamic refs. This algorithm simple-mindedly |
| 40 | // treats each schema with a default as its own root. |
| 41 | func (rs *Resolvedscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#) validateDefaultsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#validateDefaults().() error { |
| 42 | if s := rs.rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#root..Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Schema.; s != "" && s != draft202012scip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/draft202012. { |
| 43 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("cannot validate version %s, only %s", s, draft202012scip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/draft202012.) |
| 44 | } |
| 45 | st := &statescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#{rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs.: rs} |
| 46 | for s := range rs.rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#root..allscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#all().() { |
| 47 | // We checked for nil schemas in [Schema.Resolve]. |
| 48 | assertscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/assert().(s != nil, "nil schema") |
| 49 | if s.DynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#DynamicRef. != "" { |
| 50 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("jsonschema: %s: validateDefaults does not support dynamic refs", rs.schemaStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#schemaString().(s)) |
| 51 | } |
| 52 | if s.Defaultscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Default. != nil { |
| 53 | var d any |
| 54 | if err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshal().(s.Defaultscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Default., &d); err != nil { |
| 55 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("unmarshaling default value of schema %s: %w", rs.schemaStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#schemaString().(s), err) |
| 56 | } |
| 57 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(d), s, nil); err != nil { |
| 58 | return err |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | // state is the state of single call to ResolvedSchema.Validate. |
| 66 | type statescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state# struct { |
| 67 | rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs. *Resolvedscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved# |
| 68 | // stack holds the schemas from recursive calls to validate. |
| 69 | // These are the "dynamic scopes" used to resolve dynamic references. |
| 70 | // https://json-schema.org/draft/2020-12/json-schema-core#scopes |
| 71 | stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#stack. []*Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema# |
| 72 | } |
| 73 | |
| 74 | // validate validates the reflected value of the instance. |
| 75 | func (st *statescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#) validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#, schema *Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#, callerAnns *annotationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#) (err error) { |
| 76 | defer wrapfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/wrapf().(&err, "validating %s", st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..schemaStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#schemaString().(schema)) |
| 77 | |
| 78 | // Maintain a stack for dynamic schema resolution. |
| 79 | st.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#stack. = append(st.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#stack., schema) // push |
| 80 | defer func() { |
| 81 | st.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#stack. = st.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#stack.[:len(st.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#stack.)-1] // pop |
| 82 | }() |
| 83 | |
| 84 | // We checked for nil schemas in [Schema.Resolve]. |
| 85 | assertscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/assert().(schema != nil, "nil schema") |
| 86 | |
| 87 | // Step through interfaces and pointers. |
| 88 | for instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 reflect/Pointer. || instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Interface. { |
| 89 | instance = instance.Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().() |
| 90 | } |
| 91 | |
| 92 | schemaInfo := st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..resolvedInfosscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#resolvedInfos.[schema] |
| 93 | |
| 94 | // type: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-6.1.1 |
| 95 | if schema.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Type. != "" || schema.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Types. != nil { |
| 96 | gotType, ok := jsonTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/jsonType().(instance) |
| 97 | if !ok { |
| 98 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("type: %v of type %[1]T is not a valid JSON value", instance) |
| 99 | } |
| 100 | if schema.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Type. != "" { |
| 101 | // "number" subsumes integers |
| 102 | if !(gotType == schema.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Type. || |
| 103 | gotType == "integer" && schema.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Type. == "number") { |
| 104 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("type: %v has type %q, want %q", instance, gotType, schema.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Type.) |
| 105 | } |
| 106 | } else { |
| 107 | if !(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Containsscip-go gomod github.com/golang/go/src go1.25.0 slices/Contains().(schema.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Types., gotType) || (gotType == "integer" && slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Containsscip-go gomod github.com/golang/go/src go1.25.0 slices/Contains().(schema.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Types., "number"))) { |
| 108 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("type: %v has type %q, want one of %q", |
| 109 | instance, gotType, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(schema.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Types., ", ")) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | // enum: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-6.1.2 |
| 114 | if schema.Enumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Enum. != nil { |
| 115 | ok := false |
| 116 | for _, e := range schema.Enumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Enum. { |
| 117 | if equalValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/equalValue().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(e), instance) { |
| 118 | ok = true |
| 119 | break |
| 120 | } |
| 121 | } |
| 122 | if !ok { |
| 123 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("enum: %v does not equal any of: %v", instance, schema.Enumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Enum.) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // const: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-6.1.3 |
| 128 | if schema.Constscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Const. != nil { |
| 129 | if !equalValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/equalValue().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(*schema.Constscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Const.), instance) { |
| 130 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("const: %v does not equal %v", instance, *schema.Constscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Const.) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // numbers: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-6.2 |
| 135 | if schema.MultipleOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MultipleOf. != nil || schema.Minimumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Minimum. != nil || schema.Maximumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Maximum. != nil || schema.ExclusiveMinimumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#ExclusiveMinimum. != nil || schema.ExclusiveMaximumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#ExclusiveMaximum. != nil { |
| 136 | n, ok := jsonNumberscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/jsonNumber().(instance) |
| 137 | if ok { // these keywords don't apply to non-numbers |
| 138 | if schema.MultipleOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MultipleOf. != nil { |
| 139 | // TODO: validate MultipleOf as non-zero. |
| 140 | // The test suite assumes floats. |
| 141 | nf, _ := n.Float64scip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Rat#Float64().() // don't care if it's exact or not |
| 142 | if _, f := mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Modfscip-go gomod github.com/golang/go/src go1.25.0 math/Modf().(nf / *schema.MultipleOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MultipleOf.); f != 0 { |
| 143 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("multipleOf: %s is not a multiple of %f", n, *schema.MultipleOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MultipleOf.) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | m := new(bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/.Ratscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Rat#) // reuse for all of the following |
| 148 | cmp := func(f float64) int { return n.Cmpscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Rat#Cmp().(m.SetFloat64scip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Rat#SetFloat64().(f)) } |
| 149 | |
| 150 | if schema.Minimumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Minimum. != nil && cmp(*schema.Minimumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Minimum.) < 0 { |
| 151 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("minimum: %s is less than %f", n, *schema.Minimumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Minimum.) |
| 152 | } |
| 153 | if schema.Maximumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Maximum. != nil && cmp(*schema.Maximumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Maximum.) > 0 { |
| 154 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("maximum: %s is greater than %f", n, *schema.Maximumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Maximum.) |
| 155 | } |
| 156 | if schema.ExclusiveMinimumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#ExclusiveMinimum. != nil && cmp(*schema.ExclusiveMinimumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#ExclusiveMinimum.) <= 0 { |
| 157 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("exclusiveMinimum: %s is less than or equal to %f", n, *schema.ExclusiveMinimumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#ExclusiveMinimum.) |
| 158 | } |
| 159 | if schema.ExclusiveMaximumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#ExclusiveMaximum. != nil && cmp(*schema.ExclusiveMaximumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#ExclusiveMaximum.) >= 0 { |
| 160 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("exclusiveMaximum: %s is greater than or equal to %f", n, *schema.ExclusiveMaximumscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#ExclusiveMaximum.) |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // strings: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-6.3 |
| 166 | if instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Stringscip-go gomod github.com/golang/go/src go1.25.0 reflect/String. && (schema.MinLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinLength. != nil || schema.MaxLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxLength. != nil || schema.Patternscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Pattern. != "") { |
| 167 | str := instance.Stringscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#String().() |
| 168 | n := utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.RuneCountInStringscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/RuneCountInString().(str) |
| 169 | if schema.MinLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinLength. != nil { |
| 170 | if m := *schema.MinLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinLength.; n < m { |
| 171 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("minLength: %q contains %d Unicode code points, fewer than %d", str, n, m) |
| 172 | } |
| 173 | } |
| 174 | if schema.MaxLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxLength. != nil { |
| 175 | if m := *schema.MaxLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxLength.; n > m { |
| 176 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("maxLength: %q contains %d Unicode code points, more than %d", str, n, m) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if schema.Patternscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Pattern. != "" && !schemaInfo.patternscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#pattern..MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString().(str) { |
| 181 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("pattern: %q does not match regular expression %q", str, schema.Patternscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Pattern.) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | var anns annotationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations# // all the annotations for this call and child calls |
| 186 | |
| 187 | // $ref: https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3.1 |
| 188 | if schema.Refscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Ref. != "" { |
| 189 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance, schemaInfo.resolvedRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#resolvedRef., &anns); err != nil { |
| 190 | return err |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // $dynamicRef: https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3.2 |
| 195 | if schema.DynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#DynamicRef. != "" { |
| 196 | // The ref behaves lexically or dynamically, but not both. |
| 197 | assertscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/assert().((schemaInfo.resolvedDynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#resolvedDynamicRef. == nil) != (schemaInfo.dynamicRefAnchorscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#dynamicRefAnchor. == ""), |
| 198 | "DynamicRef not resolved properly") |
| 199 | if schemaInfo.resolvedDynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#resolvedDynamicRef. != nil { |
| 200 | // Same as $ref. |
| 201 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance, schemaInfo.resolvedDynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#resolvedDynamicRef., &anns); err != nil { |
| 202 | return err |
| 203 | } |
| 204 | } else { |
| 205 | // Dynamic behavior. |
| 206 | // Look for the base of the outermost schema on the stack with this dynamic |
| 207 | // anchor. (Yes, outermost: the one farthest from here. This the opposite |
| 208 | // of how ordinary dynamic variables behave.) |
| 209 | // Why the base of the schema being validated and not the schema itself? |
| 210 | // Because the base is the scope for anchors. In fact it's possible to |
| 211 | // refer to a schema that is not on the stack, but a child of some base |
| 212 | // on the stack. |
| 213 | // For an example, search for "detached" in testdata/draft2020-12/dynamicRef.json. |
| 214 | var dynamicSchema *Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema# |
| 215 | for _, s := range st.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#stack. { |
| 216 | base := st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..resolvedInfosscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#resolvedInfos.[s].basescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#base. |
| 217 | info, ok := st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..resolvedInfosscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#resolvedInfos.[base].anchorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#anchors.[schemaInfo.dynamicRefAnchorscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#dynamicRefAnchor.] |
| 218 | if ok && info.dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/anchorInfo#dynamic. { |
| 219 | dynamicSchema = info.schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/anchorInfo#schema. |
| 220 | break |
| 221 | } |
| 222 | } |
| 223 | if dynamicSchema == nil { |
| 224 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("missing dynamic anchor %q", schemaInfo.dynamicRefAnchorscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#dynamicRefAnchor.) |
| 225 | } |
| 226 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance, dynamicSchema, &anns); err != nil { |
| 227 | return err |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // logic |
| 233 | // https://json-schema.org/draft/2020-12/json-schema-core#section-10.2 |
| 234 | // These must happen before arrays and objects because if they evaluate an item or property, |
| 235 | // then the unevaluatedItems/Properties schemas don't apply to it. |
| 236 | // See https://json-schema.org/draft/2020-12/json-schema-core#section-11.2, paragraph 4. |
| 237 | // |
| 238 | // If any of these fail, then validation fails, even if there is an unevaluatedXXX |
| 239 | // keyword in the schema. The spec is unclear about this, but that is the intention. |
| 240 | |
| 241 | valid := func(s *Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#, anns *annotationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#) bool { return st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance, s, anns) == nil } |
| 242 | |
| 243 | if schema.AllOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AllOf. != nil { |
| 244 | for _, ss := range schema.AllOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AllOf. { |
| 245 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance, ss, &anns); err != nil { |
| 246 | return err |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | if schema.AnyOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AnyOf. != nil { |
| 251 | // We must visit them all, to collect annotations. |
| 252 | ok := false |
| 253 | for _, ss := range schema.AnyOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AnyOf. { |
| 254 | if valid(ss, &anns) { |
| 255 | ok = true |
| 256 | } |
| 257 | } |
| 258 | if !ok { |
| 259 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("anyOf: did not validate against any of %v", schema.AnyOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AnyOf.) |
| 260 | } |
| 261 | } |
| 262 | if schema.OneOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#OneOf. != nil { |
| 263 | // Exactly one. |
| 264 | var okSchema *Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema# |
| 265 | for _, ss := range schema.OneOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#OneOf. { |
| 266 | if valid(ss, &anns) { |
| 267 | if okSchema != nil { |
| 268 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("oneOf: validated against both %v and %v", okSchema, ss) |
| 269 | } |
| 270 | okSchema = ss |
| 271 | } |
| 272 | } |
| 273 | if okSchema == nil { |
| 274 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("oneOf: did not validate against any of %v", schema.OneOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#OneOf.) |
| 275 | } |
| 276 | } |
| 277 | if schema.Notscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Not. != nil { |
| 278 | // Ignore annotations from "not". |
| 279 | if valid(schema.Notscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Not., nil) { |
| 280 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("not: validated against %v", schema.Notscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Not.) |
| 281 | } |
| 282 | } |
| 283 | if schema.Ifscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#If. != nil { |
| 284 | var ss *Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema# |
| 285 | if valid(schema.Ifscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#If., &anns) { |
| 286 | ss = schema.Thenscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Then. |
| 287 | } else { |
| 288 | ss = schema.Elsescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Else. |
| 289 | } |
| 290 | if ss != nil { |
| 291 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance, ss, &anns); err != nil { |
| 292 | return err |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // arrays |
| 298 | // TODO(jba): consider arrays of structs. |
| 299 | if instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Arrayscip-go gomod github.com/golang/go/src go1.25.0 reflect/Array. || instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Slicescip-go gomod github.com/golang/go/src go1.25.0 reflect/Slice. { |
| 300 | // https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1 |
| 301 | // This validate call doesn't collect annotations for the items of the instance; they are separate |
| 302 | // instances in their own right. |
| 303 | // TODO(jba): if the test suite doesn't cover this case, add a test. For example, nested arrays. |
| 304 | for i, ischema := range schema.PrefixItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#PrefixItems. { |
| 305 | if i >= instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().() { |
| 306 | break // shorter is OK |
| 307 | } |
| 308 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Index().(i), ischema, nil); err != nil { |
| 309 | return err |
| 310 | } |
| 311 | } |
| 312 | anns.noteEndIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#noteEndIndex().(min(len(schema.PrefixItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#PrefixItems.), instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().())) |
| 313 | |
| 314 | if schema.Itemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Items. != nil { |
| 315 | for i := len(schema.PrefixItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#PrefixItems.); i < instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().(); i++ { |
| 316 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Index().(i), schema.Itemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Items., nil); err != nil { |
| 317 | return err |
| 318 | } |
| 319 | } |
| 320 | // Note that all the items in this array have been validated. |
| 321 | anns.allItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#allItems. = true |
| 322 | } |
| 323 | |
| 324 | nContains := 0 |
| 325 | if schema.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Contains. != nil { |
| 326 | for i := range instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().() { |
| 327 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Index().(i), schema.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Contains., nil); err == nil { |
| 328 | nContains++ |
| 329 | anns.noteIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#noteIndex().(i) |
| 330 | } |
| 331 | } |
| 332 | if nContains == 0 && (schema.MinContainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinContains. == nil || *schema.MinContainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinContains. > 0) { |
| 333 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("contains: %s does not have an item matching %s", instance, schema.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Contains.) |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-6.4 |
| 338 | // TODO(jba): check that these next four keywords' values are integers. |
| 339 | if schema.MinContainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinContains. != nil && schema.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Contains. != nil { |
| 340 | if m := *schema.MinContainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinContains.; nContains < m { |
| 341 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("minContains: contains validated %d items, less than %d", nContains, m) |
| 342 | } |
| 343 | } |
| 344 | if schema.MaxContainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxContains. != nil && schema.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Contains. != nil { |
| 345 | if m := *schema.MaxContainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxContains.; nContains > m { |
| 346 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("maxContains: contains validated %d items, greater than %d", nContains, m) |
| 347 | } |
| 348 | } |
| 349 | if schema.MinItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinItems. != nil { |
| 350 | if m := *schema.MinItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinItems.; instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().() < m { |
| 351 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("minItems: array length %d is less than %d", instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().(), m) |
| 352 | } |
| 353 | } |
| 354 | if schema.MaxItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxItems. != nil { |
| 355 | if m := *schema.MaxItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxItems.; instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().() > m { |
| 356 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("maxItems: array length %d is greater than %d", instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().(), m) |
| 357 | } |
| 358 | } |
| 359 | if schema.UniqueItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#UniqueItems. { |
| 360 | if instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().() > 1 { |
| 361 | // Hash each item and compare the hashes. |
| 362 | // If two hashes differ, the items differ. |
| 363 | // If two hashes are the same, compare the collisions for equality. |
| 364 | // (The same logic as hash table lookup.) |
| 365 | // TODO(jba): Use container/hash.Map when it becomes available (https://go.dev/issue/69559), |
| 366 | hashes := map[uint64][]int{} // from hash to indices |
| 367 | seed := maphashscip-go gomod github.com/golang/go/src go1.25.0 `hash/maphash`/.MakeSeedscip-go gomod github.com/golang/go/src go1.25.0 `hash/maphash`/MakeSeed().() |
| 368 | for i := range instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().() { |
| 369 | item := instance.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Index().(i) |
| 370 | var h maphashscip-go gomod github.com/golang/go/src go1.25.0 `hash/maphash`/.Hashscip-go gomod github.com/golang/go/src go1.25.0 `hash/maphash`/Hash# |
| 371 | h.SetSeedscip-go gomod github.com/golang/go/src go1.25.0 `hash/maphash`/Hash#SetSeed().(seed) |
| 372 | hashValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/hashValue().(&h, item) |
| 373 | hv := h.Sum64scip-go gomod github.com/golang/go/src go1.25.0 `hash/maphash`/Hash#Sum64().() |
| 374 | if sames := hashes[hv]; len(sames) > 0 { |
| 375 | for _, j := range sames { |
| 376 | if equalValuescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/equalValue().(item, instance.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Index().(j)) { |
| 377 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("uniqueItems: array items %d and %d are equal", i, j) |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | hashes[hv] = append(hashes[hv], i) |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // https://json-schema.org/draft/2020-12/json-schema-core#section-11.2 |
| 387 | if schema.UnevaluatedItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#UnevaluatedItems. != nil && !anns.allItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#allItems. { |
| 388 | // Apply this subschema to all items in the array that haven't been successfully validated. |
| 389 | // That includes validations by subschemas on the same instance, like allOf. |
| 390 | for i := anns.endIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#endIndex.; i < instance.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().(); i++ { |
| 391 | if !anns.evaluatedIndexesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#evaluatedIndexes.[i] { |
| 392 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Index().(i), schema.UnevaluatedItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#UnevaluatedItems., nil); err != nil { |
| 393 | return err |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | anns.allItemsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#allItems. = true |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // objects |
| 402 | // https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.2 |
| 403 | // Validating structs is problematic. See https://github.com/google/jsonschema-go/issues/23. |
| 404 | if instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Structscip-go gomod github.com/golang/go/src go1.25.0 reflect/Struct. { |
| 405 | return errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("cannot validate against a struct; see https://github.com/google/jsonschema-go/issues/23 for details") |
| 406 | } |
| 407 | if instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Mapscip-go gomod github.com/golang/go/src go1.25.0 reflect/Map. { |
| 408 | if kt := instance.Typescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Type().().Keyscip-go gomod github.com/golang/go/src go1.25.0 reflect/Type#Key().(); kt.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Stringscip-go gomod github.com/golang/go/src go1.25.0 reflect/String. { |
| 409 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("map key type %s is not a string", kt) |
| 410 | } |
| 411 | // Track the evaluated properties for just this schema, to support additionalProperties. |
| 412 | // If we used anns here, then we'd be including properties evaluated in subschemas |
| 413 | // from allOf, etc., which additionalProperties shouldn't observe. |
| 414 | evalProps := map[string]bool{} |
| 415 | for prop, subschema := range schema.Propertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Properties. { |
| 416 | val := propertyscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/property().(instance, prop) |
| 417 | if !val.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() { |
| 418 | // It's OK if the instance doesn't have the property. |
| 419 | continue |
| 420 | } |
| 421 | // If the instance is a struct and an optional property has the zero |
| 422 | // value, then we could interpret it as present or missing. Be generous: |
| 423 | // assume it's missing, and thus always validates successfully. |
| 424 | if instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Structscip-go gomod github.com/golang/go/src go1.25.0 reflect/Struct. && val.IsZeroscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsZero().() && !schemaInfo.isRequiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#isRequired.[prop] { |
| 425 | continue |
| 426 | } |
| 427 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(val, subschema, nil); err != nil { |
| 428 | return err |
| 429 | } |
| 430 | evalProps[prop] = true |
| 431 | } |
| 432 | if len(schema.PatternPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#PatternProperties.) > 0 { |
| 433 | for prop, val := range propertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/properties().(instance) { |
| 434 | // Check every matching pattern. |
| 435 | for re, schema := range schemaInfo.patternPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#patternProperties. { |
| 436 | if re.MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString().(prop) { |
| 437 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(val, schema, nil); err != nil { |
| 438 | return err |
| 439 | } |
| 440 | evalProps[prop] = true |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | if schema.AdditionalPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AdditionalProperties. != nil { |
| 446 | // Special case for a better error message when additional properties is |
| 447 | // 'falsy' |
| 448 | // |
| 449 | // If additionalProperties is {"not":{}} (which is how we |
| 450 | // unmarshal "false"), we can produce a better error message that |
| 451 | // summarizes all the extra properties. Otherwise, we fall back to the |
| 452 | // default validation. |
| 453 | // |
| 454 | // Note: this is much faster than comparing with falseSchema using Equal. |
| 455 | isFalsy := schema.AdditionalPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AdditionalProperties..Notscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Not. != nil && reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(*schema.AdditionalPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AdditionalProperties..Notscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Not.).IsZeroscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsZero().() |
| 456 | if isFalsy { |
| 457 | var disallowed []string |
| 458 | for prop := range propertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/properties().(instance) { |
| 459 | if !evalProps[prop] { |
| 460 | disallowed = append(disallowed, prop) |
| 461 | } |
| 462 | } |
| 463 | if len(disallowed) > 0 { |
| 464 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("unexpected additional properties %q", disallowed) |
| 465 | } |
| 466 | } else { |
| 467 | // Apply to all properties not handled above. |
| 468 | for prop, val := range propertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/properties().(instance) { |
| 469 | if !evalProps[prop] { |
| 470 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(val, schema.AdditionalPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#AdditionalProperties., nil); err != nil { |
| 471 | return err |
| 472 | } |
| 473 | evalProps[prop] = true |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | anns.notePropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#noteProperties().(evalProps) |
| 479 | if schema.PropertyNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#PropertyNames. != nil { |
| 480 | // Note: properties unnecessarily fetches each value. We could define a propertyNames function |
| 481 | // if performance ever matters. |
| 482 | for prop := range propertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/properties().(instance) { |
| 483 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(prop), schema.PropertyNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#PropertyNames., nil); err != nil { |
| 484 | return err |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | // https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-6.5 |
| 490 | var min, max int |
| 491 | if schema.MinPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinProperties. != nil || schema.MaxPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxProperties. != nil { |
| 492 | min, max = numPropertiesBoundsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/numPropertiesBounds().(instance, schemaInfo.isRequiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#isRequired.) |
| 493 | } |
| 494 | if schema.MinPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinProperties. != nil { |
| 495 | if n, m := max, *schema.MinPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MinProperties.; n < m { |
| 496 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("minProperties: object has %d properties, less than %d", n, m) |
| 497 | } |
| 498 | } |
| 499 | if schema.MaxPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxProperties. != nil { |
| 500 | if n, m := min, *schema.MaxPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#MaxProperties.; n > m { |
| 501 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("maxProperties: object has %d properties, greater than %d", n, m) |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | hasProperty := func(prop string) bool { |
| 506 | return propertyscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/property().(instance, prop).IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() |
| 507 | } |
| 508 | |
| 509 | missingProperties := func(props []string) []string { |
| 510 | var missing []string |
| 511 | for _, p := range props { |
| 512 | if !hasProperty(p) { |
| 513 | missing = append(missing, p) |
| 514 | } |
| 515 | } |
| 516 | return missing |
| 517 | } |
| 518 | |
| 519 | if schema.Requiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Required. != nil { |
| 520 | if m := missingProperties(schema.Requiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Required.); len(m) > 0 { |
| 521 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("required: missing properties: %q", m) |
| 522 | } |
| 523 | } |
| 524 | if schema.DependentRequiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#DependentRequired. != nil { |
| 525 | // "Validation succeeds if, for each name that appears in both the instance |
| 526 | // and as a name within this keyword's value, every item in the corresponding |
| 527 | // array is also the name of a property in the instance." §6.5.4 |
| 528 | for dprop, reqs := range schema.DependentRequiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#DependentRequired. { |
| 529 | if hasProperty(dprop) { |
| 530 | if m := missingProperties(reqs); len(m) > 0 { |
| 531 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("dependentRequired[%q]: missing properties %q", dprop, m) |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // https://json-schema.org/draft/2020-12/json-schema-core#section-10.2.2.4 |
| 538 | if schema.DependentSchemasscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#DependentSchemas. != nil { |
| 539 | // This does not collect annotations, although it seems like it should. |
| 540 | for dprop, ss := range schema.DependentSchemasscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#DependentSchemas. { |
| 541 | if hasProperty(dprop) { |
| 542 | // TODO: include dependentSchemas[dprop] in the errors. |
| 543 | err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(instance, ss, &anns) |
| 544 | if err != nil { |
| 545 | return err |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | if schema.UnevaluatedPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#UnevaluatedProperties. != nil && !anns.allPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#allProperties. { |
| 551 | // This looks a lot like AdditionalProperties, but depends on in-place keywords like allOf |
| 552 | // in addition to sibling keywords. |
| 553 | for prop, val := range propertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/properties().(instance) { |
| 554 | if !anns.evaluatedPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#evaluatedProperties.[prop] { |
| 555 | if err := st.validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#validate().(val, schema.UnevaluatedPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#UnevaluatedProperties., nil); err != nil { |
| 556 | return err |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | // The spec says the annotation should be the set of evaluated properties, but we can optimize |
| 561 | // by setting a single boolean, since after this succeeds all properties will be validated. |
| 562 | // See https://json-schema.slack.com/archives/CT7FF623C/p1745592564381459. |
| 563 | anns.allPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#allProperties. = true |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if callerAnns != nil { |
| 568 | // Our caller wants to know what we've validated. |
| 569 | callerAnns.mergescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/annotations#merge().(&anns) |
| 570 | } |
| 571 | return nil |
| 572 | } |
| 573 | |
| 574 | // resolveDynamicRef returns the schema referred to by the argument schema's |
| 575 | // $dynamicRef value. |
| 576 | // It returns an error if the dynamic reference has no referent. |
| 577 | // If there is no $dynamicRef, resolveDynamicRef returns nil, nil. |
| 578 | // See https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3.2. |
| 579 | func (st *statescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#) resolveDynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#resolveDynamicRef().(schema *Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#) (*Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#, error) { |
| 580 | if schema.DynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#DynamicRef. == "" { |
| 581 | return nil, nil |
| 582 | } |
| 583 | info := st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..resolvedInfosscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#resolvedInfos.[schema] |
| 584 | // The ref behaves lexically or dynamically, but not both. |
| 585 | assertscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/assert().((info.resolvedDynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#resolvedDynamicRef. == nil) != (info.dynamicRefAnchorscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#dynamicRefAnchor. == ""), |
| 586 | "DynamicRef not statically resolved properly") |
| 587 | if r := info.resolvedDynamicRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#resolvedDynamicRef.; r != nil { |
| 588 | // Same as $ref. |
| 589 | return r, nil |
| 590 | } |
| 591 | // Dynamic behavior. |
| 592 | // Look for the base of the outermost schema on the stack with this dynamic |
| 593 | // anchor. (Yes, outermost: the one farthest from here. This the opposite |
| 594 | // of how ordinary dynamic variables behave.) |
| 595 | // Why the base of the schema being validated and not the schema itself? |
| 596 | // Because the base is the scope for anchors. In fact it's possible to |
| 597 | // refer to a schema that is not on the stack, but a child of some base |
| 598 | // on the stack. |
| 599 | // For an example, search for "detached" in testdata/draft2020-12/dynamicRef.json. |
| 600 | for _, s := range st.stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#stack. { |
| 601 | base := st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..resolvedInfosscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#resolvedInfos.[s].basescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#base. |
| 602 | info, ok := st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..resolvedInfosscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#resolvedInfos.[base].anchorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#anchors.[info.dynamicRefAnchorscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#dynamicRefAnchor.] |
| 603 | if ok && info.dynamicscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/anchorInfo#dynamic. { |
| 604 | return info.schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/anchorInfo#schema., nil |
| 605 | } |
| 606 | } |
| 607 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("missing dynamic anchor %q", info.dynamicRefAnchorscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#dynamicRefAnchor.) |
| 608 | } |
| 609 | |
| 610 | // ApplyDefaults modifies an instance by applying the schema's defaults to it. If |
| 611 | // a schema or sub-schema has a default, then a corresponding zero instance value |
| 612 | // is set to the default. |
| 613 | // |
| 614 | // The JSON Schema specification does not describe how defaults should be interpreted. |
| 615 | // This method honors defaults only on properties, and only those that are not required. |
| 616 | // If the instance is a map and the property is missing, the property is added to |
| 617 | // the map with the default. |
| 618 | // If the instance is a struct, the field corresponding to the property exists, and |
| 619 | // its value is zero, the field is set to the default. |
| 620 | // ApplyDefaults can panic if a default cannot be assigned to a field. |
| 621 | // |
| 622 | // The argument must be a pointer to the instance. |
| 623 | // (In case we decide that top-level defaults are meaningful.) |
| 624 | // |
| 625 | // It is recommended to first call Resolve with a ValidateDefaults option of true, |
| 626 | // then call this method, and lastly call Validate. |
| 627 | func (rs *Resolvedscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#) ApplyDefaultsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#ApplyDefaults().(instancep any) error { |
| 628 | // TODO(jba): consider what defaults on top-level or array instances might mean. |
| 629 | // TODO(jba): follow $ref and $dynamicRef |
| 630 | // TODO(jba): apply defaults on sub-schemas to corresponding sub-instances. |
| 631 | st := &statescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#{rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs.: rs} |
| 632 | return st.applyDefaultsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#applyDefaults().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(instancep), rs.rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#root.) |
| 633 | } |
| 634 | |
| 635 | // Leave this as a potentially recursive helper function, because we'll surely want |
| 636 | // to apply defaults on sub-schemas someday. |
| 637 | func (st *statescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#) applyDefaultsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#applyDefaults().(instancep reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#, schema *Schemascip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#) (err error) { |
| 638 | defer wrapfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/wrapf().(&err, "applyDefaults: schema %s, instance %v", st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..schemaStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#schemaString().(schema), instancep) |
| 639 | |
| 640 | schemaInfo := st.rsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/state#rs..resolvedInfosscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Resolved#resolvedInfos.[schema] |
| 641 | instance := instancep.Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().() |
| 642 | if instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Interface. && instance.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() { |
| 643 | // If we unmarshalled into 'any', the default object unmarshalling will be map[string]any. |
| 644 | instance = instance.Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().() |
| 645 | } |
| 646 | if instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Mapscip-go gomod github.com/golang/go/src go1.25.0 reflect/Map. || instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Structscip-go gomod github.com/golang/go/src go1.25.0 reflect/Struct. { |
| 647 | if instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Mapscip-go gomod github.com/golang/go/src go1.25.0 reflect/Map. { |
| 648 | if kt := instance.Typescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Type().().Keyscip-go gomod github.com/golang/go/src go1.25.0 reflect/Type#Key().(); kt.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Stringscip-go gomod github.com/golang/go/src go1.25.0 reflect/String. { |
| 649 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("map key type %s is not a string", kt) |
| 650 | } |
| 651 | } |
| 652 | for prop, subschema := range schema.Propertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Properties. { |
| 653 | // Ignore defaults on required properties. (A required property shouldn't have a default.) |
| 654 | if schemaInfo.isRequiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/resolvedInfo#isRequired.[prop] { |
| 655 | continue |
| 656 | } |
| 657 | val := propertyscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/property().(instance, prop) |
| 658 | switch instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() { |
| 659 | case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Mapscip-go gomod github.com/golang/go/src go1.25.0 reflect/Map.: |
| 660 | // If there is a default for this property, and the map key is missing, |
| 661 | // set the map value to the default. |
| 662 | if subschema.Defaultscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Default. != nil && !val.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() { |
| 663 | // Create an lvalue, since map values aren't addressable. |
| 664 | lvalue := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Newscip-go gomod github.com/golang/go/src go1.25.0 reflect/New().(instance.Typescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Type().().Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Type#Elem().()) |
| 665 | if err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshal().(subschema.Defaultscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Default., lvalue.Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Interface().()); err != nil { |
| 666 | return err |
| 667 | } |
| 668 | instance.SetMapIndexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#SetMapIndex().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(prop), lvalue.Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().()) |
| 669 | } |
| 670 | case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Structscip-go gomod github.com/golang/go/src go1.25.0 reflect/Struct.: |
| 671 | // If there is a default for this property, and the field exists but is zero, |
| 672 | // set the field to the default. |
| 673 | if subschema.Defaultscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Default. != nil && val.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() && val.IsZeroscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsZero().() { |
| 674 | if err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshal().(subschema.Defaultscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/Schema#Default., val.Addrscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Addr().().Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Interface().()); err != nil { |
| 675 | return err |
| 676 | } |
| 677 | } |
| 678 | default: |
| 679 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("applyDefaults: property %s: bad value %s of kind %s", |
| 680 | prop, instance, instance.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().())) |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | return nil |
| 685 | } |
| 686 | |
| 687 | // property returns the value of the property of v with the given name, or the invalid |
| 688 | // reflect.Value if there is none. |
| 689 | // If v is a map, the property is the value of the map whose key is name. |
| 690 | // If v is a struct, the property is the value of the field with the given name according |
| 691 | // to the encoding/json package (see [jsonName]). |
| 692 | // If v is anything else, property panics. |
| 693 | func propertyscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/property().(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#, name string) reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value# { |
| 694 | switch v.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() { |
| 695 | case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Mapscip-go gomod github.com/golang/go/src go1.25.0 reflect/Map.: |
| 696 | return v.MapIndexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#MapIndex().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(name)) |
| 697 | case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Structscip-go gomod github.com/golang/go/src go1.25.0 reflect/Struct.: |
| 698 | props := structPropertiesOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/structPropertiesOf().(v.Typescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Type().()) |
| 699 | // Ignore nonexistent properties. |
| 700 | if sf, ok := props[name]; ok { |
| 701 | return v.FieldByIndexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#FieldByIndex().(sf.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/StructField#Index.) |
| 702 | } |
| 703 | return reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#{} |
| 704 | default: |
| 705 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("property(%q): bad value %s of kind %s", name, v, v.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().())) |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | // properties returns an iterator over the names and values of all properties |
| 710 | // in v, which must be a map or a struct. |
| 711 | // If a struct, zero-valued properties that are marked omitempty or omitzero |
| 712 | // are excluded. |
| 713 | func propertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/properties().(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seq2scip-go gomod github.com/golang/go/src go1.25.0 iter/Seq2#[string, reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#] { |
| 714 | return func(yield func(string, reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#) bool) { |
| 715 | switch v.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() { |
| 716 | case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Mapscip-go gomod github.com/golang/go/src go1.25.0 reflect/Map.: |
| 717 | for k, e := range v.Seq2scip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Seq2().() { |
| 718 | if !yield(k.Stringscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#String().(), e) { |
| 719 | return |
| 720 | } |
| 721 | } |
| 722 | case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Structscip-go gomod github.com/golang/go/src go1.25.0 reflect/Struct.: |
| 723 | for name, sf := range structPropertiesOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/structPropertiesOf().(v.Typescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Type().()) { |
| 724 | val := v.FieldByIndexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#FieldByIndex().(sf.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/StructField#Index.) |
| 725 | if val.IsZeroscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsZero().() { |
| 726 | info := fieldJSONInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/fieldJSONInfo().(sf) |
| 727 | if info.settingsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/jsonInfo#settings.["omitempty"] || info.settingsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/jsonInfo#settings.["omitzero"] { |
| 728 | continue |
| 729 | } |
| 730 | } |
| 731 | if !yield(name, val) { |
| 732 | return |
| 733 | } |
| 734 | } |
| 735 | default: |
| 736 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("bad value %s of kind %s", v, v.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().())) |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // numPropertiesBounds returns bounds on the number of v's properties. |
| 742 | // v must be a map or a struct. |
| 743 | // If v is a map, both bounds are the map's size. |
| 744 | // If v is a struct, the max is the number of struct properties. |
| 745 | // But since we don't know whether a zero value indicates a missing optional property |
| 746 | // or not, be generous and use the number of non-zero properties as the min. |
| 747 | func numPropertiesBoundsscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/numPropertiesBounds().(v reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#, isRequired map[string]bool) (int, int) { |
| 748 | switch v.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() { |
| 749 | case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Mapscip-go gomod github.com/golang/go/src go1.25.0 reflect/Map.: |
| 750 | return v.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().(), v.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().() |
| 751 | case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Structscip-go gomod github.com/golang/go/src go1.25.0 reflect/Struct.: |
| 752 | sp := structPropertiesOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/structPropertiesOf().(v.Typescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Type().()) |
| 753 | min := 0 |
| 754 | for prop, sf := range sp { |
| 755 | if !v.FieldByIndexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#FieldByIndex().(sf.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/StructField#Index.).IsZeroscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsZero().() || isRequired[prop] { |
| 756 | min++ |
| 757 | } |
| 758 | } |
| 759 | return min, len(sp) |
| 760 | default: |
| 761 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("properties: bad value: %s of kind %s", v, v.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().())) |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // A propertyMap is a map from property name to struct field index. |
| 766 | type propertyMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/propertyMap# = map[string]reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.StructFieldscip-go gomod github.com/golang/go/src go1.25.0 reflect/StructField# |
| 767 | |
| 768 | var structPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/structProperties. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mapscip-go gomod github.com/golang/go/src go1.25.0 sync/Map# // from reflect.Type to propertyMap |
| 769 | |
| 770 | // structPropertiesOf returns the JSON Schema properties for the struct type t. |
| 771 | // The caller must not mutate the result. |
| 772 | func structPropertiesOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/structPropertiesOf().(t reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Typescip-go gomod github.com/golang/go/src go1.25.0 reflect/Type#) propertyMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/propertyMap# { |
| 773 | // Mutex not necessary: at worst we'll recompute the same value. |
| 774 | if props, ok := structPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/structProperties..Loadscip-go gomod github.com/golang/go/src go1.25.0 sync/Map#Load().(t); ok { |
| 775 | return props.(propertyMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/propertyMap#) |
| 776 | } |
| 777 | props := map[string]reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.StructFieldscip-go gomod github.com/golang/go/src go1.25.0 reflect/StructField#{} |
| 778 | for _, sf := range reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.VisibleFieldsscip-go gomod github.com/golang/go/src go1.25.0 reflect/VisibleFields().(t) { |
| 779 | if sf.Anonymousscip-go gomod github.com/golang/go/src go1.25.0 reflect/StructField#Anonymous. { |
| 780 | continue |
| 781 | } |
| 782 | info := fieldJSONInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/fieldJSONInfo().(sf) |
| 783 | if !info.omitscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/jsonInfo#omit. { |
| 784 | props[info.namescip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/jsonInfo#name.] = sf |
| 785 | } |
| 786 | } |
| 787 | structPropertiesscip-go gomod golang.org/x/tools . `golang.org/x/tools/debian/go/src/github.com/google/jsonschema-go/jsonschema`/structProperties..Storescip-go gomod github.com/golang/go/src go1.25.0 sync/Map#Store().(t, props) |
| 788 | return props |
| 789 | } |