Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON模式:如何允许数值类型的属性为空字符串?_Php_Json_Jsonschema - Fatal编程技术网

Php JSON模式:如何允许数值类型的属性为空字符串?

Php JSON模式:如何允许数值类型的属性为空字符串?,php,json,jsonschema,Php,Json,Jsonschema,在属性定义中,我需要允许数值或空字符串值,此表达式是否适用于此目的 "tprice":{"type":["number",{"enum":[""]}]} 用于验证数据的库(Jsv4)为空字符串生成错误: Invalid type: string 当我尝试为此属性设置零长度字符串时。我认为解决方案是在模式中使用anyOf。 这是适合您的模式: { "$schema": "http://json-schema.org/draft-04/schema#", "properties": {

在属性定义中,我需要允许数值或空字符串值,此表达式是否适用于此目的

"tprice":{"type":["number",{"enum":[""]}]}
用于验证数据的库(Jsv4)为空字符串生成错误:

Invalid type: string

当我尝试为此属性设置零长度字符串时。

我认为解决方案是在模式中使用
anyOf
。 这是适合您的模式:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "properties": {
      "tprice": {
          "anyOf": [ 
          { 
              "type": "number"
          }, 
          { 
              "type": "string", 
              "maxLength": 0
          } 
       ] 
     }
  }
}
我曾经测试过它

{
   "tprice": 123
}


验证很好。

谢谢,我的表达式适用于draft3,我的验证器适用于draft4,我的数据是php$\u POST数组,有所有字符串,我想,我应该使用带模式的“字符串”类型。@vitus我想我不明白你的意思。。。对你有用吗?
{
   "tprice": ""
}