Comments (8)
I see no reason why this cannot be doable, the trick however would be to generate the schemas correctly.
At the schema level, what you can already do is this:
{
"properties": {
"type": { "enum": [ "dog" ] },
"etc": "etc"
}
}
This would ensure that only dogs are permitted.
I have a prototype for a processing chain ready, so I'll let @reinert know when I am ready and see how we can help you solving this.
Would you mind providing some sample JSON data?
from json-schema-validator.
-ETIMEOUT...
from json-schema-validator.
Well, it is not my implementation which supports such a feature, but JSON Schema itself, with the "extends" keyword in draft v3 and the "allOf" keyword in draft v4. For instance:
{
"title": "animal",
"id": "some://where/animal#",
"etc": "etc"
}
and
{
"title": "dog",
"id": "some://where/dog#",
"allOf": [ { "$ref": "animal#" }, { "specific": [ "dog", "elements", "here" ] } ]
}
If this is what you are asking, the library currently does not have an API to build schemas out of other schemas (save for using JsonNodeFactory
to manipulate the JSON Schema directly).
Here again, JJSchema looks like it does what you want. I am curious: what is your target use of JSON Schema? It seems to me like you come from the "Java XML world", where options are aplenty and where you can do everything save for coffee...
Can you take some time to explain what you are trying to achieve?
from json-schema-validator.
currently we are using xml for communicating between partners,
and we use an XSD to validate a xml, and we use JAXB to serialize/deserialize a xml, and JAXB to generate Java classes from the XSD.
We would like to communicate between partners also with json,
so need to do the same as before with json, instead of xml.
if i understood correctly, allOf just indicates that the type must conforms to all the schemas.
that's not what i meant,
I meant that we have a json file with a list of animals, and some of them are dogs and some of them are cats.
dog inherits from animal, and defines some more attributes.
the dog instance in the json file needs to indicate somehow that it is a dog (as it is done in xml with xsi:type).
in the previous jackson example, this is done by adding a new field called "type", with possible values "dog" or "cat".
http://programmerbruce.blogspot.de/2011/05/deserialize-json-with-jackson-into.html
{
"animals":
[
{"type":"dog","name":"Spike"},
{"type":"cat","name":"Fluffy"}
]
}
from json-schema-validator.
{
"type" : "object",
"additionalProperties": false,
"properties" : {
"animals" : {
"type" : "array",
"items" : { "type" : "#/animal" }
}
},
"animal" : {
"type" : "object",
"additionalProperties": false,
"properties" : {
"name" : { "type" : "string", "required": true }
}
},
"dog" : {
"type" : "object",
"additionalProperties": false,
"extends" : "#/animal"
"properties" : {
"breed" : { "type" : "string", "required": true },
"leashColor" : { "type" : "string", "required": true }
}
},
"cat" : {
"type" : "object",
"extends" : "#/animal"
"additionalProperties": false,
"properties" : {
"favoriteToy" : { "type" : "string", "required": true }
}
}
}
I would expect that this passes:
{
"animals":
[
{"type":"dog","name":"Spike", "breed":"mutt", "leash_color":"red"},
{"type":"cat","name":"Fluffy", "favorite_toy":"spider ring"}
]
}
and that if you remove the "favorite_toy" from the cat, then it should not pass.
but somehow, the json schema needs to know that the property "type" asserts the type of the instance (dog or cat),
and validate the instance with its schema.
from json-schema-validator.
No, the type
keyword in JSON Schema does not work like this. It only checks the primitive types of the instances (in v3, it also played the role of v4's anyOf
).
You need to make use of subschemas instead. Another thing is that schemas are not additive, they are all evaluated in their own context. Which means, writing, for instance (draft v4):
{
"type": "object",
"allOf": [
{ "properties": { "p": {} }, "additionalProperties": false },
{ "properties": { "q": {} } }
]
}
will fail to validate:
{ "p": "foo", "q": "bar" }
All schemas are evaluated in their own context. Therefore, the first schema checking p
sees q
and yells.
This is quite different from XML Schema!
from json-schema-validator.
Back to your problem a little. What you want is really definitions
. It is defined as a placeholder for subschemas. You can write:
{
"definitions": {
"type1": { "schema": "here" },
"type2": { "schema": "here" }
}
}
And then, from your schema, you refer to them using $ref
:
{
"$ref": "#/definitions/type1"
}
An example of such a subdivision can be found in a schema I wrote for GeoJSON:
As to the general status of my project: I have the processing chain mechanics sorted and tested; after I am done porting all validation keywords etc to it, I'll attack the next steps, and that includes making a processor to generate a JSON Schema from a POJO (using JJSchema) and the reverse (using jsonschema2pojo).
Which means I'll have a pretty complete chain... But there will be package split: the validation engine parts will still be independent.
from json-schema-validator.
OK, so it appears the package to use here is jsonschema2pojo. Note that Jackson proper can do it (your link to programmerbruce above tells how and I have even implemented it myself), but the question appears to be: do you want to generate schemas for validation, deserialize directly, serialize, all of them, generate schemas from classes, all of them?
Also, and that is important: where do you expect this type information to be carried? In instances or in schemas? With XSD this is in schemas. Deserializing from instances can be done with Jackson. With some work, JJSchema can be adapted to generate schemas according to some "pivot value" in instances which would guide the process of generating schemas.
In short: what are your inputs, what are your outputs?
from json-schema-validator.
Related Issues (20)
- 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 HOT 2
- release new version HOT 2
- How can we get Error Message with the error path appended?
- Obtain JUST the message from a ValidationMessage?
- BUG: ECMA262 valid pattern throws an exception
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.