Home > front end >  How to constrain a JSON field by a few possible values?
How to constrain a JSON field by a few possible values?

Time:07-22

I have a field "type1" that can only have values of "1", "7" or "9". What should I add to my schema so that the requirement of only these 3 values is fullfilled? The "type1" has this structure right now:

"type1": {
    "type": String, 
    "pattern": "^[0-9] $",
    "maxLength": 1

}

CodePudding user response:

Define it as enum to make it more simple and easier to read:

"type1": { "enum": [ "1", "7", "9" ] }

See also: Understanding JSON Schema

  • Related