Lucas_Santos
Lucas_Santos

Reputation: 4740

JSON Schema validating a range of numbers for a integer property

I'm using TV4 to validate my schema, and I saw that this lib use Json Schema model to validate a JSON.

But I didn't found a way to do a specific validation.

I have a integer property and this property can only have one of some numbers. For example, the valid number for me is, 10, 20, 30 and 40, so if I put some number different than these numbers, I need to show a validation error.

Has some way to do this specific validation in JSON Schema ?

Upvotes: 1

Views: 5386

Answers (2)

AgilePro
AgilePro

Reputation: 5616

You should be able to specify this JSON schema expression:

{
    "type"       : "number",
    "multipleOf" : 10,
    "maximum"    : 40,
    "minimum"    : 10
}

Upvotes: 0

Relequestual
Relequestual

Reputation: 12355

I'm not sure if you want an enum or you want your integer values to be multiples of 10.

For enum, you have an array of the allowed values. For example:

{
  "type": "string",
  "enum": ["red", "amber", "green"]
}

source

If you want values to be only multiples of a number, you want multipleOf.

{
    "type"       : "number",
    "multipleOf" : 10
}

source

These links are for the draft-4 version of JSON Schema, as that's what the library you're using supports, however these key words are also present in the lates version of JSON Schema (draft-7 at the time of writing). You may consider using a differnet library which supports newer versions.

Upvotes: 3

Related Questions