Comments (8)
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.
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.
Hello again,
Can I close the issue?
from json-schema-validator.
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.
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.
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.
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.
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)
- can't validate a doc against the schema containing "const" HOT 1
- Failed validation for no apparent reason?
- Getting java.util.concurrent.ExecutionException while validating
- Error when validating Json against a schema defining oneOf
- Are we planning on supporting draft-07 HOT 1
- Example code on README.md please plus links to examples in src
- Migrate from joda-time to java 8 time API HOT 2
- Improcess error hanling in getJsonSchema - never squash exceptions
- Android app crashes because schema (v4) cannot be found at runtime HOT 2
- Getting NPE when passing null to validate method.
- Has the functionality of the ResolvingSchemaWalker been replaced in more recent versions?
- Conflicting Transitive Dependency Versions for Guava and Jsr305
- Where is pom.xml ? I want to import this library to my local and customize it
- Not compatible with GraalVM
- NoSuchFieldError on signing
- When JSONObject contains JSONArray, JSONArray's object can not be validated
- draftv4/schema is different between json-schema-validator 2.2.14 and json-schema-core 1.2.14
- release new version HOT 2
- How can we get Error Message with the error path appended?
- Obtain JUST the message from a ValidationMessage?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-schema-validator.