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

wvyml7n5  于 7个月前  发布在  Java
关注(0)|答案(1)|浏览(53)

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

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

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

public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance();
        String exppath="exp.json";
        String schpath ="schm.json";
        try ( 
                InputStream jsonStream = new FileInputStream(exppath);
                InputStream schemaStream = new FileInputStream(schpath);
        ) {
            JsonNode json = objectMapper.readTree(jsonStream);
            com.networknt.schema.JsonSchema schema = schemaFactory.getSchema(schemaStream);
            Set<ValidationMessage> validationResult = schema.validate(json);
          
            if (validationResult.isEmpty()) {
                
                System.out.println("no validation errors :-)");
                System.out.println(json.path("FirstName"));
            } else {
                validationResult.forEach(vm -> System.out.println(vm));
            }
        }
    }


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

qybjjes1

qybjjes11#

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

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

字符串

相关问题