Published 25 Oct, 2022

Java - com.networknt json-schema-validator not checking Unknown keywords in json

Category Java
Modified : Nov 30, 2022
94

I am using json-schema-validator for validating the requests. It is validating fine.

But, Request contains some invalid objects, it not throwing any error.

Schema

{
    "$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" is Unknown keywords in the schema, But, this json-schema-validator is not throwing any error.

Is there any way to validate this ?

Dependencies

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

Answers

There are 1 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Json, Java, Validation. These have been categorized in sections for a clear and precise explanation.

43

Answer is "additionalProperties":false should be added in schema.

{
    "$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    
} 

Sample Code:

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());
        }


    }

}

Output

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