0

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?

4
  • what do you mean "rules"? Do you want to know how many constraints are defined in the schema? there are many, the type, properties, the type of the properties, minimum, maximum, additionalProperties: true (default behavior), $schema, required. what is the purpose of knowing how many "rules" are defined? Your code doesn't appear to gather any data on how many rules are defined. It's only performing a validation step using the schema definition. Commented Sep 12, 2024 at 15:59
  • @JeremyFiel - Thanks for replying. I mean to say in above example this schema has 3 fields like "id", "name", "cost". I want know these fields or number of fields using Schema Object. We have requirement of writing logic based on number of fields/rules present in the schema. Commented Sep 13, 2024 at 6:26
  • Sorry, your requirement is not clear. Commented Sep 13, 2024 at 11:53
  • @JeremyFiel I have explained everything in the description. Sorry if you are not clear but requirement is simple - in above JSON schema example we have 3 fields [id, name, cost] now same I want to fetch using everit schema object - org.everit.json.schema.Schema as framework helps to convert JSON schema to everit schema using below code - ''' Schema schemaValidator = SchemaLoader.load(jsonSchema); ''' . Now want to get 3 fields details using everit schema object. Commented Sep 16, 2024 at 6:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.