github.com/kaptinlin/jsonschema@v0.4.6/multipleOf.go (about) 1 package jsonschema 2 3 import "math/big" 4 5 // EvaluateMultipleOf checks if the numeric data is a multiple of the value specified in the "multipleOf" schema attribute. 6 // According to the JSON Schema Draft 2020-12: 7 // - The value of "multipleOf" must be a number, strictly greater than 0. 8 // - A numeric instance is valid only if division by this keyword's value results in an integer. 9 // 10 // This method ensures that the numeric data instance conforms to the divisibility constraints defined in the schema. 11 // If the instance does not conform, it returns a EvaluationError detailing the expected divisor and the actual remainder. 12 // 13 // Reference: https://json-schema.org/draft/2020-12/json-schema-validation#name-multipleof 14 func evaluateMultipleOf(schema *Schema, value *Rat) *EvaluationError { 15 if schema.MultipleOf != nil { 16 if schema.MultipleOf.Sign() == 0 || schema.MultipleOf.Sign() < 0 { 17 // If the divisor is 0, return an error. 18 return NewEvaluationError("multipleOf", "invalid_multiple_of", "Multiple of {multiple_of} should be greater than 0", map[string]interface{}{ 19 "divisor": FormatRat(schema.MultipleOf), 20 }) 21 } 22 23 // Calculate the division result to check if it's an integer. 24 resultRat := new(big.Rat).Quo(value.Rat, schema.MultipleOf.Rat) 25 if !resultRat.IsInt() { 26 // If the division result is not an integer, construct and return an error. 27 return NewEvaluationError("multipleOf", "not_multiple_of", "{value} should be a multiple of {multiple_of}", map[string]interface{}{ 28 "divisor": FormatRat(schema.MultipleOf), 29 "value": FormatRat(value), 30 }) 31 } 32 } 33 return nil 34 }