Skip to content

Commit

Permalink
encoding/jsonschema: factor format checkers into their own file
Browse files Browse the repository at this point in the history
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
rogpeppe authored and vanhtuan0409 committed Oct 15, 2024
1 parent b4a702c commit e497491
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 66 deletions.
85 changes: 85 additions & 0 deletions encoding/jsonschema/constraints_format.go
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) {}
66 changes: 0 additions & 66 deletions encoding/jsonschema/constraints_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package jsonschema

import (
"sync"

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

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) {}

0 comments on commit e497491

Please sign in to comment.