Saturday, July 23, 2022

[FIXED] How to constrain a JSON field by a few possible values?

Issue

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

}

Solution

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

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

See also: Understanding JSON Schema



Answered By - Clemens
Answer Checked By - Pedro (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.