Skip to content

Commit

Permalink
encoding/jsonschema: use uint not number for min/maxLength
Browse files Browse the repository at this point in the history
CUE's number types don't quite match JSON's, so we were
failing when a floating point number was passed to `minLength`
and `maxLength`. Use `decoder.uint` instead of `decoder.number`
which automatically converts to int if needed.

Also, in passing, fix a misleading error message in `constraintMultipleOf`.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ica0bc65b5d8d79d70e5bf526cc8a55b32c3a156a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202309
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
rogpeppe committed Oct 10, 2024
1 parent 66344b2 commit cff7484
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 48 deletions.
2 changes: 1 addition & 1 deletion encoding/jsonschema/constraints_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func constraintMultipleOf(key string, n cue.Value, s *state) {
var x big.Int
_, _ = n.MantExp(&x)
if x.Cmp(big.NewInt(0)) != 1 {
s.errf(n, `"multipleOf" value must be < 0; found %s`, n)
s.errf(n, `"multipleOf" value must be > 0; found %s`, n)
}
math := s.addImport(n, "math")
s.add(n, numType, ast.NewCall(ast.NewSel(math, "MultipleOf"), multiple))
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/constraints_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func constraintContentMediaType(key string, n cue.Value, s *state) {
}

func constraintMaxLength(key string, n cue.Value, s *state) {
max := s.number(n)
max := s.uint(n)
strings := s.addImport(n, "strings")
s.add(n, stringType, ast.NewCall(ast.NewSel(strings, "MaxRunes"), max))
}

func constraintMinLength(key string, n cue.Value, s *state) {
min := s.number(n)
min := s.uint(n)
strings := s.addImport(n, "strings")
s.add(n, stringType, ast.NewCall(ast.NewSel(strings, "MinRunes"), min))
}
Expand Down
8 changes: 4 additions & 4 deletions encoding/jsonschema/external_teststats.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Generated by CUE_UPDATE=1 go test. DO NOT EDIT
v2:
schema extract (pass / total): 993 / 1363 = 72.9%
tests (pass / total): 3517 / 4803 = 73.2%
tests on extracted schemas (pass / total): 3517 / 3801 = 92.5%
tests (pass / total): 3525 / 4803 = 73.4%
tests on extracted schemas (pass / total): 3525 / 3801 = 92.7%

v3:
schema extract (pass / total): 993 / 1363 = 72.9%
tests (pass / total): 3517 / 4803 = 73.2%
tests on extracted schemas (pass / total): 3517 / 3801 = 92.5%
tests (pass / total): 3525 / 4803 = 73.4%
tests on extracted schemas (pass / total): 3525 / 3801 = 92.7%

Optional tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@
{
"description": "shorter is valid",
"data": "f",
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"f\" and [...] (mismatched types string and list):\n generated.cue:4:1\n generated.cue:4:48\n instance.json:1:1\nconflicting values \"f\" and bool (mismatched types string and bool):\n generated.cue:4:1\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"f\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"f\" and number (mismatched types string and number):\n generated.cue:4:1\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"f\" and {...} (mismatched types string and struct):\n generated.cue:4:1\n generated.cue:4:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MaxRunes:\n generated.cue:4:41\n",
"v3": "conflicting values \"f\" and [...] (mismatched types string and list):\n generated.cue:4:48\n instance.json:1:1\nconflicting values \"f\" and bool (mismatched types string and bool):\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"f\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"f\" and number (mismatched types string and number):\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"f\" and {...} (mismatched types string and struct):\n generated.cue:4:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MaxRunes:\n generated.cue:4:41\n"
}
"valid": true
},
{
"description": "too long is invalid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@
{
"description": "longer is valid",
"data": "foo",
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"foo\" and [...] (mismatched types string and list):\n generated.cue:4:1\n generated.cue:4:48\n instance.json:1:1\nconflicting values \"foo\" and bool (mismatched types string and bool):\n generated.cue:4:1\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"foo\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"foo\" and number (mismatched types string and number):\n generated.cue:4:1\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"foo\" and {...} (mismatched types string and struct):\n generated.cue:4:1\n generated.cue:4:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MinRunes:\n generated.cue:4:41\n",
"v3": "conflicting values \"foo\" and [...] (mismatched types string and list):\n generated.cue:4:48\n instance.json:1:1\nconflicting values \"foo\" and bool (mismatched types string and bool):\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"foo\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"foo\" and number (mismatched types string and number):\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"foo\" and {...} (mismatched types string and struct):\n generated.cue:4:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MinRunes:\n generated.cue:4:41\n"
}
"valid": true
},
{
"description": "too short is invalid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@
{
"description": "shorter is valid",
"data": "f",
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"f\" and [...] (mismatched types string and list):\n generated.cue:4:1\n generated.cue:4:48\n instance.json:1:1\nconflicting values \"f\" and bool (mismatched types string and bool):\n generated.cue:4:1\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"f\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"f\" and number (mismatched types string and number):\n generated.cue:4:1\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"f\" and {...} (mismatched types string and struct):\n generated.cue:4:1\n generated.cue:4:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MaxRunes:\n generated.cue:4:41\n",
"v3": "conflicting values \"f\" and [...] (mismatched types string and list):\n generated.cue:4:48\n instance.json:1:1\nconflicting values \"f\" and bool (mismatched types string and bool):\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"f\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"f\" and number (mismatched types string and number):\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"f\" and {...} (mismatched types string and struct):\n generated.cue:4:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MaxRunes:\n generated.cue:4:41\n"
}
"valid": true
},
{
"description": "too long is invalid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@
{
"description": "longer is valid",
"data": "foo",
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"foo\" and [...] (mismatched types string and list):\n generated.cue:4:1\n generated.cue:4:48\n instance.json:1:1\nconflicting values \"foo\" and bool (mismatched types string and bool):\n generated.cue:4:1\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"foo\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"foo\" and number (mismatched types string and number):\n generated.cue:4:1\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"foo\" and {...} (mismatched types string and struct):\n generated.cue:4:1\n generated.cue:4:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MinRunes:\n generated.cue:4:41\n",
"v3": "conflicting values \"foo\" and [...] (mismatched types string and list):\n generated.cue:4:48\n instance.json:1:1\nconflicting values \"foo\" and bool (mismatched types string and bool):\n generated.cue:4:8\n instance.json:1:1\nconflicting values \"foo\" and null (mismatched types string and null):\n generated.cue:4:1\n instance.json:1:1\nconflicting values \"foo\" and number (mismatched types string and number):\n generated.cue:4:15\n instance.json:1:1\nconflicting values \"foo\" and {...} (mismatched types string and struct):\n generated.cue:4:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MinRunes:\n generated.cue:4:41\n"
}
"valid": true
},
{
"description": "too short is invalid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
{
"description": "shorter is valid",
"data": "f",
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"f\" and [...] (mismatched types string and list):\n generated.cue:3:1\n generated.cue:3:48\n instance.json:1:1\nconflicting values \"f\" and bool (mismatched types string and bool):\n generated.cue:3:1\n generated.cue:3:8\n instance.json:1:1\nconflicting values \"f\" and null (mismatched types string and null):\n generated.cue:3:1\n instance.json:1:1\nconflicting values \"f\" and number (mismatched types string and number):\n generated.cue:3:1\n generated.cue:3:15\n instance.json:1:1\nconflicting values \"f\" and {...} (mismatched types string and struct):\n generated.cue:3:1\n generated.cue:3:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MaxRunes:\n generated.cue:3:41\n",
"v3": "conflicting values \"f\" and [...] (mismatched types string and list):\n generated.cue:3:48\n instance.json:1:1\nconflicting values \"f\" and bool (mismatched types string and bool):\n generated.cue:3:8\n instance.json:1:1\nconflicting values \"f\" and null (mismatched types string and null):\n generated.cue:3:1\n instance.json:1:1\nconflicting values \"f\" and number (mismatched types string and number):\n generated.cue:3:15\n instance.json:1:1\nconflicting values \"f\" and {...} (mismatched types string and struct):\n generated.cue:3:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MaxRunes:\n generated.cue:3:41\n"
}
"valid": true
},
{
"description": "too long is invalid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
{
"description": "longer is valid",
"data": "foo",
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"foo\" and [...] (mismatched types string and list):\n generated.cue:3:1\n generated.cue:3:48\n instance.json:1:1\nconflicting values \"foo\" and bool (mismatched types string and bool):\n generated.cue:3:1\n generated.cue:3:8\n instance.json:1:1\nconflicting values \"foo\" and null (mismatched types string and null):\n generated.cue:3:1\n instance.json:1:1\nconflicting values \"foo\" and number (mismatched types string and number):\n generated.cue:3:1\n generated.cue:3:15\n instance.json:1:1\nconflicting values \"foo\" and {...} (mismatched types string and struct):\n generated.cue:3:1\n generated.cue:3:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MinRunes:\n generated.cue:3:41\n",
"v3": "conflicting values \"foo\" and [...] (mismatched types string and list):\n generated.cue:3:48\n instance.json:1:1\nconflicting values \"foo\" and bool (mismatched types string and bool):\n generated.cue:3:8\n instance.json:1:1\nconflicting values \"foo\" and null (mismatched types string and null):\n generated.cue:3:1\n instance.json:1:1\nconflicting values \"foo\" and number (mismatched types string and number):\n generated.cue:3:15\n instance.json:1:1\nconflicting values \"foo\" and {...} (mismatched types string and struct):\n generated.cue:3:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MinRunes:\n generated.cue:3:41\n"
}
"valid": true
},
{
"description": "too short is invalid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
{
"description": "shorter is valid",
"data": "f",
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"f\" and [...] (mismatched types string and list):\n generated.cue:3:1\n generated.cue:3:48\n instance.json:1:1\nconflicting values \"f\" and bool (mismatched types string and bool):\n generated.cue:3:1\n generated.cue:3:8\n instance.json:1:1\nconflicting values \"f\" and null (mismatched types string and null):\n generated.cue:3:1\n instance.json:1:1\nconflicting values \"f\" and number (mismatched types string and number):\n generated.cue:3:1\n generated.cue:3:15\n instance.json:1:1\nconflicting values \"f\" and {...} (mismatched types string and struct):\n generated.cue:3:1\n generated.cue:3:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MaxRunes:\n generated.cue:3:41\n",
"v3": "conflicting values \"f\" and [...] (mismatched types string and list):\n generated.cue:3:48\n instance.json:1:1\nconflicting values \"f\" and bool (mismatched types string and bool):\n generated.cue:3:8\n instance.json:1:1\nconflicting values \"f\" and null (mismatched types string and null):\n generated.cue:3:1\n instance.json:1:1\nconflicting values \"f\" and number (mismatched types string and number):\n generated.cue:3:15\n instance.json:1:1\nconflicting values \"f\" and {...} (mismatched types string and struct):\n generated.cue:3:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MaxRunes:\n generated.cue:3:41\n"
}
"valid": true
},
{
"description": "too long is invalid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
{
"description": "longer is valid",
"data": "foo",
"valid": true,
"skip": {
"v2": "6 errors in empty disjunction:\nconflicting values \"foo\" and [...] (mismatched types string and list):\n generated.cue:3:1\n generated.cue:3:48\n instance.json:1:1\nconflicting values \"foo\" and bool (mismatched types string and bool):\n generated.cue:3:1\n generated.cue:3:8\n instance.json:1:1\nconflicting values \"foo\" and null (mismatched types string and null):\n generated.cue:3:1\n instance.json:1:1\nconflicting values \"foo\" and number (mismatched types string and number):\n generated.cue:3:1\n generated.cue:3:15\n instance.json:1:1\nconflicting values \"foo\" and {...} (mismatched types string and struct):\n generated.cue:3:1\n generated.cue:3:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MinRunes:\n generated.cue:3:41\n",
"v3": "conflicting values \"foo\" and [...] (mismatched types string and list):\n generated.cue:3:48\n instance.json:1:1\nconflicting values \"foo\" and bool (mismatched types string and bool):\n generated.cue:3:8\n instance.json:1:1\nconflicting values \"foo\" and null (mismatched types string and null):\n generated.cue:3:1\n instance.json:1:1\nconflicting values \"foo\" and number (mismatched types string and number):\n generated.cue:3:15\n instance.json:1:1\nconflicting values \"foo\" and {...} (mismatched types string and struct):\n generated.cue:3:56\n instance.json:1:1\ncannot use 2.0 (type float) as int in argument 2 to strings.MinRunes:\n generated.cue:3:41\n"
}
"valid": true
},
{
"description": "too short is invalid",
Expand Down
3 changes: 2 additions & 1 deletion encoding/jsonschema/testdata/txtar/issue3176.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import "strings"

matchN(1, [{
...
}, strings.MaxRunes(3)]) & (string | {
}, strings.MaxRunes(
3)]) & (string | {
{[=~"^x-" & !~"^()$"]: string}
...
})

0 comments on commit cff7484

Please sign in to comment.