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

     1  package jsonschema
     2  
     3  // EvaluateMinItems checks if the array data contains at least the minimum number of items specified in the "minItems" schema attribute.
     4  // According to the JSON Schema Draft 2020-12:
     5  //   - The value of "minItems" must be a non-negative integer.
     6  //   - An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword.
     7  //   - Omitting this keyword has the same behavior as a value of 0, which means no minimum size constraint unless explicitly specified.
     8  //
     9  // This method ensures that the array data instance conforms to the minimum items constraints defined in the schema.
    10  // If the instance violates this constraint, it returns a EvaluationError detailing the required minimum and the actual size.
    11  //
    12  // Reference: https://json-schema.org/draft/2020-12/json-schema-validation#name-minitems
    13  func evaluateMinItems(schema *Schema, array []interface{}) *EvaluationError {
    14  	if schema.MinItems != nil {
    15  		if float64(len(array)) < *schema.MinItems {
    16  			// If the array size is less than the minimum required, construct and return an error.
    17  			return NewEvaluationError("minItems", "items_too_short", "Value should have at least {min_items} items", map[string]interface{}{
    18  				"min_items": *schema.MinItems,
    19  				"count":     len(array),
    20  			})
    21  		}
    22  	}
    23  	return nil
    24  }