forked from cue-lang/cue
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoding/jsonschema: factor format checkers into their own file
There will be a bunch more format-related functionality to add, and format is not actually string-specific (some format constraints can apply to non-string values too), so factor it into its own file. For cue-lang#3479. Signed-off-by: Roger Peppe <rogpeppe@gmail.com> Change-Id: Ief66a284f0175bacb45c2e1ff8139f52aad19f8a Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202105 Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
- Loading branch information
1 parent
b4a702c
commit e497491
Showing
2 changed files
with
85 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2019 CUE Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package jsonschema | ||
|
||
import ( | ||
"sync" | ||
|
||
"cuelang.org/go/cue" | ||
) | ||
|
||
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters