@@ -10,7 +10,6 @@ import (
1010 "math"
1111 "math/big"
1212 "reflect"
13- "regexp"
1413 "slices"
1514 "strings"
1615 "unicode/utf8"
@@ -19,16 +18,9 @@ import (
1918// The value of the "$schema" keyword for the version that we can validate.
2019const draft202012 = "https://json-schema.org/draft/2020-12/schema"
2120
22- // Temporary definition of ResolvedSchema.
23- // The full definition deals with references between schemas, specifically the $id, $anchor and $ref keywords.
24- // We'll ignore that for now.
25- type ResolvedSchema struct {
26- root * Schema
27- }
28-
2921// Validate validates the instance, which must be a JSON value, against the schema.
3022// It returns nil if validation is successful or an error if it is not.
31- func (rs * ResolvedSchema ) Validate (instance any ) error {
23+ func (rs * Resolved ) Validate (instance any ) error {
3224 if s := rs .root .Schema ; s != "" && s != draft202012 {
3325 return fmt .Errorf ("cannot validate version %s, only %s" , s , draft202012 )
3426 }
@@ -39,7 +31,7 @@ func (rs *ResolvedSchema) Validate(instance any) error {
3931
4032// state is the state of single call to ResolvedSchema.Validate.
4133type state struct {
42- rs * ResolvedSchema
34+ rs * Resolved
4335 depth int
4436}
4537
@@ -60,10 +52,8 @@ func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *an
6052 return fmt .Errorf ("max recursion depth of %d reached" , st .depth )
6153 }
6254
63- // Treat the nil schema like the empty schema, as accepting everything.
64- if schema == nil {
65- return nil
66- }
55+ // We checked for nil schemas in [Schema.Resolve].
56+ assert (schema != nil , "nil schema" )
6757
6858 // Step through interfaces.
6959 if instance .IsValid () && instance .Kind () == reflect .Interface {
@@ -156,15 +146,8 @@ func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *an
156146 }
157147 }
158148
159- if schema .Pattern != "" {
160- // TODO(jba): compile regexps during schema validation.
161- m , err := regexp .MatchString (schema .Pattern , str )
162- if err != nil {
163- return err
164- }
165- if ! m {
166- return fmt .Errorf ("pattern: %q does not match pattern %q" , str , schema .Pattern )
167- }
149+ if schema .Pattern != "" && ! schema .pattern .MatchString (str ) {
150+ return fmt .Errorf ("pattern: %q does not match regular expression %q" , str , schema .Pattern )
168151 }
169152 }
170153
@@ -364,13 +347,8 @@ func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *an
364347 for vprop , val := range instance .Seq2 () {
365348 prop := vprop .String ()
366349 // Check every matching pattern.
367- for pattern , schema := range schema .PatternProperties {
368- // TODO(jba): pre-compile regexps
369- m , err := regexp .MatchString (pattern , prop )
370- if err != nil {
371- return err
372- }
373- if m {
350+ for re , schema := range schema .patternProperties {
351+ if re .MatchString (prop ) {
374352 if err := st .validate (val , schema , nil , append (path , prop )); err != nil {
375353 return err
376354 }
0 commit comments