19

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

Jay Pete
  • 4,123
  • 4
  • 35
  • 51

4 Answers4

24

They replaced it with multipleOf (via v3 divisibleBy).

For 2 decimal places, just add multipleOf: 0.01.

fiddur
  • 1,768
  • 15
  • 11
  • Hmmm... then integers aren't accepted. Integer 5 is not evenly divisible by 0.01. – Jay Pete Dec 03 '14 at 21:20
  • Then you might be using a faulty json-schema validator. Which validator did you use, and did you go with v3 divisibleBy or v4 multipleOf? – fiddur Dec 04 '14 at 08:33
  • 1
    Try this on http://json-schema-validator.herokuapp.com/ Schema: { "type": "integer", "multipleOf": 0.01 } Data: 5 – fiddur Dec 04 '14 at 08:40
  • 1
    I use Newtonsoft.Json for .NET, It only supports v3. And the schema that you have given in your example should be { "type": "number", "multipleOf": 0.01 } to also allow 5.12. When I read the documentation and unit tests for NewtonSoft, it behaves as intended. – Jay Pete Dec 04 '14 at 15:47
  • 4
    Due to floating point imprecision, this is not reliable, e.g. 147.41 will pass validation, but 147.42 will not https://github.com/geraintluff/tv4/issues/158 – Dónal Nov 30 '16 at 10:16
12

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

Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
cristian fusi
  • 121
  • 1
  • 2
2

Based on JSON Schema specification rfc.section.6.3:

{ 
  "type": "string", 
  "pattern": "-?^\\d*(.\\d{0,2})?$" 
}
PranasB
  • 71
  • 4
  • 1
    I think that your regex would allow `42.` as value: the capturing group allows zero decimal places after the dot. I'd think that, when the (optional) group is given, at least one decimal place should follow the dot, so I'd go for `-?^\\d*(.\\d{1,2})?$`. – fxnn Jan 13 '21 at 05:25
1

If you use python jsonschema library than you need

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

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

Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88
  • `multipleOfPrecision` is not valid to use, it does not exist in the JSON Schema specification. Refer to this [GitHub Issue](https://github.com/python-jsonschema/jsonschema/issues/1039) for more information – Daniel Anner Jan 30 '23 at 21:47