gpt4 book ai didi

java - com.networknt json-schema-validator 不检查 json 中的未知关键字

转载 作者:行者123 更新时间:2023-12-01 16:28:18 26 4
gpt4 key购买 nike

我正在使用 json-schema-validator 来验证请求。它正在验证良好。

但是,Request 包含一些无效对象,它不会抛出任何错误。

架构

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "integer"
},
"name": {
"description": "Name of the product",
"type": "string"
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
"required": ["id", "name", "price"]
}

JSON

{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"],
"errorKey":"Invalid JSON"

}

“errorKey”是架构中的未知关键字,但是,此 json-schema-validator 不会引发任何错误。

有什么方法可以验证这一点吗?

依赖关系

<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.40</version>
</dependency>

最佳答案

答案是“additionalProperties”:应在架构中添加 false。

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "integer"
},
"name": {
"description": "Name of the product",
"type": "string"
},
"price": {
"type": "number",
"minimum": 0
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
"required": ["id", "name", "price"],
"additionalProperties":false
}

示例代码:

public class App {

public static void main(String arg[]) throws JsonMappingException, JsonProcessingException {

System.out.println("Test in progress");
JsonSchemaFactory jsonSchemaFacory = JsonSchemaFactory.getInstance(VersionFlag.V7);
String schema = "{\r\n" +
" \"$schema\": \"http://json-schema.org/draft-07/schema#\",\r\n" +
" \"title\": \"Product\",\r\n" +
" \"description\": \"A product from Acme's catalog\",\r\n" +
" \"type\": \"object\",\r\n" +
" \"properties\": {\r\n" +
" \"id\": {\r\n" +
" \"description\": \"The unique identifier for a product\",\r\n" +
" \"type\": \"integer\"\r\n" +
" },\r\n" +
" \"name\": {\r\n" +
" \"description\": \"Name of the product\",\r\n" +
" \"type\": \"string\"\r\n" +
" },\r\n" +
" \"price\": {\r\n" +
" \"type\": \"number\",\r\n" +
" \"minimum\": 0\r\n" +
" },\r\n" +
" \"tags\": {\r\n" +
" \"type\": \"array\",\r\n" +
" \"items\": {\r\n" +
" \"type\": \"string\"\r\n" +
" },\r\n" +
" \"minItems\": 1,\r\n" +
" \"uniqueItems\": true\r\n" +
" }\r\n" +
" },\r\n" +
" \"required\": [\"id\", \"name\", \"price\"],\r\n" +
" \"additionalProperties\":false\r\n" +
" \r\n" +
"} ";

String value = "{\r\n" + " \"id\": 1,\r\n" + " \"name\": \"A green door\",\r\n"
+ " \"price\": 12.50,\r\n" + " \"tags\": [\"home\", \"green\"],\r\n"
+ " \"errorKey\":\"Invalid JSON\"\r\n" + "\r\n" + "}";



ObjectMapper objectMapper = new ObjectMapper();

JsonNode schemaNode = objectMapper.readTree(schema);
JsonNode validationFor = objectMapper.readTree(value);
JsonSchema jsonSchema = jsonSchemaFacory.getSchema(schemaNode);
Set<ValidationMessage> errorMessage = jsonSchema.validate(validationFor);
for (ValidationMessage error: errorMessage)
{
System.out.println(error.getMessage());
}


}

}

输出

$.errorKey: is not defined in the schema and the schema does not allow additional properties

关于java - com.networknt json-schema-validator 不检查 json 中的未知关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62117492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com