Skip to content

Commit

Permalink
encoding/jsonschema: add keyword TODOs
Browse files Browse the repository at this point in the history
This adds entries for all the known JSON Schema keywords
to the constraints map. This will enable us to distinguish unknown
keywords from unimplemented keywords when implementing
different strictness modes. It also acts as a handy reference
for which features are unimplemented as yet.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I9b2309f92cefaf6152d2ed397af2246e3c9a5aec
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200533
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
rogpeppe committed Sep 3, 2024
1 parent e7175ba commit 33fe828
Show file tree
Hide file tree
Showing 122 changed files with 844 additions and 810 deletions.
40 changes: 34 additions & 6 deletions encoding/jsonschema/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package jsonschema

import (
"fmt"

"cuelang.org/go/cue"
)

Expand Down Expand Up @@ -44,6 +46,9 @@ var constraintMap = map[string]*constraint{}

func init() {
for _, c := range constraints {
if _, ok := constraintMap[c.key]; ok {
panic(fmt.Errorf("duplicate constraint entry for %q", c.key))
}
constraintMap[c.key] = c
}
}
Expand All @@ -54,51 +59,74 @@ func init() {
const numPhases = 5

var constraints = []*constraint{
todo("$anchor", vfrom(VersionDraft2019_09)),
p2d("$comment", constraintComment, vfrom(VersionDraft7)),
p2("$defs", constraintAddDefinitions),
todo("$dynamicAnchor", vfrom(VersionDraft2020_12)),
todo("$dynamicRef", vfrom(VersionDraft2020_12)),
p1d("$id", constraintID, vfrom(VersionDraft6)),
p0("$schema", constraintSchema),
todo("$recursiveAnchor", vbetween(VersionDraft2019_09, VersionDraft2020_12)),
todo("$recursiveRef", vbetween(VersionDraft2019_09, VersionDraft2020_12)),
p2("$ref", constraintRef),
p2("additionalItems", constraintAdditionalItems),
p0("$schema", constraintSchema),
todo("$vocabulary", vfrom(VersionDraft2019_09)),
p2d("additionalItems", constraintAdditionalItems, vto(VersionDraft2019_09)),
p4("additionalProperties", constraintAdditionalProperties),
p3("allOf", constraintAllOf),
p3("anyOf", constraintAnyOf),
p2d("const", constraintConst, vfrom(VersionDraft6)),
p1d("minContains", constraintMinContains, vfrom(VersionDraft2019_09)),
p1d("maxContains", constraintMaxContains, vfrom(VersionDraft2019_09)),
p2d("contains", constraintContains, vfrom(VersionDraft6)),
p2d("contentEncoding", constraintContentEncoding, vfrom(VersionDraft7)),
p2d("contentMediaType", constraintContentMediaType, vfrom(VersionDraft7)),
todo("contentSchema", vfrom(VersionDraft2019_09)),
p2("default", constraintDefault),
p2("definitions", constraintAddDefinitions),
p2("dependencies", constraintDependencies),
todo("dependentRequired", vfrom(VersionDraft2019_09)),
todo("dependentSchemas", vfrom(VersionDraft2019_09)),
p2("deprecated", constraintDeprecated),
p2("description", constraintDescription),
todo("else", vfrom(VersionDraft7)),
p2("enum", constraintEnum),
p2d("examples", constraintExamples, vfrom(VersionDraft6)),
p2("exclusiveMaximum", constraintExclusiveMaximum),
p2("exclusiveMinimum", constraintExclusiveMinimum),
todo("format", allVersions),
p1d("id", constraintID, vto(VersionDraft4)),
todo("if", vfrom(VersionDraft7)),
p2("items", constraintItems),
p2("minItems", constraintMinItems),
p1d("maxContains", constraintMaxContains, vfrom(VersionDraft2019_09)),
p2("maxItems", constraintMaxItems),
p2("maxLength", constraintMaxLength),
p2("maxProperties", constraintMaxProperties),
p3("maximum", constraintMaximum),
p1d("minContains", constraintMinContains, vfrom(VersionDraft2019_09)),
p2("minItems", constraintMinItems),
p2("minLength", constraintMinLength),
todo("minProperties", allVersions),
p3("minimum", constraintMinimum),
p2("multipleOf", constraintMultipleOf),
p3("not", constraintNot),
p3("oneOf", constraintOneOf),
p2("nullable", constraintNullable),
p3("oneOf", constraintOneOf),
p2("pattern", constraintPattern),
p3("patternProperties", constraintPatternProperties),
todo("prefixItems", vfrom(VersionDraft2020_12)),
p2("properties", constraintProperties),
p2d("propertyNames", constraintPropertyNames, vfrom(VersionDraft6)),
todo("readOnly", vfrom(VersionDraft7)),
p3("required", constraintRequired),
todo("then", vfrom(VersionDraft7)),
p2("title", constraintTitle),
p2("type", constraintType),
todo("unevaluatedItems", vfrom(VersionDraft2019_09)),
todo("unevaluatedProperties", vfrom(VersionDraft2019_09)),
p2("uniqueItems", constraintUniqueItems),
todo("writeOnly", vfrom(VersionDraft7)),
}

func todo(name string, versions versionSet) *constraint {
return &constraint{key: name, phase: 1, versions: versions, fn: constraintTODO}
}

func p0(name string, f constraintFunc) *constraint {
Expand Down
6 changes: 6 additions & 0 deletions encoding/jsonschema/constraints_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ func constraintSchema(key string, n cue.Value, s *state) {
s.schemaVersionPresent = true
s.schemaVersion = sv
}

func constraintTODO(key string, n cue.Value, s *state) {
if s.cfg.Strict {
s.errf(n, `keyword %q not yet implemented`, key)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@
"additionalProperties": false
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentSchemas\"",
"v3": "extract error: unsupported constraint \"dependentSchemas\""
"v2": "extract error: keyword \"dependentSchemas\" not yet implemented",
"v3": "extract error: keyword \"dependentSchemas\" not yet implemented"
},
"tests": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
"$ref": "child1#my_anchor"
},
"skip": {
"v2": "extract error: unsupported constraint \"$anchor\" (and 4 more errors)",
"v3": "extract error: unsupported constraint \"$anchor\" (and 4 more errors)"
"v2": "extract error: keyword \"$anchor\" not yet implemented (and 4 more errors)",
"v3": "extract error: keyword \"$anchor\" not yet implemented (and 4 more errors)"
},
"tests": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"if\" (and 1 more errors)",
"v3": "extract error: unsupported constraint \"if\" (and 1 more errors)"
"v2": "extract error: keyword \"if\" not yet implemented (and 1 more errors)",
"v3": "extract error: keyword \"if\" not yet implemented (and 1 more errors)"
},
"tests": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"contentSchema\"",
"v3": "extract error: unsupported constraint \"contentSchema\""
"v2": "extract error: keyword \"contentSchema\" not yet implemented",
"v3": "extract error: keyword \"contentSchema\" not yet implemented"
},
"tests": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentRequired\"",
"v3": "extract error: unsupported constraint \"dependentRequired\""
"v2": "extract error: keyword \"dependentRequired\" not yet implemented",
"v3": "extract error: keyword \"dependentRequired\" not yet implemented"
},
"tests": [
{
Expand Down Expand Up @@ -97,8 +97,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentRequired\"",
"v3": "extract error: unsupported constraint \"dependentRequired\""
"v2": "extract error: keyword \"dependentRequired\" not yet implemented",
"v3": "extract error: keyword \"dependentRequired\" not yet implemented"
},
"tests": [
{
Expand Down Expand Up @@ -144,8 +144,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentRequired\"",
"v3": "extract error: unsupported constraint \"dependentRequired\""
"v2": "extract error: keyword \"dependentRequired\" not yet implemented",
"v3": "extract error: keyword \"dependentRequired\" not yet implemented"
},
"tests": [
{
Expand Down Expand Up @@ -233,8 +233,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentRequired\"",
"v3": "extract error: unsupported constraint \"dependentRequired\""
"v2": "extract error: keyword \"dependentRequired\" not yet implemented",
"v3": "extract error: keyword \"dependentRequired\" not yet implemented"
},
"tests": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentSchemas\"",
"v3": "extract error: unsupported constraint \"dependentSchemas\""
"v2": "extract error: keyword \"dependentSchemas\" not yet implemented",
"v3": "extract error: keyword \"dependentSchemas\" not yet implemented"
},
"tests": [
{
Expand Down Expand Up @@ -121,8 +121,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentSchemas\"",
"v3": "extract error: unsupported constraint \"dependentSchemas\""
"v2": "extract error: keyword \"dependentSchemas\" not yet implemented",
"v3": "extract error: keyword \"dependentSchemas\" not yet implemented"
},
"tests": [
{
Expand Down Expand Up @@ -186,8 +186,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentSchemas\"",
"v3": "extract error: unsupported constraint \"dependentSchemas\""
"v2": "extract error: keyword \"dependentSchemas\" not yet implemented",
"v3": "extract error: keyword \"dependentSchemas\" not yet implemented"
},
"tests": [
{
Expand Down Expand Up @@ -259,8 +259,8 @@
}
},
"skip": {
"v2": "extract error: unsupported constraint \"dependentSchemas\"",
"v3": "extract error: unsupported constraint \"dependentSchemas\""
"v2": "extract error: keyword \"dependentSchemas\" not yet implemented",
"v3": "extract error: keyword \"dependentSchemas\" not yet implemented"
},
"tests": [
{
Expand Down
Loading

0 comments on commit 33fe828

Please sign in to comment.