Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to specify min/max length of a string in JSON schema (v4)?

Tags:

json

schema

I'm a non-developer and new to JSON and have created a JSON schema to validate JSON code when troubleshooting customer issues to insure syntactics against our defined data elements. I noticed that I can only verify min/maxLength if the field type is an integer. If the type=string, min/maxLength does not work.

JSON Schema code snippet:

"LastName": {
    "type" : "string",
    "optional": false,
    "minLength": 1, 
    "maxLength": 254,
    "description": "Last Name of Insured"
},

In JSON code, if "LastName": "" it still validates, but our WS/JSON call will fail because this field is required and requires data.

Thanks in advance!

like image 680
Neil DeLuca Avatar asked Dec 07 '17 16:12

Neil DeLuca


1 Answers

It should work per the spec. Reference and quote below. Please elaborate "If the type=string, min/maxLength does not work." - what unexpected exacting behavior do you get?

https://json-schema.org/understanding-json-schema/reference/string.html

Length

The length of a string can be constrained using the minLength and maxLength keywords. For both keywords, the value must be a non-negative number.

{
  "type": "string",
  "minLength": 2,
  "maxLength": 3
}
like image 174
David Burg Avatar answered Oct 10 '22 22:10

David Burg