MandyW
MandyW

Reputation: 1147

How to enforce restrictions in Json Schema

we are using JsonSchema to document our Rest APIs and I need to be sure that every string, number, array has restrictions on their maximum size applied to them i.e.

This will then allow us to run javax validation on the POJOs generated from the JsonSchema (we use jsonschema2pojo with JSR303 annotations).

I'd rather not manually eyeball every schema passed my way so wondering if there was any automated tool to check every element for these items? If not I may be writing one :-)

Many thanks

Upvotes: 1

Views: 2287

Answers (1)

jruizaranguren
jruizaranguren

Reputation: 13597

You may build your own meta-schema the same way any valid JSON-schema can be validated against the draft-04 meta-schema.

Taking your sample, you would add the following constraints to general valid JSON-schemas:

{
    "oneOf" : [{
            "type" : "string",
            "required" : ["pattern", "maxLength"]
        }, {
            "type" : "array",
            "required" : ["maxItems"]
        }, {
            "type" : {
                "enum" : ["number", "integer"]
            },
            "required" : ["maximum"]
        }, {
            "type" : {
                "enum" : ["object", "boolean", "null"]
            }
        }

    ]
}

After your own meta-validation, you may safely generate java classes.

Upvotes: 1

Related Questions