-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathadditional_properties.go
More file actions
82 lines (74 loc) · 2.95 KB
/
Copy pathadditional_properties.go
File metadata and controls
82 lines (74 loc) · 2.95 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package jsonschema
import (
"fmt"
"strings"
)
// evaluateAdditionalProperties checks if properties not explicitly defined or matched by patternProperties conform to the schema specified in additionalProperties.
// According to the JSON Schema Draft 2020-12:
// - The value of "additionalProperties" must be a valid JSON Schema.
// - This keyword validates child values of instance names that do not appear in the annotation results of either "properties" or "patternProperties".
// - Validation succeeds for these properties if the child instance validates against the "additionalProperties" schema.
// - Omitting "additionalProperties" has the same assertion behavior as an empty schema, which allows any type of value.
//
// This function ensures that all properties not explicitly mentioned or matched are validated according to a default schema or constraints.
//
// Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-additionalproperties
func evaluateAdditionalProperties(
schema *Schema, object map[string]any, evaluatedProps map[string]bool,
_ map[int]bool, dynamicScope *DynamicScope,
) ([]*EvaluationResult, *EvaluationError) {
var results []*EvaluationResult
var invalidProperties []string
properties := make(map[string]bool)
if schema.Properties != nil {
for propName := range *schema.Properties {
properties[propName] = true
}
}
if schema.PatternProperties != nil {
for _, regex := range schema.compiledPatterns {
for propName := range object {
if regex.MatchString(propName) {
properties[propName] = true
}
}
}
}
if schema.AdditionalProperties != nil {
for propName, propValue := range object {
if !properties[propName] {
result, _, _ := schema.AdditionalProperties.evaluate(propValue, dynamicScope)
if result != nil {
result.SetEvaluationPath(fmt.Sprintf("/additionalProperties/%s", propName)).
SetSchemaLocation(schema.SchemaLocation(fmt.Sprintf("/additionalProperties/%s", propName))).
SetInstanceLocation(fmt.Sprintf("/%s", propName))
results = append(results, result)
if !result.IsValid() {
invalidProperties = append(invalidProperties, propName)
}
}
// Mark property as evaluated
evaluatedProps[propName] = true
}
}
}
if len(invalidProperties) == 1 {
return results, NewEvaluationError(
"additionalProperties", "additional_property_mismatch",
"Additional property {property} does not match the schema",
map[string]any{"property": fmt.Sprintf("'%s'", invalidProperties[0])},
)
}
if len(invalidProperties) > 1 {
quotedProperties := make([]string, len(invalidProperties))
for i, prop := range invalidProperties {
quotedProperties[i] = fmt.Sprintf("'%s'", prop)
}
return results, NewEvaluationError(
"additionalProperties", "additional_properties_mismatch",
"Additional properties {properties} do not match the schema",
map[string]any{"properties": strings.Join(quotedProperties, ", ")},
)
}
return results, nil
}