Mule4验证模块:JSON数组首个对象报错后停止验证咨询
Mule4 Validation模块JSON Schema验证数组时仅报告首个错误的解决办法
我正在使用Mule4 Validation模块对JSON请求体进行JSON Schema验证,发现当检测到数组中第一个对象的数据类型错误后,验证就不再继续执行了。现附上相关信息,恳请各位提供解决思路,谢谢!
输入JSON
[ {"a": 112, "b": "123"}, {"a": 123, "b": ""} ]
JSON Schema
{ "$schema": "http://json-schema.org/draft-04/schema#", "type": "array", "items": { "type": "object", "properties": { "a": { "type": "string", "minLength": 1, "maxLength": 255 }, "b": { "type": "string", "minLength": 1, "maxLength": 255 } }, "required": ["a", "b"] } }
返回错误信息
[ { "level" : "error", "schema" : { "loadingURI" : "file:/C:/Users/Harshitkawdia/Softwares/AnypointStudio/plugins/org.mule.tooling.server.4.4.0.ee_7.11.0.202303211414/mule/apps/schema-poc/schema/schema.json#", "pointer" : "/items/properties/a" }, "instance" : { "pointer" : "/0/a" }, "domain" : "validation", "keyword" : "type", "message" : "instance type (integer) does not match any allowed primitive type (allowed: [\"string\"])", "found" : "integer", "expected" : [ "string" ] } ]
解决思路
Mule4 Validation模块默认采用**快速失败(fail-fast)**模式,遇到第一个验证错误就终止流程,不会继续检测后续内容。要获取所有错误,需调整验证配置:
- 可视化配置(Anypoint Studio):在
Validate Schema组件的配置面板中,找到Fail Fast选项,将其设置为false(或勾选Collect All Errors,具体选项名依Mule版本略有差异)。 - XML配置:在
<validation:validate-schema>标签中添加failFast="false"属性,示例如下:
<validation:validate-schema schemaLocation="classpath:schema/schema.json" failFast="false"> <validation:content><![CDATA[#[payload]]]></validation:content> </validation:validate-schema>
调整后,验证器会遍历整个JSON文档,收集所有不符合Schema的错误,包括你示例中第二个对象b字段的空字符串长度不符合要求的问题。
内容的提问来源于stack exchange,提问作者Harshit Kawdia
相关产品推荐
相关产品推荐

