PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

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

 July 23, 2022     json, jsonschema, validation     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing