Skip to content

Commit

Permalink
encoding/jsonschema: add test cases for excluded types
Browse files Browse the repository at this point in the history
The jsonschema logic currently produces "constraint not allowed
because type is excluded" errors. Although such errors
do often represent a mistake on the part of the author
of the schema, they are not technically errors.

A classic example might be the following:

	{
		"enum": ["a", "b"],
		"type": ["string", "null"]
	}

Even though we know that the only values possible
are `"a"` and `"b"` so the `null` type constraints is redundant,
it's OK to include it, and it's common to see this
kind of thing in schemas in the wild.

The test cases were all taken from real-life schemas.

For #393.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I59f46f5f2002ed21e822731d0bce294399f2d892
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199397
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed Aug 13, 2024
1 parent bdf9be8 commit 89c7b4b
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 20 deletions.
20 changes: 0 additions & 20 deletions encoding/jsonschema/testdata/err.txtar

This file was deleted.

128 changes: 128 additions & 0 deletions encoding/jsonschema/testdata/typeexcluded.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
-- type.json --
{
"type": "object",
"properties": {
"e0": {
"type": [
"integer"
],
"minimum": 2,
"maximum": 3,
"maxLength": 5
},
"e1": {
"enum": [
"cover",
"contain"
],
"type": [
"array",
"boolean",
"number",
"object",
"string",
"null"
]
},
"e2": {
"title": "none",
"type": "string",
"enum": [
"none"
],
"required": [
"none"
]
},
"e3": {
"type": "array",
"uniqueItems": true,
"minItems": 1,
"items": {
"type": "string",
"minLength": 1
},
"minLength": 1
},
"e4": {
"type": "array",
"description": "Main authors of the plugin",
"items": {
"type": "string",
"uniqueItems": true
}
},
"e5": {
"type": "integer",
"minimum": 0
},
"e6": {
"items": {
"type": "string",
"pattern": "^[A-Za-z0-9 _.-]+$"
},
"type": "array",
"pattern": "^[A-Za-z0-9 _.-]+$"
},
"e7": {
"type": [
"boolean",
"object"
],
"anyOf": [
{
"type": "boolean",
"enum": [
true,
{}
]
},
{
"type": "object",
"allOf": [
{
"properties": {
"disableFix": {
"type": "boolean"
}
}
}
],
"properties": {
"ignoreMediaFeatureNames": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string"
}
}
}
}
]
}
}
}
-- out/decode/err --
constraint not allowed because type string is excluded:
type.json:10:7
constraint not allowed because type array is excluded:
type.json:18:9
constraint not allowed because type bool is excluded:
type.json:19:9
constraint not allowed because type number is excluded:
type.json:20:9
constraint not allowed because type object is excluded:
type.json:21:9
constraint not allowed because type null is excluded:
type.json:23:9
constraint not allowed because type object is excluded:
type.json:32:7
constraint not allowed because type string is excluded:
type.json:44:7
constraint not allowed because type array is excluded:
type.json:51:9
constraint not allowed because type string is excluded:
type.json:64:7
constraint not allowed because type bool is excluded:
type.json:68:9

0 comments on commit 89c7b4b

Please sign in to comment.