Coder Social home page Coder Social logo

Comments (24)

fge avatar fge commented on July 22, 2024

(what version?)

That sounds strange, for one reason at least: http://json-schema.org/draft-03/schema# does not exist (404).

If I adapt the schema like this:

{
    "extends": {
        "$ref": "#/core"
    },
    "properties": {
        "indexed": {
            "type": "boolean",
            "default": false
        }
    },
    "core": {
        "$schema" : "http://json-schema.org/draft-03/schema#",
        "id" : "http://json-schema.org/draft-03/schema#",
        "type" : "object",

        "properties" : {
            "type" : {
                "type" : ["string", "array"],
                "items" : {
                    "type" : ["string", { "$ref" : "#" } ]
                },
                "uniqueItems" : true,
                "default" : "any"
            },

            "properties" : {
                "type" : "object",
                "additionalProperties" : { "$ref" : "#" },
                "default" : {}
            },

            "patternProperties" : {
                "type" : "object",
                "additionalProperties" : { "$ref" : "#" },
                "default" : {}
            },

            "additionalProperties" : {
                "type" : [ { "$ref" : "#" }, "boolean" ],
                "default" : {}
            },

            "items" : {
                "type" : [ { "$ref" : "#" }, "array" ],
                "items" : {
                    "$ref" : "#"
                },
                "default" : {}
            },

            "additionalItems" : {
                "type" : [ { "$ref" : "#" }, "boolean" ],
                "default" : {}
            },

            "required" : {
                "type" : "boolean",
                "default" : false
            },

            "dependencies" : {
                "type" : "object",
                "additionalProperties" : {
                    "type" : ["string", "array", { "$ref" : "#" } ],
                    "items" : { "type" : "string" }
                },
                "default" : {}
            },

            "minimum" : {
                "type" : "number"
            },

            "maximum" : {
                "type" : "number"
            },

            "exclusiveMinimum" : {
                "type" : "boolean",
                "default" : false
            },

            "exclusiveMaximum" : {
                "type" : "boolean",
                "default" : false
            },

            "minItems" : {
                "type" : "integer",
                "minimum" : 0,
                "default" : 0
            },

            "maxItems" : {
                "type" : "integer",
                "minimum" : 0
            },

            "uniqueItems" : {
                "type" : "boolean",
                "default" : false
            },

            "pattern" : {
                "type" : "string",
                "format" : "regex"
            },

            "minLength" : {
                "type" : "integer",
                "minimum" : 0,
                "default" : 0
            },

            "maxLength" : {
                "type" : "integer"
            },

            "enum" : {
                "type" : "array",
                "minItems" : 1,
                "uniqueItems" : true
            },

            "default" : {
                "type" : "any"
            },

            "title" : {
                "type" : "string"
            },

            "description" : {
                "type" : "string"
            },

            "format" : {
                "type" : "string"
            },

            "divisibleBy" : {
                "type" : "number",
                "minimum" : 0,
                "exclusiveMinimum" : true,
                "default" : 1
            },

            "disallow" : {
                "type" : ["string", "array"],
                "items" : {
                    "type" : ["string", { "$ref" : "#" } ]
                },
                "uniqueItems" : true
            },

            "extends" : {
                "type" : [ { "$ref" : "#" }, "array" ],
                "items" : {
                    "$ref" : "#"
                },
                "default" : {}
            },

            "id" : {
                "type" : "string",
                "format" : "uri"
            },

            "$ref" : {
                "type" : "string",
                "format" : "uri"
            },

            "$schema" : {
                "type" : "string",
                "format" : "uri"
            }
        },

        "dependencies" : {
            "exclusiveMinimum" : "minimum",
            "exclusiveMaximum" : "maximum"
        },

        "default" : {}
    }
}

and run this simple main program:

    public static void main(final String... args)
        throws IOException
    {
        final JsonNode schemaNode = JsonLoader.fromResource("/t.json");
        final JsonNode data = JsonLoader.fromResource("/t2.json");

        final JsonSchema schema = JsonSchema.fromNode(schemaNode);

        final ValidationReport report = new ValidationReport();

        schema.validate(report, data);

        for (final String msg: report.getMessages())
            System.out.println(msg);

        System.exit(0);
    }

then the output is as expected:

#/properties/from/indexed: instance does not match any allowed primitive type

Care to show your code?

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

The version that I am using is 0.5.0 beta 3.
Then, about the code. I have three files, schema-draftv3.json, schema1.json and schema2.json.
The first file has the schema drat v3 (your #/core) and other files have the following contents

schema1.json

{
     "extends":{"$ref" : "http://json-schema.org/draft-03/schema#"},
     "properties":{
    "indexed":{"type": "boolean", "default":false} 
     }
}

schema2.json

{
    "type" : "object",
    "properties" : {
        "from" : { "type" : "string", "required":true, "indexed":111 }
    }
}

My test program is

JsonNode draftv3 = JsonLoader.fromFile(new File("schema-draftv3.json"));
JsonNode schema1 = JsonLoader.fromFile(new File("schema1.json"));
JsonNode schema2 = JsonLoader.fromFile(new File("schema2.json"));

JsonSchema schema = JsonSchema.fromNode(draftv3);
schema.validate(valReport, schema1);
for (String msg : valReport.getMessages())
    System.out.println(msg);

JsonSchema temp1schema = JsonSchema.fromNode(schema1);
temp1schema.validate(valReport, schema2);
for (String msg : valReport.getMessages())
    System.out.println(msg);

This the test that I was performed. Maybe I am making something wrong.
One question? If http://json-schema.org/draft-03/schema was not found, then that error should be reported, isn't it?

from json-schema-validator.

fge avatar fge commented on July 22, 2024

About your question: the JsonReference class maitains a map of schemas with URIs if an id is found, which is program-wide. Which means, as schema-draftv3.json has "http://json-schema.org/draft-03/schema#" in "id", the value will be cached, and it will not be looked up over the net.

That's probably a misfeature, I don't know. This part of the implementation is clearly not very nice. But that explains why you don't see an error.

As to the behaviour you see, I'll try and reproduce it.

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

Ok, thanks for your help.

from json-schema-validator.

fge avatar fge commented on July 22, 2024

OK, I have reproduced the bug...

Fixing it will not be easy :/ The implementation suffers a design problem AFAICS. This is a first impression, unfortunately I don't have the required time to debug the issue.

I'll update tonight.

from json-schema-validator.

fge avatar fge commented on July 22, 2024

"I don't have the required time to debug the issue"

I meant, I don't have the time at the moment.

$ref is a bitch :/

from json-schema-validator.

fge avatar fge commented on July 22, 2024

OK, I have found the source of the bug, and it is quite serious -- fixing it will require refactoring... JsonSchema as it is cannot handle this case.

I make this a top priority item. Thanks a lot for the reproducer. I'll keep you informed.

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

Ok, if you need any help please fell free to ask.

from json-schema-validator.

fge avatar fge commented on July 22, 2024

One question: do you use maven to grab artifacts or do you git clone?

If I need to have a fix tested, this will make a difference...

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

I use git clone.

from json-schema-validator.

fge avatar fge commented on July 22, 2024

OK, the bug is fixed, but the API needs refining before I put it in the wild.

In the meanwhile, you can try and see about the new API in Issue7Test. It is not final yet.

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

Fine. I will check it.

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

The problem is solved.

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

Hi. I was upgrading for latest version (yes, I was using a very older version..., 0.5.x) and when testing this issue again I realise that it is reproducible again. Maybe I am doing something wrong since I only adapt the old test to the new version.

from json-schema-validator.

fge avatar fge commented on July 22, 2024

OK, I admit I am too lazy to be reading this thread from the start again, so could you please post:

  • the version you are currently using,
  • the schema you use,
  • an instance which validates successfully but you think should not validate successfully, and
  • an instance which does not validate successfully but you think it should?

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

You wrote a Test case (Issue7TestCase that can be found in commit 7f3b82a) that summarises this issue.

The test case is:

package org.eel.kitchen;

import com.fasterxml.jackson.databind.JsonNode;
import org.eel.kitchen.jsonschema.ValidationContext;
import org.eel.kitchen.jsonschema.schema.JsonSchema;
import org.eel.kitchen.jsonschema.schema.JsonSchemaFactory;
import org.eel.kitchen.jsonschema.util.JsonLoader;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;

import static org.testng.Assert.*;

public final class Issue7Test
{
private JsonNode draftv3, schema1, schema2;

@BeforeClass
public void setUp()
    throws IOException
{
    draftv3 = JsonLoader.fromResource("/schema-draftv3.json");
    schema1 = JsonLoader.fromResource("/schema1.json");
    schema2 = JsonLoader.fromResource("/schema2.json");
}

@Test
public void testIssue7()
{
    final JsonSchemaFactory factory = new JsonSchemaFactory();

    ValidationContext context;
    JsonSchema schema;

    context = factory.newContext();
    schema = factory.create(draftv3);

    schema.validate(context, schema1);
    assertTrue(context.isSuccess());

    context = factory.newContext();
    schema = factory.create(schema1);

    schema.validate(context, schema2);
    assertFalse(context.isSuccess());
}

}

The schema1.json file is

{
"extends":{
"$ref" : "http://json-schema.org/draft-03/schema#"
},
"properties":{
"indexed":{
"type": "boolean",
"default":false
}
}
}

The schema2.json is

{
"type" : "object",
"properties" : {
"from" : {
"type" : "string",
"required":true,
"indexed":111
}
}
}

The problem is that on schema2, the "indexed" property has an incorrect value 111, instead of true or false.
The schema is validated correctly, but should give an error.
I was using 0.5.3 beta X and now I am using 1.4.1.

from json-schema-validator.

fge avatar fge commented on July 22, 2024

The code has changed quite a lot since 0.5.x days, the test case you point to cannot work anymore. Can you provide a more up to date example? For instance, based on one of the examples in the org.eel.kitchen.jsonschema.examples package?

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

I adapt this test case for the version 1.4.1. Where is the code. Is based on the Examples and is not a junit case.

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import org.eel.kitchen.jsonschema.main.JsonSchema;
import org.eel.kitchen.jsonschema.main.JsonSchemaFactory;
import org.eel.kitchen.jsonschema.report.ValidationReport;
import org.eel.kitchen.jsonschema.util.JsonLoader;

public class JsonSchemaTest {

public static void main(String[] args) {
    try {
        JsonNode draftv3 = JsonLoader.fromResource("/draftv3/schema");
        JsonNode schema1 = JsonLoader.fromFile(new File("schema1.json"));
        JsonNode schema2 = JsonLoader.fromFile(new File("schema2.json"));

        ValidationReport report;

        JsonSchemaFactory factory = JsonSchemaFactory.defaultFactory();
        JsonSchema draftv3Schema = factory.fromSchema(draftv3);
        report = draftv3Schema.validate(schema1);
        if (!report.isSuccess())
            System.out.println("Schema1: " + report.asJsonObject());

        JsonSchema schema1Schema = factory.fromSchema(schema1);
        report = schema1Schema.validate(schema2);
        if (!report.isSuccess())
            System.out.println("Schema2: " + report.asJsonObject());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

from json-schema-validator.

fge avatar fge commented on July 22, 2024

OK, how does that fail and when? Can you give the full output?

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

The problem is that it should fail, but the schema2 is validated correctly.
Basically the all pictures is more less as follows:

  • Define a schema1 that is an extension from draftv3 and that add a new property, 'indexed', that is declared as boolean.
  • Define an instance of schema1, schema2. The value for property 'indexed' is 111.
  • The validator should give an error, since for property 'indexed' only a boolean value should be allowed.

from json-schema-validator.

fge avatar fge commented on July 22, 2024

OK, I think I am starting to get the whole picture -- and why it does not work.

Before I give a detailed answer, can you confirm one thing: you are trying to validate a schema, and not an instance?

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

Yes, I am trying to validate a schema. Sorry, in the previous comment I incorrectly use the term 'instance' to name the schema2.

from json-schema-validator.

fge avatar fge commented on July 22, 2024

Well, I thought I had figured out why you got this error. Now, after having debugged through a test case I have written (which essentially matches yours), I see my first thought was wrong.

There is a fundamentally flawed logic somewhere in my implementation and I need to figure it out :/

Since this issue is rather old and not relevant to new code anymore, would you mind re-opening a new issue with sample code? I have only checked HEAD at the moment, I am pretty sure that 1.4.x is affected and I surmise 1.2.x is affected as well. But I need to "start anew" here. This buggers me :/

from json-schema-validator.

asargento avatar asargento commented on July 22, 2024

I have a clue... I think that the validation context is not being created correctly when you have schema that is an extension of other schema.
And I will open an new issue.

from json-schema-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.