Skip to content

Commit

Permalink
Replace big.Float by big.Rat
Browse files Browse the repository at this point in the history
  • Loading branch information
johandorland committed Jan 8, 2019
1 parent cdd33d0 commit f971f3c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion schema.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit f971f3c

Please sign in to comment.