java 如何在json-schema-validator-1.0.68.jar中获取自定义模式草稿?

wvyml7n5  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(111)

我在我的json-schema中定义了我的自定义模式草案。

  1. { "$schema": "http://oag.company.com/ver1/schema#",
  2. "title": "JSON Identity Attribute Schema"
  3. }

字符串
并希望在代码验证中获取它。我使用com.networknt附带的 json-schema-validator-1.0.68.jar。我的代码

  1. public static void main(String[] args) throws Exception {
  2. ObjectMapper objectMapper = new ObjectMapper();
  3. JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance();
  4. String exppath="exp.json";
  5. String schpath ="schm.json";
  6. try (
  7. InputStream jsonStream = new FileInputStream(exppath);
  8. InputStream schemaStream = new FileInputStream(schpath);
  9. ) {
  10. JsonNode json = objectMapper.readTree(jsonStream);
  11. com.networknt.schema.JsonSchema schema = schemaFactory.getSchema(schemaStream);
  12. Set<ValidationMessage> validationResult = schema.validate(json);
  13. if (validationResult.isEmpty()) {
  14. System.out.println("no validation errors :-)");
  15. System.out.println(json.path("FirstName"));
  16. } else {
  17. validationResult.forEach(vm -> System.out.println(vm));
  18. }
  19. }
  20. }


我得到的错误消息是
线程“main”中出现异常com.networknt.schema.JsonSchemaException:未知MetaSchema:https://oag.oracle.com/ver1/schema
是thier任何方式,所以我可以使用我的自定义模式或我可以使用任何其他库,允许自定义模式?

qybjjes1

qybjjes11#

“$schema”是IETF标准的元版本,您需要将您的引用为“$ref”。

  1. {
  2. "$schema": "http://json-schema.org/draft-{version}/schema#",
  3. "$ref": "http://oag.company.com/ver1/schema#"
  4. }

字符串

相关问题