我希望将自定义错误消息添加到我的JSON模式上,在"allOf"中进行错误验证。我有这个工作(在"not"):
"not": {
"anyOf": [
{
"required": ["prop1"],
"errorMessage": "Property 'prop1' is not allowed"
},
{
"required": ["prop2"],
"errorMessage": "Property 'prop2' is not allowed"
}
]
}
我得到:"[$.content[0] should not be valid to the schema...<standard error message>..., Property 'prop1' is not allowed.
太好了。
我正在尝试验证数组的最大条目:
"if": {
"properties": {
"required": [
"x"
]
}
},
"then": {
properties": {
"actions": {
"maxItems": 2,
"errorMessage": "you can't have more than 3 items in the array if x is true."
}
}
},
"else": {
"properties": {
"actions": {
"maxItems": 3,
"errorMessage": "you can't have more than 2 items in the array if x is false."
}
}
}
验证失败时得到的消息:
[$.content[0].actions: there must be a maximum of 3 items in the array]
和
[$.content[0].actions: there must be a maximum of 2 items in the array]
.
我不希望做说的验证,这只是一个例子。我希望错误信息还包括:you can't have more than 2 items in the array if x is false.
。这可能吗?我尝试了几种不同的东西,似乎只有当"not"
像在第一个代码块一样参与时才有可能。这似乎可以在对象声明的实际定义中完成,但我需要在"allOf"中完成。
相关POM版本:
<json-schema-validator.version>1.0.52</json-schema-validator.version>
<jsonschema2pojo.version>1.0.2</jsonschema2pojo.version>
TIA !
从文档来看,这个实现支持包含每个关键字的消息的message
关键字。不过,它并没有给出多少。我不知道它是否支持模板。
{
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"foo": {
"type": "array",
"maxItems": 3
}
},
"message": { // this is the bit you want
"maxItems" : "MaxItem must be 3 only",
"type" : "Invalid type"
}
}
我设法得到了一个(黑客)的解决方案。如果你加上2个"不"你可以使用"errorMessage"参数和1 not将否定另一个:
"properties": {
"actions": {
"not": {
"not": {
"maxItems": 3,
"errorMessage": "You cannot have more than 3 items in the actions object for Line messaging."
}
}
}
}