Skip to content

Commit

Permalink
encoding/jsonschema: constraints don't imply types
Browse files Browse the repository at this point in the history
This was a major bug based on the wrong assumption that
a JSON Schema constraint implies the type for that constraint.
Instead, the contraints only apply if a value is of a certain
type.

To keep diffs to a minimum, and to improve the overal output,
it now explicitly sorts literal
composit types (structs and lists) at the end of a series
of conjunctions.

Change-Id: Ice98a2bc00ae4a68170cd6cd726565a453b1187f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6302
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jun 16, 2020
1 parent 8bfdfe3 commit a91b869
Show file tree
Hide file tree
Showing 16 changed files with 398 additions and 157 deletions.
6 changes: 4 additions & 2 deletions cmd/cue/cmd/testdata/script/def_jsonschema.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ unsupported constraint "foo":
age: twenty

-- expect-stderr2 --
age: conflicting values "twenty" and >=0 (mismatched types string and number):
age: conflicting values "twenty" and (int & >=0) (mismatched types string and int):
./data.yaml:1:7
./schema.json:18:15
./schema.json:19:18
-- expect-stderr3 --
age: conflicting values "twenty" and >=0 (mismatched types string and number):
age: conflicting values "twenty" and (int & >=0) (mismatched types string and int):
./data.yaml:1:7
./schema.json:18:15
./schema.json:19:18
-- cue.mod --
10 changes: 5 additions & 5 deletions cmd/cue/cmd/testdata/script/def_openapi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ info: {
}
#Foo: {
a: int
b: >=0 & <10
b: uint & <10
...
}
-- expect-cue2 --
Expand All @@ -231,7 +231,7 @@ info: {
}
#Foo: {
a: int
b: >=0 & <10
b: uint & <10
...
}
-- expect-cue2 --
Expand All @@ -245,7 +245,7 @@ info: {
}
#Foo: {
a: int
b: >=0 & <10
b: uint & <10
...
}
-- expect-cue3 --
Expand All @@ -262,11 +262,11 @@ info: {
}
#Foo: {
a: int
b: >=0 & <10
b: uint & <10
...
}
#Baz: {
a: int
b: >=0 & <10
b: uint & <10
...
}
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/import_auto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ info: {

#Foo: {
a: int
b: >=0 & <10
b: int & >=0 & <10
...
}
#Bar: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "strings"

#Workflow: {
@jsonschema(schema="http://json-schema.org/draft-07/schema")
number | null | bool | string | [...] | {
null | bool | number | string | [...] | {
// The name of your workflow. GitHub displays the names of your
// workflows on your repository's actions page. If you omit this
// field, GitHub sets the name to the workflow's filename.
Expand Down Expand Up @@ -237,10 +237,9 @@ import "strings"
// commit object. You can retrieve the full commit object using
// the REST API. For more information, see
// https://developer.github.com/v3/repos/commits/#get-a-single-commit.
push?: #ref &
{
{[=~"^(branche|tag|path)s(-ignore)?$" & !~"^()$"]: _}
}
push?: #ref & {
{[=~"^(branche|tag|path)s(-ignore)?$" & !~"^()$"]: _}
}

// Runs your workflow anytime a package is published or updated.
// For more information, see
Expand Down Expand Up @@ -289,7 +288,7 @@ import "strings"
// generate your cron syntax and confirm what time it will run.
// To help you get started, there is also a list of crontab guru
// examples (https://crontab.guru/examples.html).
schedule?: [...number | null | bool | string | [...] | {
schedule?: [...null | bool | number | string | [...] | {
cron?: =~"^(((\\d+,)+\\d+|((\\d+|\\*)\\/\\d+)|(\\d+-\\d+)|\\d+|\\*) ?){5,7}$"
}] & [_, ...]
}
Expand Down Expand Up @@ -320,7 +319,9 @@ import "strings"
// If you do not set a container, all steps will run directly on
// the host specified by runs-on unless a step refers to an
// action configured to run in a container.
container?: [string]: string | #container
container?: {
[string]: string | #container
}

// A map of default settings that will apply to all steps in the
// job.
Expand All @@ -338,7 +339,9 @@ import "strings"

// A map of outputs for a job. Job outputs are available to all
// downstream jobs that depend on this job.
outputs?: [string]: string
outputs?: {
[string]: string
}

// You can use the if conditional to prevent a job from running
// unless a condition is met. You can use any supported context
Expand Down Expand Up @@ -467,7 +470,6 @@ import "strings"
{[=~"^(in|ex)clude$" & !~"^()$"]: [...{
[string]: #configuration
}] & [_, ...]}

{[!~"^(in|ex)clude$" & !~"^()$"]: [...#configuration] & [_, ...]}
}

Expand Down Expand Up @@ -496,7 +498,9 @@ import "strings"
// is automatically mapped to the service name.
// When a step does not use a container action, you must access
// the service using localhost and bind the ports.
services?: [string]: #container
services?: {
[string]: #container
}
}}
}
}
Expand All @@ -505,7 +509,7 @@ import "strings"

#name: =~"^[_a-zA-Z][a-zA-Z0-9_-]*$"

#env: [string]: number | bool | string
#env: [string]: bool | number | string

#architecture: "ARM32" | "x64" | "x86"

Expand Down Expand Up @@ -549,7 +553,7 @@ import "strings"
"working-directory"?: #["working-directory"]
}

#shell: string | ("bash" | "pwsh" | "python" | "sh" | "cmd" | "powershell")
#shell: (string | ("bash" | "pwsh" | "python" | "sh" | "cmd" | "powershell")) & string

#: "working-directory": string

Expand Down

0 comments on commit a91b869

Please sign in to comment.