go-sdk/internal/jsonschema/testdata/draft2020-12/minContains.json
Jonathan Amsterdam 860a55afbc jsonschema: validate array keywords
Add validation for the keywords dealing with JSON arrays.

We call the elements of an array its "items."

The unevaluatedItems keyword introduces some complexity. Its schema
applies to all items in the array that have not been successfully
validated ("evaluated" is the spec's misleading term) by this schema or
subschemas acting on the same array. The logic keywords like allOf and
anyOf thus contribute to the set of evaluated items.

Following the spec, we say that a schema produces "annotations" that
describe the items it has evaluated. The annotations type collects this
information. For the "contains" keyword we need to represent the set
of evaluated items, but for others we can optimize with an integer or
boolean. For example, the "items" keyword applies its schema to every
item in the array. If even one fails to validate, the array fails to
validate, and the validate call exits before reaching unevaluatedItems.
Therefore, if "items" succeeds, it has successfully validated every
item, so unevaluatedItems does not apply. A boolean annotation suffices
to record this fact.

The logic of "anyOf" must change so it can track all evaluated items.
It must still visit the remaining subschemas even after a subschema
succeeds.

Anticipating the next CL, which deals with JSON objects (maps), the
annotations struct supports properties as well as items.

Change-Id: I98283d36c9a55f4cae97636cbec20c4886daea11
Reviewed-on: https://go-review.googlesource.com/c/tools/+/668235
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-04-30 13:06:13 -07:00

224 lines
6.4 KiB
JSON

[
{
"description": "minContains without contains is ignored",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"minContains": 1
},
"tests": [
{
"description": "one item valid against lone minContains",
"data": [ 1 ],
"valid": true
},
{
"description": "zero items still valid against lone minContains",
"data": [],
"valid": true
}
]
},
{
"description": "minContains=1 with contains",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"contains": {"const": 1},
"minContains": 1
},
"tests": [
{
"description": "empty data",
"data": [ ],
"valid": false
},
{
"description": "no elements match",
"data": [ 2 ],
"valid": false
},
{
"description": "single element matches, valid minContains",
"data": [ 1 ],
"valid": true
},
{
"description": "some elements match, valid minContains",
"data": [ 1, 2 ],
"valid": true
},
{
"description": "all elements match, valid minContains",
"data": [ 1, 1 ],
"valid": true
}
]
},
{
"description": "minContains=2 with contains",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"contains": {"const": 1},
"minContains": 2
},
"tests": [
{
"description": "empty data",
"data": [ ],
"valid": false
},
{
"description": "all elements match, invalid minContains",
"data": [ 1 ],
"valid": false
},
{
"description": "some elements match, invalid minContains",
"data": [ 1, 2 ],
"valid": false
},
{
"description": "all elements match, valid minContains (exactly as needed)",
"data": [ 1, 1 ],
"valid": true
},
{
"description": "all elements match, valid minContains (more than needed)",
"data": [ 1, 1, 1 ],
"valid": true
},
{
"description": "some elements match, valid minContains",
"data": [ 1, 2, 1 ],
"valid": true
}
]
},
{
"description": "minContains=2 with contains with a decimal value",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"contains": {"const": 1},
"minContains": 2.0
},
"tests": [
{
"description": "one element matches, invalid minContains",
"data": [ 1 ],
"valid": false
},
{
"description": "both elements match, valid minContains",
"data": [ 1, 1 ],
"valid": true
}
]
},
{
"description": "maxContains = minContains",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"contains": {"const": 1},
"maxContains": 2,
"minContains": 2
},
"tests": [
{
"description": "empty data",
"data": [ ],
"valid": false
},
{
"description": "all elements match, invalid minContains",
"data": [ 1 ],
"valid": false
},
{
"description": "all elements match, invalid maxContains",
"data": [ 1, 1, 1 ],
"valid": false
},
{
"description": "all elements match, valid maxContains and minContains",
"data": [ 1, 1 ],
"valid": true
}
]
},
{
"description": "maxContains < minContains",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"contains": {"const": 1},
"maxContains": 1,
"minContains": 3
},
"tests": [
{
"description": "empty data",
"data": [ ],
"valid": false
},
{
"description": "invalid minContains",
"data": [ 1 ],
"valid": false
},
{
"description": "invalid maxContains",
"data": [ 1, 1, 1 ],
"valid": false
},
{
"description": "invalid maxContains and minContains",
"data": [ 1, 1 ],
"valid": false
}
]
},
{
"description": "minContains = 0",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"contains": {"const": 1},
"minContains": 0
},
"tests": [
{
"description": "empty data",
"data": [ ],
"valid": true
},
{
"description": "minContains = 0 makes contains always pass",
"data": [ 2 ],
"valid": true
}
]
},
{
"description": "minContains = 0 with maxContains",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"contains": {"const": 1},
"minContains": 0,
"maxContains": 1
},
"tests": [
{
"description": "empty data",
"data": [ ],
"valid": true
},
{
"description": "not more than maxContains",
"data": [ 1 ],
"valid": true
},
{
"description": "too many",
"data": [ 1, 1 ],
"valid": false
}
]
}
]