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?
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.
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.
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.
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 .
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).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With