Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question

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.

share|improve this answer
    
Thank you, my expression were applicable for draft3, my validator is for draft4 and my data is php $_POST array, there is all strings, i think, i should use "string" type with pattern. – vitus Mar 26 '15 at 10:40
    
@vitus i think i don't understand you... did it work for you? – stefankmitph Mar 26 '15 at 11:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.