validation - How to validate string and number using json schema -


i validate schema based on either maximum/minimum (number) or maximumlength/minimumlength (string). have json form:

[   {     "key":"foo",       "title":"test",     "type":"string"    } ] 

and json schema:

    {  "type": "object",   "properties": {     "foo": {         "type": ["number","string"],         "maxlength":2,         "minlength":0,         "minimum":3,         "maximum":10     }   } } 

and json model:

{   "foo": "bar" } 

why this example not work validation? model have not validated false. according this document possible have different types defined in array, how can validation based on min/max values?

your schema correct. validator using doesn't work properly. here alternative uses anyof instead.

{     "type": "object",     "properties": {         "foo": {             "anyof": [                 { "$ref": "#/definitions/boundednumber" }                 { "$ref": "#/definitions/boundedstring" }             ]         }     },     "definitions": {         "boundedstring": {             "type": "string",             "maxlength": 2,             "minlength": 0         },         "boundednumber": {             "type": "number",             "minimum": 3,             "maximum": 10         }     } } 

although quite bit longer, argue easier read/maintain because of separation of type specific keywords.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -