Home > OS >  How to get error message in JSON SCHEMA VALIDATION using EVERIT in java?
How to get error message in JSON SCHEMA VALIDATION using EVERIT in java?

Time:04-06

I am trying to validate a json schema against the json input. I am using

org.everit.json.schema-1.0.0.jar

My json input

{ "id": 200, "name": "Green Learner", "cost": 0 }

My JSON SCHEMA .

    {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Youtube Channel",
    "description": "Youtube Channel for software development training",
    "type": "object",
     
    "properties": {
     
       "id": {
          "description": "The unique identifier for a product",
          "type": "integer"
       },
         
       "name": {
          "description": "Name of the the channle",
          "type": "string"
       },
         
       "cost": {
          "type": "number",
          "minimum": 100,
          "maximum":10000
       }
    },
     
    "required": ["id", "name", "cost"]
 }

JAVA Code for validation.

import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.everit.json.schema.Schema;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 *
 * @author amitkumar
 */
public class JsonValidate {
    public static void main(String[] args) throws FileNotFoundException {
        File schemaFile = new File("schema.json");

        JSONTokener schemaData = new JSONTokener(new FileInputStream(schemaFile));
        JSONObject jsonSchema = new JSONObject(schemaData);

        //json data
        File jsonData = new File("product_invalid.json");
        JSONTokener jsonDataFile = new JSONTokener(new FileInputStream(jsonData));
        JSONObject jsonObject = new JSONObject(jsonDataFile);

       
        Schema schemaValidator = SchemaLoader.load(jsonSchema);
        schemaValidator.validate(jsonObject);

        System.out.println(jsonObject.getInt("cost"));

    }
}

When i run the code with org.everit.json.schema-1.0.0.jar, i get following error message .

Exception in thread "main" org.everit.json.schema.ValidationException:v0.0 is not higher or equal to 100

This is the warning message i get when i use

json-schema-validator-1.0.42.jar comes with com.networknt it clearly mention me object name which got error.

$.Cost: 0.0 is not higher or equal to 100

i want to do the same with org.everit.json.schema-1.0.0.jar, which object in my json input got the error .It does not show me the object name .

CodePudding user response:

If the library does not support your requirement, you could log the missing information by yourself. Something like

try {
    schemaValidator.validate(jsonObject);
} catch(ValidationException e) {
    LOG.error("Validation error in Object: '{}'", yourObjectName, e)
}

By the way, there are more newer versions than 1.0.0 of org.everit.json.schema available: https://search.maven.org/artifact/org.everit.json/org.everit.json.schema

Edit add additional information:
The ValidationException has methods like getPointerToViolation() which might get you the information you need. See JavaDoc of the current version: https://erosb.github.io/everit-json-schema/javadoc/1.12.2/org/everit/json/schema/ValidationException.html#getPointerToViolation()

Be aware, that version 1.0.0 does not have this methods (JavaDoc of v1.0.0)!

  • Related