Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON schema: how to allow empty string for property with numeric type?

In the property definition i need to allow numeric or empty string value, is this expression right for this purpose?

"tprice":{"type":["number",{"enum":[""]}]}

Library, that i use to validate data (Jsv4) generates error for empty string:

Invalid type: string

while i try to set zero length string for this property.

like image 780
vitus Avatar asked Mar 25 '15 10:03

vitus


1 Answers

I think the solution for you is the use of anyOf in the schema. This is the schema that works for you:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "properties": {
      "tprice": {
          "anyOf": [ 
          { 
              "type": "number"
          }, 
          { 
              "type": "string", 
              "maxLength": 0
          } 
       ] 
     }
  }
}

I've used jsonschemalint.com to test it.

{
   "tprice": 123
}

and

{
   "tprice": ""
}

validates just fine.

like image 171
stefankmitph Avatar answered Nov 14 '22 21:11

stefankmitph