Java com.networknt json模式验证程序未检查json中的未知关键字
我使用json模式验证器来验证请求。很好 但是,请求包含一些无效对象,它不会抛出任何错误 模式Java com.networknt json模式验证程序未检查json中的未知关键字,java,json,validation,Java,Json,Validation,我使用json模式验证器来验证请求。很好 但是,请求包含一些无效对象,它不会抛出任何错误 模式 { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Product", "description": "A product from Acme's catalog", "type": "object", "properties": { "id": {
{
"$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模式验证器不会抛出任何错误
有没有办法验证这一点
依赖关系
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.40</version>
</dependency>
com.networknt
json模式验证器
1.0.40
答案是“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());
}
}
}
您可能还需要考虑切换到JSON验证的更明确的工具,例如声明性JSON验证。
$.errorKey: is not defined in the schema and the schema does not allow additional properties