@@ -20,39 +20,38 @@ import (
20
20
"fmt"
21
21
"regexp"
22
22
23
- "github.com/arduino/go-paths-helper"
24
23
"github.com/ory/jsonschema/v3"
25
24
"github.com/sirupsen/logrus"
26
25
)
27
26
28
27
// RequiredPropertyMissing returns whether the given required property is missing from the document.
29
- func RequiredPropertyMissing (propertyName string , validationResult * jsonschema. ValidationError , schemasPath * paths. Path ) bool {
30
- return ValidationErrorMatch ("#" , "/required$" , "" , "^#/" + propertyName + "$" , validationResult , schemasPath )
28
+ func RequiredPropertyMissing (propertyName string , validationResult ValidationResult ) bool {
29
+ return ValidationErrorMatch ("#" , "/required$" , "" , "^#/" + propertyName + "$" , validationResult )
31
30
}
32
31
33
32
// PropertyPatternMismatch returns whether the given property did not match the regular expression defined in the JSON schema.
34
- func PropertyPatternMismatch (propertyName string , validationResult * jsonschema. ValidationError , schemasPath * paths. Path ) bool {
35
- return ValidationErrorMatch ("#/" + propertyName , "/pattern$" , "" , "" , validationResult , schemasPath )
33
+ func PropertyPatternMismatch (propertyName string , validationResult ValidationResult ) bool {
34
+ return ValidationErrorMatch ("#/" + propertyName , "/pattern$" , "" , "" , validationResult )
36
35
}
37
36
38
37
// PropertyLessThanMinLength returns whether the given property is less than the minimum length allowed by the schema.
39
- func PropertyLessThanMinLength (propertyName string , validationResult * jsonschema. ValidationError , schemasPath * paths. Path ) bool {
40
- return ValidationErrorMatch ("^#/" + propertyName + "$" , "/minLength$" , "" , "" , validationResult , schemasPath )
38
+ func PropertyLessThanMinLength (propertyName string , validationResult ValidationResult ) bool {
39
+ return ValidationErrorMatch ("^#/" + propertyName + "$" , "/minLength$" , "" , "" , validationResult )
41
40
}
42
41
43
42
// PropertyGreaterThanMaxLength returns whether the given property is greater than the maximum length allowed by the schema.
44
- func PropertyGreaterThanMaxLength (propertyName string , validationResult * jsonschema. ValidationError , schemasPath * paths. Path ) bool {
45
- return ValidationErrorMatch ("^#/" + propertyName + "$" , "/maxLength$" , "" , "" , validationResult , schemasPath )
43
+ func PropertyGreaterThanMaxLength (propertyName string , validationResult ValidationResult ) bool {
44
+ return ValidationErrorMatch ("^#/" + propertyName + "$" , "/maxLength$" , "" , "" , validationResult )
46
45
}
47
46
48
47
// PropertyEnumMismatch returns whether the given property does not match any of the items in the enum array.
49
- func PropertyEnumMismatch (propertyName string , validationResult * jsonschema. ValidationError , schemasPath * paths. Path ) bool {
50
- return ValidationErrorMatch ("#/" + propertyName , "/enum$" , "" , "" , validationResult , schemasPath )
48
+ func PropertyEnumMismatch (propertyName string , validationResult ValidationResult ) bool {
49
+ return ValidationErrorMatch ("#/" + propertyName , "/enum$" , "" , "" , validationResult )
51
50
}
52
51
53
52
// MisspelledOptionalPropertyFound returns whether a misspelled optional property was found.
54
- func MisspelledOptionalPropertyFound (validationResult * jsonschema. ValidationError , schemasPath * paths. Path ) bool {
55
- return ValidationErrorMatch ("#/" , "/misspelledOptionalProperties/" , "" , "" , validationResult , schemasPath )
53
+ func MisspelledOptionalPropertyFound (validationResult ValidationResult ) bool {
54
+ return ValidationErrorMatch ("#/" , "/misspelledOptionalProperties/" , "" , "" , validationResult )
56
55
}
57
56
58
57
// ValidationErrorMatch returns whether the given query matches against the JSON schema validation error.
@@ -62,10 +61,9 @@ func ValidationErrorMatch(
62
61
schemaPointerQuery ,
63
62
schemaPointerValueQuery ,
64
63
failureContextQuery string ,
65
- validationResult * jsonschema.ValidationError ,
66
- schemasPath * paths.Path ,
64
+ validationResult ValidationResult ,
67
65
) bool {
68
- if validationResult == nil {
66
+ if validationResult . Result == nil {
69
67
// No error, so nothing to match.
70
68
logrus .Trace ("Schema validation passed. No match is possible." )
71
69
return false
@@ -82,28 +80,27 @@ func ValidationErrorMatch(
82
80
schemaPointerValueRegexp ,
83
81
failureContextRegexp ,
84
82
validationResult ,
85
- schemasPath )
83
+ )
86
84
}
87
85
88
86
func validationErrorMatch (
89
87
instancePointerRegexp ,
90
88
schemaPointerRegexp ,
91
89
schemaPointerValueRegexp ,
92
90
failureContextRegexp * regexp.Regexp ,
93
- validationError * jsonschema.ValidationError ,
94
- schemasPath * paths.Path ,
91
+ validationError ValidationResult ,
95
92
) bool {
96
93
logrus .Trace ("--------Checking schema validation failure match--------" )
97
- logrus .Tracef ("Checking instance pointer: %s match with regexp: %s" , validationError .InstancePtr , instancePointerRegexp )
98
- if instancePointerRegexp .MatchString (validationError .InstancePtr ) {
94
+ logrus .Tracef ("Checking instance pointer: %s match with regexp: %s" , validationError .Result . InstancePtr , instancePointerRegexp )
95
+ if instancePointerRegexp .MatchString (validationError .Result . InstancePtr ) {
99
96
logrus .Tracef ("Matched!" )
100
- matchedSchemaPointer := validationErrorSchemaPointerMatch (schemaPointerRegexp , validationError , schemasPath )
97
+ matchedSchemaPointer := validationErrorSchemaPointerMatch (schemaPointerRegexp , validationError )
101
98
if matchedSchemaPointer != "" {
102
99
logrus .Tracef ("Matched!" )
103
- if validationErrorSchemaPointerValueMatch (schemaPointerValueRegexp , validationError . SchemaURL , matchedSchemaPointer , schemasPath ) {
100
+ if validationErrorSchemaPointerValueMatch (schemaPointerValueRegexp , validationError , matchedSchemaPointer ) {
104
101
logrus .Tracef ("Matched!" )
105
- logrus .Tracef ("Checking failure context: %v match with regexp: %s" , validationError .Context , failureContextRegexp )
106
- if validationErrorContextMatch (failureContextRegexp , validationError ) {
102
+ logrus .Tracef ("Checking failure context: %v match with regexp: %s" , validationError .Result . Context , failureContextRegexp )
103
+ if validationErrorContextMatch (failureContextRegexp , validationError . Result ) {
107
104
logrus .Tracef ("Matched!" )
108
105
return true
109
106
}
@@ -112,14 +109,16 @@ func validationErrorMatch(
112
109
}
113
110
114
111
// Recursively check all causes for a match.
115
- for _ , validationErrorCause := range validationError .Causes {
112
+ for _ , validationErrorCause := range validationError .Result . Causes {
116
113
if validationErrorMatch (
117
114
instancePointerRegexp ,
118
115
schemaPointerRegexp ,
119
116
schemaPointerValueRegexp ,
120
117
failureContextRegexp ,
121
- validationErrorCause ,
122
- schemasPath ,
118
+ ValidationResult {
119
+ Result : validationErrorCause ,
120
+ dataLoader : validationError .dataLoader ,
121
+ },
123
122
) {
124
123
return true
125
124
}
@@ -131,18 +130,17 @@ func validationErrorMatch(
131
130
// validationErrorSchemaPointerMatch matches the JSON schema pointer related to the validation failure against a regular expression.
132
131
func validationErrorSchemaPointerMatch (
133
132
schemaPointerRegexp * regexp.Regexp ,
134
- validationError * jsonschema.ValidationError ,
135
- schemasPath * paths.Path ,
133
+ validationError ValidationResult ,
136
134
) string {
137
- logrus .Tracef ("Checking schema pointer: %s match with regexp: %s" , validationError .SchemaPtr , schemaPointerRegexp )
138
- if schemaPointerRegexp .MatchString (validationError .SchemaPtr ) {
139
- return validationError .SchemaPtr
135
+ logrus .Tracef ("Checking schema pointer: %s match with regexp: %s" , validationError .Result . SchemaPtr , schemaPointerRegexp )
136
+ if schemaPointerRegexp .MatchString (validationError .Result . SchemaPtr ) {
137
+ return validationError .Result . SchemaPtr
140
138
}
141
139
142
140
// The schema validator does not provide full pointer past logic inversion keywords to the lowest level keywords related to the validation error cause.
143
141
// Therefore, the sub-keywords must be checked for matches in order to be able to interpret the exact cause of the failure.
144
- if regexp .MustCompile ("(/not)|(/oneOf)$" ).MatchString (validationError .SchemaPtr ) {
145
- return validationErrorSchemaSubPointerMatch (schemaPointerRegexp , validationError .SchemaPtr , validationErrorSchemaPointerValue (validationError , schemasPath ))
142
+ if regexp .MustCompile ("(/not)|(/oneOf)$" ).MatchString (validationError .Result . SchemaPtr ) {
143
+ return validationErrorSchemaSubPointerMatch (schemaPointerRegexp , validationError .Result . SchemaPtr , validationErrorSchemaPointerValue (validationError ))
146
144
}
147
145
148
146
return ""
@@ -184,11 +182,10 @@ func validationErrorSchemaSubPointerMatch(schemaPointerRegexp *regexp.Regexp, pa
184
182
// it matches against the given regular expression.
185
183
func validationErrorSchemaPointerValueMatch (
186
184
schemaPointerValueRegexp * regexp.Regexp ,
187
- schemaURL ,
185
+ validationError ValidationResult ,
188
186
schemaPointer string ,
189
- schemasPath * paths.Path ,
190
187
) bool {
191
- marshalledSchemaPointerValue , err := json .Marshal (schemaPointerValue (schemaURL , schemaPointer , schemasPath ))
188
+ marshalledSchemaPointerValue , err := json .Marshal (schemaPointerValue (validationError . Result . SchemaURL , schemaPointer , validationError . dataLoader ))
192
189
if err != nil {
193
190
panic (err )
194
191
}
0 commit comments