@@ -10,7 +10,6 @@ import (
10
10
"math"
11
11
"math/big"
12
12
"reflect"
13
- "regexp"
14
13
"slices"
15
14
"strings"
16
15
"unicode/utf8"
@@ -19,16 +18,9 @@ import (
19
18
// The value of the "$schema" keyword for the version that we can validate.
20
19
const draft202012 = "https://json-schema.org/draft/2020-12/schema"
21
20
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
-
29
21
// Validate validates the instance, which must be a JSON value, against the schema.
30
22
// 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 {
32
24
if s := rs .root .Schema ; s != "" && s != draft202012 {
33
25
return fmt .Errorf ("cannot validate version %s, only %s" , s , draft202012 )
34
26
}
@@ -39,7 +31,7 @@ func (rs *ResolvedSchema) Validate(instance any) error {
39
31
40
32
// state is the state of single call to ResolvedSchema.Validate.
41
33
type state struct {
42
- rs * ResolvedSchema
34
+ rs * Resolved
43
35
depth int
44
36
}
45
37
@@ -60,10 +52,8 @@ func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *an
60
52
return fmt .Errorf ("max recursion depth of %d reached" , st .depth )
61
53
}
62
54
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" )
67
57
68
58
// Step through interfaces.
69
59
if instance .IsValid () && instance .Kind () == reflect .Interface {
@@ -156,15 +146,8 @@ func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *an
156
146
}
157
147
}
158
148
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 )
168
151
}
169
152
}
170
153
@@ -364,13 +347,8 @@ func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *an
364
347
for vprop , val := range instance .Seq2 () {
365
348
prop := vprop .String ()
366
349
// 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 ) {
374
352
if err := st .validate (val , schema , nil , append (path , prop )); err != nil {
375
353
return err
376
354
}
0 commit comments