Skip to content

Commit

Permalink
encoding/jsonschema: initial support for format keyword
Browse files Browse the repository at this point in the history
This CL just adds the most basic support for the format keyword, which
is just to ignore it. It does add TODO entries for all the known formats
with their respective versions, so it will be easy to add support for
them in future CLs.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I524547997f4e612a0ed501743192e916fd399900
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201125
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
  • Loading branch information
rogpeppe committed Sep 12, 2024
1 parent f811ee7 commit 9000dc3
Show file tree
Hide file tree
Showing 85 changed files with 2,963 additions and 9,358 deletions.
2 changes: 1 addition & 1 deletion encoding/jsonschema/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var constraints = []*constraint{
p2("exclusiveMaximum", constraintExclusiveMaximum, allVersions|openAPI),
p2("exclusiveMinimum", constraintExclusiveMinimum, allVersions|openAPI),
p1("externalDocs", constraintTODO, openAPI),
p1("format", constraintTODO, allVersions|openAPI),
p1("format", constraintFormat, allVersions|openAPI),
p1("id", constraintID, vto(VersionDraft4)),
p1("if", constraintIf, vfrom(VersionDraft7)),
p2("items", constraintItems, allVersions|openAPI),
Expand Down
65 changes: 65 additions & 0 deletions encoding/jsonschema/constraints_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package jsonschema

import (
"regexp"
"sync"

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
Expand Down Expand Up @@ -61,3 +62,67 @@ func constraintPattern(key string, n cue.Value, s *state) {
}
s.add(n, stringType, &ast.UnaryExpr{Op: token.MAT, X: s.string(n)})
}

type formatFuncInfo struct {
versions versionSet
f func(s *state)
}

var formatFuncs = sync.OnceValue(func() map[string]formatFuncInfo {
return map[string]formatFuncInfo{
"binary": {openAPI, formatTODO},
"byte": {openAPI, formatTODO},
"data": {openAPI, formatTODO},
"date": {vfrom(VersionDraft7), formatTODO},
"date-time ": {allVersions | openAPI, formatTODO},
"double": {openAPI, formatTODO},
"duration": {vfrom(VersionDraft2019_09), formatTODO},
"email": {allVersions | openAPI, formatTODO},
"float": {openAPI, formatTODO},
"hostname": {allVersions | openAPI, formatTODO},
"idn-email": {vfrom(VersionDraft7), formatTODO},
"idn-hostname": {vfrom(VersionDraft7), formatTODO},
"int32": {openAPI, formatTODO},
"int64": {openAPI, formatTODO},
"ipv4": {allVersions | openAPI, formatTODO},
"ipv6": {allVersions | openAPI, formatTODO},
"iri": {vfrom(VersionDraft7), formatTODO},
"iri-reference": {vfrom(VersionDraft7), formatTODO},
"json-pointer": {vfrom(VersionDraft6), formatTODO},
"password": {openAPI, formatTODO},
"regex": {vfrom(VersionDraft7), formatTODO},
"relative-json-pointer": {vfrom(VersionDraft7), formatTODO},
"time": {vfrom(VersionDraft7), formatTODO},
"uri": {allVersions | openAPI, formatTODO},
"uri-reference": {vfrom(VersionDraft6), formatTODO},
"uri-template": {vfrom(VersionDraft6), formatTODO},
"uuid": {vfrom(VersionDraft2019_09), formatTODO},
}
})

func constraintFormat(key string, n cue.Value, s *state) {
formatStr, ok := s.strValue(n)
if !ok {
return
}
finfo, ok := formatFuncs()[formatStr]
if !ok {
// TODO StrictKeywords isn't exactly right here, but in general
// we want unknown formats to be ignored even when StrictFeatures
// is enabled, and StrictKeywords is closest to what we want.
// Perhaps we should have a "lint" mode?
if s.cfg.StrictKeywords {
s.errf(n, "unknown format %q", formatStr)
}
return
}
if !finfo.versions.contains(s.schemaVersion) {
if s.cfg.StrictKeywords {
s.errf(n, "format %q is not recognized in schema version %v", formatStr, s.schemaVersion)
}
return
}
finfo.f(s)
}

func formatTODO(s *state) {}
24 changes: 12 additions & 12 deletions encoding/jsonschema/external_teststats.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# Generated by teststats. DO NOT EDIT
v2:
schema extract (pass / total): 923 / 1363 = 67.7%
tests (pass / total): 3076 / 4803 = 64.0%
tests on extracted schemas (pass / total): 3076 / 3362 = 91.5%
schema extract (pass / total): 993 / 1363 = 72.9%
tests (pass / total): 3515 / 4803 = 73.2%
tests on extracted schemas (pass / total): 3515 / 3801 = 92.5%

v3:
schema extract (pass / total): 911 / 1363 = 66.8%
tests (pass / total): 3037 / 4803 = 63.2%
tests on extracted schemas (pass / total): 3037 / 3318 = 91.5%
schema extract (pass / total): 981 / 1363 = 72.0%
tests (pass / total): 3476 / 4803 = 72.4%
tests on extracted schemas (pass / total): 3476 / 3757 = 92.5%

Optional tests

v2:
schema extract (pass / total): 154 / 274 = 56.2%
tests (pass / total): 391 / 2372 = 16.5%
tests on extracted schemas (pass / total): 391 / 522 = 74.9%
schema extract (pass / total): 230 / 274 = 83.9%
tests (pass / total): 1421 / 2372 = 59.9%
tests on extracted schemas (pass / total): 1421 / 2258 = 62.9%

v3:
schema extract (pass / total): 154 / 274 = 56.2%
tests (pass / total): 381 / 2372 = 16.1%
tests on extracted schemas (pass / total): 381 / 522 = 73.0%
schema extract (pass / total): 230 / 274 = 83.9%
tests (pass / total): 1411 / 2372 = 59.5%
tests on extracted schemas (pass / total): 1411 / 2258 = 62.5%
Loading

0 comments on commit 9000dc3

Please sign in to comment.