Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multipleOf by using arbritrary precision big.Rat instead of big.Float #224

Merged
merged 1 commit into from Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion schema.go
Expand Up @@ -453,7 +453,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema)
},
))
}
if multipleOfValue.Cmp(big.NewFloat(0)) <= 0 {
if multipleOfValue.Cmp(big.NewRat(0, 1)) <= 0 {
return errors.New(formatErrorDescription(
Locale.GreaterThanZero(),
ErrorDetails{"number": KEY_MULTIPLE_OF},
Expand Down
10 changes: 5 additions & 5 deletions subSchema.go
Expand Up @@ -103,11 +103,11 @@ type subSchema struct {
propertiesChildren []*subSchema

// validation : number / integer
multipleOf *big.Float
maximum *big.Float
exclusiveMaximum *big.Float
minimum *big.Float
exclusiveMinimum *big.Float
multipleOf *big.Rat
maximum *big.Rat
exclusiveMaximum *big.Rat
minimum *big.Rat
exclusiveMinimum *big.Rat

// validation : string
minLength *int
Expand Down
6 changes: 3 additions & 3 deletions utils.go
Expand Up @@ -120,7 +120,7 @@ func checkJsonInteger(what interface{}) (isInt bool) {

jsonNumber := what.(json.Number)

bigFloat, isValidNumber := new(big.Float).SetString(string(jsonNumber))
bigFloat, isValidNumber := new(big.Rat).SetString(string(jsonNumber))

return isValidNumber && bigFloat.IsInt()

Expand Down Expand Up @@ -168,11 +168,11 @@ func mustBeInteger(what interface{}) *int {
return nil
}

func mustBeNumber(what interface{}) *big.Float {
func mustBeNumber(what interface{}) *big.Rat {

if isJsonNumber(what) {
number := what.(json.Number)
float64Value, success := new(big.Float).SetString(string(number))
float64Value, success := new(big.Rat).SetString(string(number))
if success {
return float64Value
} else {
Expand Down
7 changes: 3 additions & 4 deletions validation.go
Expand Up @@ -842,17 +842,16 @@ func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{
}

number := value.(json.Number)
float64Value, _ := new(big.Float).SetString(string(number))
float64Value, _ := new(big.Rat).SetString(string(number))

// multipleOf:
if currentSubSchema.multipleOf != nil {

if q := new(big.Float).Quo(float64Value, currentSubSchema.multipleOf); !q.IsInt() {
if q := new(big.Rat).Quo(float64Value, currentSubSchema.multipleOf); !q.IsInt() {
result.addInternalError(
new(MultipleOfError),
context,
resultErrorFormatJsonNumber(number),
ErrorDetails{"multiple": currentSubSchema.multipleOf},
ErrorDetails{"multiple": new(big.Float).SetRat(currentSubSchema.multipleOf)},
)
}
}
Expand Down