Search code examples
jsonjsonschema

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 ?


Solution

  • 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.