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

     1  package jsonschema
     2  
     3  // EvaluateMaximum checks if the numeric data's value does not exceed the maximum value specified in the schema.
     4  // According to the JSON Schema Draft 2020-12:
     5  //   - The value of the "maximum" keyword must be a number, representing an inclusive upper limit for a numeric instance.
     6  //   - This keyword validates only if the instance is less than or exactly equal to "maximum".
     7  //
     8  // This method ensures that the numeric data instance conforms to the maximum constraints defined in the schema.
     9  // If the instance exceeds the maximum value, it returns a EvaluationError detailing the expected maximum and actual value.
    10  //
    11  // Reference: https://json-schema.org/draft/2020-12/json-schema-validation#name-maximum
    12  func evaluateMaximum(schema *Schema, value *Rat) *EvaluationError {
    13  	if schema.Maximum.Rat != nil {
    14  		if value.Cmp(schema.Maximum.Rat) > 0 {
    15  			// If the data value exceeds the maximum value, construct and return an error.
    16  			return NewEvaluationError("maximum", "value_above_maximum", "{value} should be at most {maximum}", map[string]interface{}{
    17  				"value":   FormatRat(value),
    18  				"maximum": FormatRat(schema.Maximum),
    19  			})
    20  		}
    21  	}
    22  	return nil
    23  }