Skip to content

Commit c35f470

Browse files
committed
cmd/cue: support different jsonschema strictness modes
This wires up the support for jsonschema strictness modes in internal/filetypes to the actual jsonschema logic. Signed-off-by: Roger Peppe <[email protected]> Change-Id: I215346425aea3afb4145d6aac4052822d5bab0a7 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200924 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 6264e29 commit c35f470

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

Diff for: cmd/cue/cmd/flags.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func addGlobalFlags(f *pflag.FlagSet) {
7979
f.BoolP(string(flagIgnore), "i", false,
8080
"proceed in the presence of errors")
8181
f.Bool(string(flagStrict), false,
82-
"report errors for lossy mappings")
82+
"report errors for lossy mappings (deprecated: use \"jsonschema+strict:\" filetype instead; see cue help filetypes)")
8383
f.BoolP(string(flagVerbose), "v", false,
8484
"print information about progress")
8585
f.BoolP(string(flagAllErrors), "E", false, "print all available errors")

Diff for: cmd/cue/cmd/help.go

+13
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,19 @@ defaults as requested.
609609
graph Like data, but allow references.
610610
schema Export data and definitions.
611611
612+
The following tags are only valid in combination with other tags,
613+
and influence the functioning of the codec. The tag they are
614+
valid with is mentioned in parentheses at the end.
615+
616+
strictFeatures report errors for lossy mappings (jsonschema)
617+
strictKeywords report errors for unknown keywords (jsonschema)
618+
strict report errors for either of the above (jsonschema)
619+
620+
The above flags also accept a boolean flag value (e.g. true, 1, false, 0)
621+
to set them explicitly. For example:
622+
623+
jsonschema+strictKeywords=0+strictFeatures=1
624+
612625
Many commands also support the --out and --outfile/-o flags.
613626
The --out flag specifies the output type using a qualifier
614627
(without the ':'). The -o flag specifies an output file

Diff for: cmd/cue/cmd/testdata/script/def_jsonschema.txtar

+17-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ exec cue def jsonschema: bad.json
1010
! exec cue def jsonschema: bad.json --strict
1111
cmp stderr expect-stderr
1212

13+
! exec cue def jsonschema+strict: bad.json
14+
cmp stderr expect-stderr
15+
16+
# With only strict features, it should error only for the
17+
# unimplemented feature and not for the unknown keyword.
18+
! exec cue def jsonschema+strictFeatures: bad.json
19+
cmp stderr expect-stderr-strict-features
20+
1321
! exec cue export data.yaml schema.json
1422
cmp stderr expect-stderr2
1523

@@ -62,12 +70,19 @@ import "strings"
6270

6371
-- bad.json --
6472
{
73+
"$schema": "https://json-schema.org/draft/2020-12/schema",
6574
"type": "number",
66-
"foo": "bar"
75+
"$dynamicAnchor": "bar",
76+
"foo": true
6777
}
6878
-- expect-stderr --
79+
keyword "$dynamicAnchor" not yet implemented:
80+
./bad.json:4:3
6981
unknown keyword "foo":
70-
./bad.json:3:3
82+
./bad.json:5:3
83+
-- expect-stderr-strict-features --
84+
keyword "$dynamicAnchor" not yet implemented:
85+
./bad.json:4:3
7186
-- data.yaml --
7287
age: twenty
7388

Diff for: cmd/cue/cmd/testdata/script/help_cmd.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Global Flags:
167167
-E, --all-errors print all available errors
168168
-i, --ignore proceed in the presence of errors
169169
-s, --simplify simplify output
170-
--strict report errors for lossy mappings
170+
--strict report errors for lossy mappings (deprecated: use "jsonschema+strict:" filetype instead; see cue help filetypes)
171171
--trace trace computation
172172
-v, --verbose print information about progress
173173

@@ -182,6 +182,6 @@ Global Flags:
182182
-E, --all-errors print all available errors
183183
-i, --ignore proceed in the presence of errors
184184
-s, --simplify simplify output
185-
--strict report errors for lossy mappings
185+
--strict report errors for lossy mappings (deprecated: use "jsonschema+strict:" filetype instead; see cue help filetypes)
186186
--trace trace computation
187187
-v, --verbose print information about progress

Diff for: cmd/cue/cmd/testdata/script/help_cmd_flags.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,6 @@ Global Flags:
145145
-E, --all-errors print all available errors
146146
-i, --ignore proceed in the presence of errors
147147
-s, --simplify simplify output
148-
--strict report errors for lossy mappings
148+
--strict report errors for lossy mappings (deprecated: use "jsonschema+strict:" filetype instead; see cue help filetypes)
149149
--trace trace computation
150150
-v, --verbose print information about progress

Diff for: internal/encoding/encoding.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ type Config struct {
137137
PkgName string // package name for files to generate
138138

139139
Force bool // overwrite existing files
140-
Strict bool
140+
Strict bool // strict mode for jsonschema (deprecated)
141141
Stream bool // potentially write more than one document per file
142142
AllErrors bool
143143

@@ -287,7 +287,15 @@ func jsonSchemaFunc(cfg *Config, f *build.File) interpretFunc {
287287
cfg := &jsonschema.Config{
288288
PkgName: cfg.PkgName,
289289

290-
Strict: cfg.Strict,
290+
// Note: we don't populate Strict because then we'd
291+
// be ignoring the values of the other tags when it's true,
292+
// and there's (deliberately) nothing that Strict does that
293+
// cannot be described by the other two keywords.
294+
// The strictKeywords and strictFeatures tags are
295+
// set by internal/filetypes from the strict tag when appropriate.
296+
297+
StrictKeywords: cfg.Strict || f.BoolTags["strictKeywords"],
298+
StrictFeatures: cfg.Strict || f.BoolTags["strictFeatures"],
291299
}
292300
file, err = jsonschema.Extract(v, cfg)
293301
// TODO: simplify currently erases file line info. Reintroduce after fix.

0 commit comments

Comments
 (0)