chandan Kr.
chandan Kr.

Reputation: 19

Number of rules defined in everit schema using Java

I am trying to validate a JSON schema against the JSON input.

I am using org.everit.json.schema-1.12.1.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 Chandan
 */
public class ValidateJson {
    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);

    }
}

The requirement is to know the number of rules defined in the schema.

Schema schemaValidator = SchemaLoader.load(jsonSchema);

How can I identify how many rules the schema object contains?

Upvotes: 0

Views: 52

Answers (0)

Related Questions