如何强制 fge json-schema 验证器对未知关键字抛出错误?
How to force fge json-schema validator to throw an error on unknown keywords?
我希望我的应用程序接收 Json 架构,然后验证某些 JSON 对象是否符合该架构。我的问题是,如果我提供 false Json-schema,我不会收到异常,而只会在日志中收到一些警告。 所以我要么被抛出异常,要么以某种方式考虑警告。但是,警告日志对我来说似乎相当沉默。
我正在使用 java 并且该库是 json-schema
的标准库
String json = "{\"a\":\"b\"};
JsonNode schema = new ObjectMapper().readTree(json);
JsonSchemaFactory.byDefault().getSyntaxValidator();
// this returns true but I want it to return false, since "a" is not a valid keyword
validator.schemaIsValid(schema);
// this returns a waning --> the following keywords are unknown and will be ignored: [a]
System.out.println(validator.validateSchema(schema));
JSON 架构 ("the schema of schemas") 的元架构允许附加属性,因此 fge 验证器也允许,并且没有理由为什么实施会更改此限制。
您可以做的一件事是下载元模式,将其更改为 "additionalProperties": false
,然后使用验证器加载此更改后的元模式并使用它验证您自己的模式。
draft4 元模式在这里:http://json-schema.org/draft-04/schema
我能够更改 FGE 验证器所需的错误阈值,以便在提供未知属性时将验证视为不成功。以下代码完成工作
void validateSchema(JsonNode schemaNode) throws JsonProcessingException {
SyntaxValidator validator = JsonSchemaFactory.byDefault().getSyntaxValidator();
ProcessingReport report = new ListProcessingReport(null, LogLevel.INFO);
ValueHolder<SchemaTree> holder = ValueHolder.<SchemaTree>hold("schema",
new CanonicalSchemaTree(SchemaKey.anonymousKey(), schemaNode));
Processor<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>> processor =
validator.getProcessor();
report = ProcessingResult.uncheckedResult(processor, report, holder).getReport();
if (!report.isSuccess()) {
IllegalArgumentException ex = new IllegalArgumentException();
for (ProcessingMessage processingMessage : report) {
ex.addSuppressed(processingMessage.asException());
}
throw ex;
}
}
好方法@erosb,@Llvanov。作为替代方案,我能够通过将 'additionalProperties' 属性直接注入架构节点来让解析器验证未知属性。
((ObjectNode) schemaNode).put(
"additionalProperties",
false);
我希望我的应用程序接收 Json 架构,然后验证某些 JSON 对象是否符合该架构。我的问题是,如果我提供 false Json-schema,我不会收到异常,而只会在日志中收到一些警告。 所以我要么被抛出异常,要么以某种方式考虑警告。但是,警告日志对我来说似乎相当沉默。
我正在使用 java 并且该库是 json-schema
的标准库String json = "{\"a\":\"b\"};
JsonNode schema = new ObjectMapper().readTree(json);
JsonSchemaFactory.byDefault().getSyntaxValidator();
// this returns true but I want it to return false, since "a" is not a valid keyword
validator.schemaIsValid(schema);
// this returns a waning --> the following keywords are unknown and will be ignored: [a]
System.out.println(validator.validateSchema(schema));
JSON 架构 ("the schema of schemas") 的元架构允许附加属性,因此 fge 验证器也允许,并且没有理由为什么实施会更改此限制。
您可以做的一件事是下载元模式,将其更改为 "additionalProperties": false
,然后使用验证器加载此更改后的元模式并使用它验证您自己的模式。
draft4 元模式在这里:http://json-schema.org/draft-04/schema
我能够更改 FGE 验证器所需的错误阈值,以便在提供未知属性时将验证视为不成功。以下代码完成工作
void validateSchema(JsonNode schemaNode) throws JsonProcessingException {
SyntaxValidator validator = JsonSchemaFactory.byDefault().getSyntaxValidator();
ProcessingReport report = new ListProcessingReport(null, LogLevel.INFO);
ValueHolder<SchemaTree> holder = ValueHolder.<SchemaTree>hold("schema",
new CanonicalSchemaTree(SchemaKey.anonymousKey(), schemaNode));
Processor<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>> processor =
validator.getProcessor();
report = ProcessingResult.uncheckedResult(processor, report, holder).getReport();
if (!report.isSuccess()) {
IllegalArgumentException ex = new IllegalArgumentException();
for (ProcessingMessage processingMessage : report) {
ex.addSuppressed(processingMessage.asException());
}
throw ex;
}
}
好方法@erosb,@Llvanov。作为替代方案,我能够通过将 'additionalProperties' 属性直接注入架构节点来让解析器验证未知属性。
((ObjectNode) schemaNode).put(
"additionalProperties",
false);