github.com/kaptinlin/jsonschema@v0.4.6/maxProperties.go (about)

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