Coder Social home page Coder Social logo

Comments (17)

fge avatar fge commented on July 22, 2024 7

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.

fge avatar fge commented on July 22, 2024 2

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.

fge avatar fge commented on July 22, 2024 1

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.

ettig avatar ettig commented on July 22, 2024

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.

ettig avatar ettig commented on July 22, 2024

Thanks!
sorry I wasn't clear..
I'll give it a try, thanks a lot!

from json-schema-validator.

ettig avatar ettig commented on July 22, 2024

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.

fge avatar fge commented on July 22, 2024

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.

ettig avatar ettig commented on July 22, 2024

thank you so much for your quick and helpful replies,
I'll try this :)

from json-schema-validator.

ettig avatar ettig commented on July 22, 2024

works great thanks!

from json-schema-validator.

fge avatar fge commented on July 22, 2024

Good to hear ;) Can I close the issue?

from json-schema-validator.

ettig avatar ettig commented on July 22, 2024

Yes, thanks again for your help :)

from json-schema-validator.

fge avatar fge commented on July 22, 2024

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.

kof avatar kof commented on July 22, 2024

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.

fge avatar fge commented on July 22, 2024

@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.

agapare avatar agapare commented on July 22, 2024

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.

huggsboson avatar huggsboson commented on July 22, 2024

@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.

tigerqiu712 avatar tigerqiu712 commented on July 22, 2024

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)

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.