jimblackler / jsonschemafriend

A JSON Schema loader and validator, delivered as a Java library.
Apache License 2.0
52 stars 23 forks source link

Loading Schema String Not Validating #101

Open jonsinfinity1 opened 2 months ago

jonsinfinity1 commented 2 months ago

I cannot get a schema string to load. I consistently get a TypeError expecting an object but found string with my implementation and then when I try to run the basic example with json string from your documentation I get a TypeError saying it expected an integer but got boolean. I'm using the 0.12.4 version using Java 11 (corretto), I first started with Java 22. I tried previous versions of your build but the result has been the same. Clean intellij project using gradle and you're the only dependency.

public class Main {
    public static void main(String[] args) {
        // Create a new schema in a JSON string.
        String schemaString = "{"
                + "  \"$schema\": \"http://json-schema.org/draft-07/schema#\","
                + "  \"type\": \"integer\""
                + "}";

        try {
            SchemaStore schemaStore = new SchemaStore(); // Initialize a SchemaStore.
            Schema schema = schemaStore.loadSchemaJson(schemaString); // Load the schema.
            Validator validator = new Validator(); // Create a validator.
            validator.validateJson(schema, "1"); // Will not throw an exception.
            validator.validateJson(schema, "true"); // Will throw a ValidationException.
        } catch (SchemaException e) {
            e.printStackTrace();
        }
    }
}

11:00:39 PM: Executing ':org.example.Main.main()'...

Task :compileJava Task :processResources NO-SOURCE Task :classes

Task :org.example.Main.main()

BUILD SUCCESSFUL in 374ms 2 actionable tasks: 2 executed net.jimblackler.jsonschemafriend.ListValidationException: Validation errors: true at root failed with "Expected: [integer] Found: [boolean]" at net.jimblackler.jsonschemafriend.Validator.validate(Validator.java:715) at net.jimblackler.jsonschemafriend.Validator.validateJson(Validator.java:721) at org.example.Main.main(Main.java:21) 11:00:40 PM: Execution finished ':org.example.Main.main()'.

jimblackler commented 2 months ago

I get a TypeError saying it expected an integer but got boolean.

That's exactly what's supposed to happen with the example. The line triggering it is commented "Will throw a ValidationException" (TypeError is a ValidationException).

jonsinfinity1 commented 2 months ago

My bad, I'm getting the same error when trying to load my schema from file and figured the error was happening at the same place. When I use my schema file it doesn't get past the loadSchema(). The schema validates using your validator here: https://tryjsonschematypes.appspot.com/#validate as well as generating data using the schema. I thought maybe it had something to do with the way the string was created but I've taken the string directly from debug to your validator and it works fine.

One thing to note is that it sets the meta-schema to draft-04 even though the schema is using draft-07. I believe the schema I'm using still conforms to draft-04 so not sure it'a a factor.

Any thoughts would be appreciated.

package org.example;

//import net.jimblackler.jsongenerator.Configuration;
//import net.jimblackler.jsongenerator.DefaultConfig;
//import net.jimblackler.jsongenerator.Generator;
import net.jimblackler.jsonschemafriend.Schema;
import net.jimblackler.jsonschemafriend.SchemaStore;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {

        try {
            // Load the JSON schema from a file or string
            String schemaFilePath = "/../../../../src/schemas/address.json";
            String schemaContent = new String(Files.readAllBytes(Paths.get(schemaFilePath)));

            // Parse the schema using the SchemaLoader
            SchemaStore schemaStore = new SchemaStore();
            Schema schema = schemaStore.loadSchema(schemaContent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here's the schema I'm testing with:

{
  "$id": "https://example.com/address.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Address",
  "type": "object",
  "properties": {
    "streetAddress": {
      "type": "string"
    },
    "city": {
      "type": "string"
    },
    "state": {
      "type": "string"
    },
    "postalCode": {
      "type": "string",
      "pattern": "^\\d{5}$"
    }
  },
  "required": ["streetAddress", "city", "state", "postalCode"]
}

And here's the stacktrace:

net.jimblackler.jsonschemafriend.StandardGenerationException: {valid=false, keywordLocation=http://json-schema.org/draft-04/schema, absoluteKeywordLocation=http://json-schema.org/draft-04/schema#, instanceLocation=, errors=[{valid=false, error=Expected: [object] Found: [string], keywordLocation=http://json-schema.org/draft-04/schema, absoluteKeywordLocation=http://json-schema.org/draft-04/schema#, instanceLocation=#}]}
    at net.jimblackler.jsonschemafriend.SchemaStore.loadSchema(SchemaStore.java:233)
    at net.jimblackler.jsonschemafriend.SchemaStore.loadSchema(SchemaStore.java:138)
    at net.jimblackler.jsonschemafriend.SchemaStore.loadSchema(SchemaStore.java:94)
    at net.jimblackler.jsonschemafriend.SchemaStore.loadSchema(SchemaStore.java:86)
    at org.example.Main.main(Main.java:26)
1:15:12 PM: Execution finished ':org.example.Main.main()'.