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
Dispatch-Trailer: {"type":"trybot","CL":1201125,"patchset":2,"ref":"refs/changes/25/1201125/2","targetBranch":"master"}
  • Loading branch information
rogpeppe authored and cueckoo committed Sep 12, 2024
1 parent 6002948 commit c0ce4d0
Show file tree
Hide file tree
Showing 85 changed files with 2,959 additions and 9,352 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
67 changes: 67 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,69 @@ 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},
}
})

// 2019 changed "hostname" "idn-hostname"

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) {}
12 changes: 6 additions & 6 deletions encoding/jsonschema/external_teststats.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Generated by teststats. DO NOT EDIT
v2:
schema extract (pass / total): 1077 / 1637 = 65.8%
tests (pass / total): 3467 / 7175 = 48.3%
tests on extracted schemas (pass / total): 3467 / 3884 = 89.3%
schema extract (pass / total): 1223 / 1637 = 74.7%
tests (pass / total): 4936 / 7175 = 68.8%
tests on extracted schemas (pass / total): 4936 / 6059 = 81.5%

v3:
schema extract (pass / total): 1065 / 1637 = 65.1%
tests (pass / total): 3418 / 7175 = 47.6%
tests on extracted schemas (pass / total): 3418 / 3840 = 89.0%
schema extract (pass / total): 1211 / 1637 = 74.0%
tests (pass / total): 4887 / 7175 = 68.1%
tests on extracted schemas (pass / total): 4887 / 6015 = 81.2%
Loading

0 comments on commit c0ce4d0

Please sign in to comment.