Coder Social home page Coder Social logo

Comments (8)

fge avatar fge commented on July 3, 2024

Hello,

Have you consulted the Javadoc links in the readme? Also, what do you call the latest version? 1.0.1 or 1.1.0? Don't forget that 1.1.0 is a development version.

In essence: load your schema and data the way you already do (using JsonLoader) and then do:

    final JsonSchemaFactory factory = new JsonSchemaFactory.Builder().build();
    final SchemaContainer container = factory.registerSchema(jsonSchemaNode);
    final JsonSchema schema = factory.createSchema(container);
    final ValidationReport report = schema.validate(jsonNode);
    return report.isSuccess();

from json-schema-validator.

fge avatar fge commented on July 3, 2024

Also: JsonSchema is thread safe and immutable, so you can use only one instance to validate all your data. No need to rebuild a factory/schema each time, that's a waste of resources ;)

Please consult the javadoc links. There is sample code in them ;)

from json-schema-validator.

fge avatar fge commented on July 3, 2024

Hello again,

Can I close the issue?

from json-schema-validator.

java-developer avatar java-developer commented on July 3, 2024

Thank you for your help. For anyone having a similar issue, this is what I did:

/**
     * Returns <code>true</code> if the specified JSON conforms to the specified JSON schema.
     * 
     * @param json JSON to check.
     * @param jsonSchema JSON schema that the JSON must conform to.
     * @throws IOException If the JSON schema file cannot be opened.
     * @return <code>true</code> if the JSON is valid.
     */
    public boolean isValidJson(final String json, final File jsonSchema) throws IOException {
        final JsonSchemaFactory factory = new JsonSchemaFactory.Builder().build();
        final JsonNode jsonSchemaNode = JsonLoader.fromFile(jsonSchema);
        final SchemaContainer container = factory.registerSchema(jsonSchemaNode);
        final JsonSchema schema = factory.createSchema(container);
        final JsonNode jsonNode = JsonLoader.fromReader(new StringReader(json));
        final ValidationReport report = schema.validate(jsonNode);
        return report.isSuccess();
    }

from json-schema-validator.

fge avatar fge commented on July 3, 2024

Hello,

You are wasting resources here. You create a factory each time, but it is thread safe: get it out of your method!

What's more:

  • do you actually use several schemas? If not, you can get the JsonSchema instance out of the method as well;
  • a factory can load from file URIs: you can do factory.getSchema("file:/path/to/your/file.json"), no need to create a File object.

As I said, there is a lot of Javadoc ;)

from json-schema-validator.

java-developer avatar java-developer commented on July 3, 2024

Performance is not important here because this is just a utility method for my acceptance tests which validate the JSON output of my services. However, I have taken your advice and removed the factory from the method :)

I use several schemas so the JsonSchema instance needs to stay in the method.

I work for a big company and this method is being used by many people so I can't modify the method signature. As a result, I'll need to keep using File objects but I appreciate that your way is better.

from json-schema-validator.

fge avatar fge commented on July 3, 2024

Thanks for your input :) You have just given me an idea: creating, say, a URIUtils class which takes various kinds of inputs and turns them into URIs. So, for instance:

URIUtils.toURI(fileObject)

will return an absolute URI of the form:

file:/path/to/the/backing/File/object

Similarly, I think I'll add a .fromString method to JsonLoader. I just didn't think someone would actually have String inputs! But it does not cost much to do.

Just out of curiosity, do you use another JSON library to begin with? I also have in the roadmap a generic way of injecting JSON data from other libraries (I use Jackson, as you'll have noticed), would that be of some interest to you?

It is to me, because I want to be able to inject from/output to MongoDB, and right now I do this by hand.

from json-schema-validator.

java-developer avatar java-developer commented on July 3, 2024

We use Jackson annotations for marshalling via Spring 3.2 MVC using @responsebody on the controller methods.

Our requirements are very simple and I don't think we have any requirement to inject JSON data from other libraries. We just need to validate the output of our services against a JSON schema.

Thanks for taking the time to work on the project and answer questions :)

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.