Rounding errors in JSON Schema validation

JSON Schema includes the multipleOf keyword. This is very useful for checking that a number is rounded to a specific number of decimal places.

  "type": "number",
  "description": "An integer",
  "multipleOf": 1.0

Unfortunately, rounding errors in some languages mean acceptable data can fail validation.

If you’re using JavaScript, the Ajv library has a solution to this problem: the multipleOfPrecision option.

const ajv = new Ajv({ multipleOfPrecision: 2 })

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.