Java 日期\时间的Json架构验证不起作用
我的json模式具有以下结构 v0.jsonJava 日期\时间的Json架构验证不起作用,java,jsonschema,json-schema-validator,Java,Jsonschema,Json Schema Validator,我的json模式具有以下结构 v0.json "order_datetime": { "type": "string", "description": "The date and time when the order was placed", "format": "dat
"order_datetime": {
"type": "string",
"description": "The date and time when the order was placed",
"format": "date_time",
"examples": [
"2021-02-16T13:30:27.816Z"
]
}
而输入json包含了这些数据
"order_datetime": "2021"
这是我的模式验证器,但即使数据格式错误,验证还是成功的
@RequiredArgsConstructor
public class OrderJsonSchemaValidator {
public boolean validate(String message) throws JsonProcessingException {
Schema schema;
String schemaName = "v0.json";
try {
schema = loadSchema(schemaName);
} catch (IOException e) {
throw new RuntimeException("Property file " + schemaName + " not found in the classpath in resources messages directory");
}
try{
schema.validate(new JSONObject(message));
return true;
}catch (ValidationException e) {
System.out.println(e.getMessage());
e.getCausingExceptions().stream()
.map(ValidationException::getMessage)
.forEach(System.out::println);
}
return false;
}
public Schema loadSchema(String schemaPath) throws IOException {
Schema schema;
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(schemaPath)) {
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
schema = SchemaLoader.load(rawSchema);
}
return schema;
}
我不知道您使用的是哪个实现,但默认情况下,
format
不进行任何验证。在draft-07
和更早版本中,对格式的支持是可选的。在草稿2019-09
及以上版本中,默认情况下,它仅为注释
如果您希望格式
执行语义验证,则需要查看实现文档以获得支持。如果他们没有列出支持,请提出问题。如果您不能提交问题,可以深入研究代码
对于2019-09及以上版本的JSON模式草案,如果您希望您的模式要求互操作地使用格式,您需要创建一种新的方言,指定所需的适当词汇。它是“格式”:“日期时间”
,而不是“格式”:“日期时间”
,我正在使用“$Schema”:“太好了!什么实现/库呢?我通过重新调整json模式解决了这个问题。我最初将必填字段放在属性之前。只是在属性{}确定之后移动了所需的[]。我已经投票决定以“打字错误导致”结束这一问题,并且修复程序没有考虑到这个问题。