Decimal precision in JSON Schema

22,399

Solution 1

They replaced it with multipleOf (via v3 divisibleBy).

For 2 decimal places, just add multipleOf: 0.01.

Solution 2

I would suggest { "type": "number", "multipleOf": 0.01 } rather than { "type": "integer", "multipleOf": 0.01 }.

See http://spacetelescope.github.io/understanding-json-schema/reference/numeric.html#multiples

Solution 3

Based on JSON Schema specification rfc.section.6.3:

{ 
  "type": "string", 
  "pattern": "-?^\\d*(.\\d{0,2})?$" 
}

Solution 4

If you use python jsonschema library than you need

{ "type": "number", "multipleOfPrecision": 0.01 }

https://github.com/epoberezkin/ajv#options

Share:
22,399
Jay Pete
Author by

Jay Pete

Updated on July 29, 2022

Comments

  • Jay Pete
    Jay Pete over 1 year

    I want to have my JSON Schema validate that no more than two decimal places are sent to my REST api.

    From what I can see in the latest JSON Schema RFC (v4) doesn't allow this. V1 had a maxDecimals validator.

    Does anyone know why that was taken out?

    I have a field that only holds two decimals when I store it in the database, and I do not just want to round down to two decimals. That would be changing the input quite dramatically for some users. So I want to reject any greater precision and force them to round them selves.

    I can of course do this using a custom validator that I write myself, but I would rather not unless I absolutely have to.

    Is there another way of indicating this in v4?

    Thanks