JSON Schema校验失效求助:缺失属性未触发报错
解决JSON Schema校验失效问题
兄弟我懂你今天诸事不顺的心情,咱们直接来搞定这个JSON Schema的问题!你遇到的核心问题是:移除column或masking_type字段时Schema没触发报错,这是因为你在定义数组元素结构时用错了关键字,咱们一步步来修正。
问题分析
你的原Schema里,patternProperties下的数组配置直接写了properties和required,但这两个关键字是用来定义对象结构的,而数组的元素规则必须通过items关键字来声明。所以原来的校验规则根本没作用到数组里的每个对象上,自然不会报错。
修正后的JSON Schema
{ "title": "Schema title", "description": "Description of the schema", "type": "object", "properties": { "tables_not_to_mask": { "type": "array", "minItems": 0, "items": {"type": "string"} }, "tables_to_mask": { "type": "object", "patternProperties": { ".*": { "type": "array", "minItems": 0, "items": { "type": "object", "properties": { "column": {"type": "string"}, "masking_type": {"type": "string"} }, "required": ["column", "masking_type"] } } } } }, "required": ["tables_not_to_mask", "tables_to_mask"] }
关键修正点
- 把原来直接放在数组配置下的
properties和required,移到items对象内部:这样就明确指定了数组中的每个对象必须包含column和masking_type两个必填字段 - 给
items加上"type": "object",明确数组元素是对象类型
现在你再测试移除column或masking_type的场景,Schema就会正确触发校验报错了,同时完全满足你的所有需求:
tables_not_to_mask和tables_to_mask为必填字段tables_to_mask可包含0个或多个动态表名- 每个表下的字段配置数组可包含0个或多个对象,只要存在对象就必须同时包含
column和masking_type
内容的提问来源于stack exchange,提问作者Hari
相关产品推荐
相关产品推荐

