Hi,
The pattern is not working for number type. I have scenario like allow only two decimals. I added the pattern in the schema. But its not working as expected. Do you have any workaround or solution for this issue?
[original thread by Nanda]
Hi,
The pattern is not working for number type. I have scenario like allow only two decimals. I added the pattern in the schema. But its not working as expected. Do you have any workaround or solution for this issue?
[original thread by Nanda]
Hi @nandhu(nandhu), pattern
is a keyword for strings so it will not work for numbers. To test for two decimals you can simply use minimum
and maximum
, e.g.
type: 'integer',
minimum: 10,
maximum: 99
[Nanda]
Hi @sdirix(sdirix) , thanks for the quick response. I wants to allow only two decimal places. for example 102534.76, 365.1 and 5345 are valid numbers. If I enter 4354.63452 then it should throw the validation error.
[Nanda]
Hi,
Is there any option or workaround to achieve this?
Thanks.
As far as I’m aware of there is no mechanism in JSON Schema to specify this constraint for numeric instances. The quick and dirty solution is to use string
instead and then validate using pattern
while converting it to a number
afterwards.
To keep the type number
you will need to customize Ajv. Using a custom validator you can also check for the fractional part of the value.