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

     1  package jsonschema
     2  
     3  // EvaluateMinProperties checks if the number of properties in the object meets or exceeds the specified minimum.
     4  // According to the JSON Schema Draft 2020-12:
     5  //   - The "minProperties" keyword must be a non-negative integer.
     6  //   - An object instance is valid against "minProperties" if its number of properties is greater than or equal to the value of this keyword.
     7  //   - Omitting this keyword has the same behavior as a value of 0.
     8  //
     9  // This method ensures that the object instance conforms to the property count constraints defined in the schema.
    10  // If the instance has fewer properties than the minimum required, it returns a EvaluationError detailing the expected minimum and the actual count.
    11  //
    12  // Reference: https://json-schema.org/draft/2020-12/json-schema-validation#name-minProperties
    13  func evaluateMinProperties(schema *Schema, object map[string]interface{}) *EvaluationError {
    14  	minProperties := float64(0) // Default value if minProperties is omitted
    15  	if schema.MinProperties != nil {
    16  		minProperties = *schema.MinProperties
    17  	}
    18  
    19  	actualCount := float64(len(object))
    20  	if actualCount < minProperties {
    21  		return NewEvaluationError("minProperties", "too_few_properties", "Value should have at least {min_properties} properties", map[string]interface{}{
    22  			"min_properties": minProperties,
    23  		})
    24  	}
    25  
    26  	return nil
    27  }