Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set exact value in a JSON Schema?

I use JSON Schema to validate app objects against some schema for testing.

I see that I can set minimum and maximum values for a property:

"responseCode": {
        "type": "integer",
        "minimum": 100,
        "maximum": 500
    }

But I couldn't find if I can set an exact required value, like "value":123.

Is it possible to set it to exactly what I need to validate for?

like image 875
Sergei Basharov Avatar asked Nov 16 '16 09:11

Sergei Basharov


People also ask

What is const in JSON Schema?

const. An instance validates against this keyword if its value equals to the value of this keyword. The value of this keyword can be anything. Schema.

What should be the value of required keyword in a JSON Schema?

Required Properties The required keyword takes an array of zero or more strings. Each of these strings must be unique. In Draft 4, required must contain at least one string.

Can JSON store ints?

The integer type is used for integral numbers. JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. For example, 1 and 1.0 are two ways to represent the same value in JSON.

What is allOf in JSON Schema?

By the definition of this keyword, it meant that the instance had to be valid against the current schema and all schemas specified in extends ; basically, draft v4's allOf is draft v3's extends .


2 Answers

You can:

{ "enum": [123] }

or

{ "const": 123 }

const is now part of draft-06 of JSON schema specification (it is supported by Ajv and some other validators).

like image 191
esp Avatar answered Sep 18 '22 11:09

esp


Already mentioned but the 2 options I know are:

{ "enum": [123] }

and

{ "const": 123 }

My source was: https://json-schema.org/understanding-json-schema/reference/generic.html

Keep up the good work!

like image 38
Héctor E. Orbegoso L. Avatar answered Sep 20 '22 11:09

Héctor E. Orbegoso L.