-
Notifications
You must be signed in to change notification settings - Fork 8
/
maxProperties.go
23 lines (21 loc) · 1.15 KB
/
maxProperties.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package jsonschema
// EvaluateMaxProperties checks if the number of properties in the object does not exceed the specified maximum.
// According to the JSON Schema Draft 2020-12:
// - The "maxProperties" keyword must be a non-negative integer.
// - An object instance is valid against "maxProperties" if its number of properties is less than, or equal to, the value of this keyword.
//
// This method ensures that the object instance conforms to the property count constraints defined in the schema.
// If the instance exceeds the maximum number of properties, it returns a EvaluationError detailing the expected maximum and the actual count.
//
// Reference: https://json-schema.org/draft/2020-12/json-schema-validation#name-maxProperties
func evaluateMaxProperties(schema *Schema, object map[string]interface{}) *EvaluationError {
if schema.MaxProperties != nil {
actualCount := float64(len(object))
if actualCount > *schema.MaxProperties {
return NewEvaluationError("maxProperties", "too_many_properties", "Value should have at most {max_properties} properties", map[string]interface{}{
"max_properties": *schema.MaxProperties,
})
}
}
return nil
}