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": ""
}