forked from kaptinlin/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anyOf.go
56 lines (49 loc) · 2.16 KB
/
anyOf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package jsonschema
import (
"fmt"
)
// EvaluateAnyOf checks if the data conforms to at least one of the schemas specified in the anyOf attribute.
// According to the JSON Schema Draft 2020-12:
// - The "anyOf" keyword's value must be a non-empty array, where each item is either a valid JSON Schema or a boolean.
// - An instance validates successfully against this keyword if it validates successfully against at least one schema or is true for any boolean in this array.
//
// This function ensures that the data instance meets at least one of the specified constraints defined by the schemas or booleans in the anyOf array.
// If the instance fails to conform to all conditions in the array, it returns a EvaluationError detailing the specific failures.
//
// Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-anyof
func evaluateAnyOf(schema *Schema, data interface{}, evaluatedProps map[string]bool, evaluatedItems map[int]bool, dynamicScope *DynamicScope) ([]*EvaluationResult, *EvaluationError) {
if len(schema.AnyOf) == 0 {
return nil, nil // No anyOf constraints to validate against.
}
var valid bool
results := []*EvaluationResult{}
for i, subSchema := range schema.AnyOf {
if subSchema != nil {
skipEval := false
if subSchema.Boolean != nil && *subSchema.Boolean {
// If the schema is `true`, skip updating evaluated properties and items.
skipEval = true
}
result, schemaEvaluatedProps, schemaEvaluatedItems := subSchema.evaluate(data, dynamicScope)
if result != nil {
results = append(results, result.SetEvaluationPath(fmt.Sprintf("/anyOf/%d", i)).
SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/anyOf/%d", i))).
SetInstanceLocation(""),
)
if result.IsValid() {
valid = true
// Merge maps only if the evaluation is successful
if !skipEval {
mergeStringMaps(evaluatedProps, schemaEvaluatedProps)
mergeIntMaps(evaluatedItems, schemaEvaluatedItems)
}
}
}
}
}
if valid {
return results, nil // Return nil only if at least one schema succeeds
} else {
return results, NewEvaluationError("anyOf", "any_of_item_mismatch", "Value does not match anyOf schema")
}
}