Comments (17)
This is a valid schema for draft v3, yes, but not draft v4. And draft v4 is the default.
You have two choices:
- add
"$schema": "http://json-schema.org/draft-03/schema#"
at the root of your schema, since the library detected its presence and switches versions automatically; - if you want to use draft v3 as the default, you have to customize your
ValidationConfiguration
:
final ValidationConfiguration cfg = ValidationConfiguration.newBuilder()
.setDefaultVersion(SchemaVersion.DRAFTV3).freeze();
final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
.setValidationConfiguration(cfg).freeze();
Or of course, you can use a draft v4 schema:
{
"type" : "array",
"items" : {
"anyOf" : [
{ "type" : "object", "properties" : { "name" : { "type" : "string" }}},
{ "type" : "object", "properties" : { "price" : { "type" : "number" } } }
]
}
}
from json-schema-validator.
Please note that it is not because you bind array elements to a certain schema that these elements must be there. If, for instance, you want your array to have at least these two elements then you must use minItems
. Similarly, if you want your array to have a limited number of elements, use maxItems
.
Here is for instance a schema constraining an array to have two items, the first being an integer and the second a boolean:
{
"type": "array",
"items": [ { "type": "integer" }, { "type": "boolean" } ],
"minItems": 2,
"maxItems": 2
}
from json-schema-validator.
Ah, you didn't mention that you wanted schemas at specific indices in the array. In that case, your schema is not doing it correctly. Your schema should be this:
{
"type": "array",
"items": [
{ "schema": [ "for", "first", "element", "here" ] },
{ "schema": [ "for", "second", "element", "here" ] }
],
"etc": "etc"
}
In your other example, the array would not be limited in the number of items but all items would share the same schema.
from json-schema-validator.
Thanks!
one thing though - I'm not sure the anyOf will help me with the restriction i'm trying to achieve:
I want to restrict the array to hold couples of [country, region](it's not in the json schema example I wrote above but it's what i need to perform in my schema)
I want the country to be the first element in the array and to be selected from an enum of countries,
the region to be the second element in the array and to be selected from an enum of regions.
I tried the following schema hoping this will do the trick:
{
"type": "array",
"items": {
"type":[
{"type":"string","enum":["FR","BE"]},
{"type":"string","enum":["EU","US"]}
]
},
"minItems": 1,
"uniqueItems": true,
"maxItems": 2
}
the question is if the anyOf is suitable...
from json-schema-validator.
Thanks!
sorry I wasn't clear..
I'll give it a try, thanks a lot!
from json-schema-validator.
and this can work with draft v4 ?
So I should write it like this?:
{
"type": "array",
"items": [
{"type":"string","enum":["FR","BE"]},
{"type":"string","enum":["EU","US"]}
],
"minItems": 1,
"maxItems": 2
}
from json-schema-validator.
The items
keyword hasn't changed semantics with draft v4, so yes, this will work.
Note that you don't need "type": "string"
since an instance is valid against enum
if it is exactly equal to one of the elements in the enum. Therefore, enum
is enough here.
from json-schema-validator.
thank you so much for your quick and helpful replies,
I'll try this :)
from json-schema-validator.
works great thanks!
from json-schema-validator.
Good to hear ;) Can I close the issue?
from json-schema-validator.
Yes, thanks again for your help :)
from json-schema-validator.
OK, thanks! Do not hesitate if you have further questions!
Note: for pure schema-writing related problems, you may join the Google group, or channel #jsonschema on Freenode.
from json-schema-validator.
shouldn't items contain schema or array of schemas? if so using anyOf should not be inside of items, but instead of items ...
from json-schema-validator.
@kof sorry, I don't quite make up your question here. What do you have in mind? Remember that if the value of items
is an array, each element of this array is the schema the instance element must obey at that particular index.
from json-schema-validator.
Hi guys, I need help build a json schema from the sample below:
{
"1453235400": {
"ERRORS_OUT": null,
"OCTETS_OUT": 232.957375822694,
"PACKETS_OUT": 1.8996969225584,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 1.86311703463372,
"OCTETS_IN": 238.160971788575,
"ERRORS_IN": null,
"IFSTATUS": 1
},
"1453235700": {
"ERRORS_OUT": null,
"OCTETS_OUT": 231.133641048357,
"PACKETS_OUT": 1.88395658914729,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 1.84730778885197,
"OCTETS_IN": 236.086393503138,
"ERRORS_IN": null,
"IFSTATUS": 1
},
"1453236000": {
"ERRORS_OUT": null,
"OCTETS_OUT": 230.8488,
"PACKETS_OUT": 1.88284444444444,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 1.84617777777778,
"OCTETS_IN": 235.842177777778,
"ERRORS_IN": null,
"IFSTATUS": 1
}
}
from json-schema-validator.
@agapare You should start a new issue for this... it should be pretty doable but I'm going to need 20 free minutes to work it out.
from json-schema-validator.
this issue was fixed ?
{
"1453235400": {
"ERRORS_OUT": null,
"OCTETS_OUT": 232.957375822694,
"PACKETS_OUT": 1.8996969225584,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 1.86311703463372,
"OCTETS_IN": 238.160971788575,
"ERRORS_IN": null,
"IFSTATUS": 1
},
"1453235700": {
"ERRORS_OUT": null,
"OCTETS_OUT": 231.133641048357,
"PACKETS_OUT": 1.88395658914729,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 1.84730778885197,
"OCTETS_IN": 236.086393503138,
"ERRORS_IN": null,
"IFSTATUS": 1
},
"1453236000": {
"ERRORS_OUT": null,
"OCTETS_OUT": 230.8488,
"PACKETS_OUT": 1.88284444444444,
"DISCARDS_OUT": null,
"SPEED": 2048000,
"DISCARDS_IN": null,
"PACKETS_IN": 1.84617777777778,
"OCTETS_IN": 235.842177777778,
"ERRORS_IN": null,
"IFSTATUS": 1
}
}
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.