diff --git a/encoding/jsonschema/decode_test.go b/encoding/jsonschema/decode_test.go index ffa53bcc263..72c96543312 100644 --- a/encoding/jsonschema/decode_test.go +++ b/encoding/jsonschema/decode_test.go @@ -17,8 +17,10 @@ package jsonschema_test import ( "bytes" "fmt" + "io/fs" "net/url" "path" + "strings" "testing" "github.com/go-quicktest/qt" @@ -62,52 +64,71 @@ func TestDecode(t *testing.T) { cfg.Strict = t.HasTag("strict") ctx := t.Context() - var v cue.Value - - for _, f := range t.Archive.Files { - switch path.Ext(f.Name) { - case ".json": - expr, err := json.Extract(f.Name, f.Data) - if err != nil { - t.Fatal(err) - } - v = ctx.BuildExpr(expr) - case ".yaml": - file, err := yaml.Extract(f.Name, f.Data) - if err != nil { - t.Fatal(err) - } - v = ctx.BuildFile(file) - } + + fsys, err := txtar.FS(t.Archive) + if err != nil { + t.Fatal(err) + } + v, err := readSchema(ctx, fsys) + if err != nil { + t.Fatal(err) + } + if err := v.Err(); err != nil { + t.Fatal(err) } + w := t.Writer("extract") expr, err := jsonschema.Extract(v, cfg) if err != nil { - got := []byte(errors.Details(err, nil)) - got = append(bytes.TrimSpace(got), '\n') - t.Writer("err").Write(got) + got := "ERROR:\n" + errors.Details(err, nil) + w.Write([]byte(strings.TrimSpace(got) + "\n")) + return + } + if expr == nil { + t.Fatal("no expression was extracted") } - if expr != nil { - b, err := format.Node(expr, format.Simplify()) - if err != nil { + b, err := format.Node(expr, format.Simplify()) + if err != nil { + t.Fatal(errors.Details(err, nil)) + } + if !t.HasTag("noverify") { + // Verify the generated CUE. + v := ctx.CompileBytes(b, cue.Filename("generated.cue")) + if err := v.Err(); err != nil { t.Fatal(errors.Details(err, nil)) } + // TODO run some instance verification tests. + } - // verify the generated CUE. - if !t.HasTag("noverify") { - v := ctx.CompileBytes(b, cue.Filename("generated.cue")) - if err := v.Err(); err != nil { - t.Fatal(errors.Details(err, nil)) - } - } + b = append(bytes.TrimSpace(b), '\n') + w.Write(b) - b = append(bytes.TrimSpace(b), '\n') - t.Writer("cue").Write(b) - } }) } +func readSchema(ctx *cue.Context, fsys fs.FS) (cue.Value, error) { + jsonData, jsonErr := fs.ReadFile(fsys, "schema.json") + yamlData, yamlErr := fs.ReadFile(fsys, "schema.yaml") + switch { + case jsonErr == nil && yamlErr == nil: + return cue.Value{}, fmt.Errorf("cannot define both schema.json and schema.yaml") + case jsonErr == nil: + expr, err := json.Extract("schema.json", jsonData) + if err != nil { + return cue.Value{}, err + } + return ctx.BuildExpr(expr), nil + case yamlErr == nil: + file, err := yaml.Extract("schema.yaml", yamlData) + if err != nil { + return cue.Value{}, err + } + return ctx.BuildFile(file), nil + } + return cue.Value{}, fmt.Errorf("no schema.yaml or schema.json file found for test") +} + func TestMapURL(t *testing.T) { v := cuecontext.New().CompileString(` type: "object" diff --git a/encoding/jsonschema/testdata/basic.txtar b/encoding/jsonschema/testdata/basic.txtar index 2951bace25d..0521c5fc65f 100644 --- a/encoding/jsonschema/testdata/basic.txtar +++ b/encoding/jsonschema/testdata/basic.txtar @@ -1,4 +1,4 @@ --- basic.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-07/schema#", @@ -37,7 +37,7 @@ } } --- out/decode/cue -- +-- out/decode/extract -- import "strings" // Main schema diff --git a/encoding/jsonschema/testdata/boolschema.txtar b/encoding/jsonschema/testdata/boolschema.txtar index 46647d25371..6deb8daa9e7 100644 --- a/encoding/jsonschema/testdata/boolschema.txtar +++ b/encoding/jsonschema/testdata/boolschema.txtar @@ -1,4 +1,4 @@ --- type.json -- +-- schema.json -- { "anyOf": [ true, @@ -6,5 +6,5 @@ ] } --- out/decode/cue -- +-- out/decode/extract -- _ | _|_ diff --git a/encoding/jsonschema/testdata/boolschema_oldversion.txtar b/encoding/jsonschema/testdata/boolschema_oldversion.txtar index ed91b6a2c70..33dda9780c6 100644 --- a/encoding/jsonschema/testdata/boolschema_oldversion.txtar +++ b/encoding/jsonschema/testdata/boolschema_oldversion.txtar @@ -1,9 +1,10 @@ --- type.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-04/schema#", "anyOf": [true] } --- out/decode/err -- +-- out/decode/extract -- +ERROR: boolean schemas not supported in http://json-schema.org/draft-04/schema#: - type.json:3:12 + schema.json:3:12 diff --git a/encoding/jsonschema/testdata/boolschemaembed.txtar b/encoding/jsonschema/testdata/boolschemaembed.txtar index d6971c58dfd..02249c9f38e 100644 --- a/encoding/jsonschema/testdata/boolschemaembed.txtar +++ b/encoding/jsonschema/testdata/boolschemaembed.txtar @@ -1,4 +1,4 @@ --- type.json -- +-- schema.json -- { "$defs": { "foo": true @@ -6,7 +6,7 @@ "$ref": "#/$defs/foo", "type": "string" } --- out/decode/cue -- +-- out/decode/extract -- #foo & string #foo: _ diff --git a/encoding/jsonschema/testdata/constarray.txtar b/encoding/jsonschema/testdata/constarray.txtar index 31538a9ca88..8e5eab065e2 100644 --- a/encoding/jsonschema/testdata/constarray.txtar +++ b/encoding/jsonschema/testdata/constarray.txtar @@ -1,4 +1,4 @@ --- type.json -- +-- schema.json -- { "type": "array", "const": [ @@ -6,5 +6,5 @@ ] } --- out/decode/cue -- +-- out/decode/extract -- [1] diff --git a/encoding/jsonschema/testdata/constobj.txtar b/encoding/jsonschema/testdata/constobj.txtar index 008c30101c1..27f9b134147 100644 --- a/encoding/jsonschema/testdata/constobj.txtar +++ b/encoding/jsonschema/testdata/constobj.txtar @@ -1,10 +1,10 @@ --- type.json -- +-- schema.json -- { "type": "object", "const": {"a": 1} } --- out/decode/cue -- +-- out/decode/extract -- a: 1 ... diff --git a/encoding/jsonschema/testdata/def.txtar b/encoding/jsonschema/testdata/def.txtar index 089146d9ef8..2ec0ccdaf6f 100644 --- a/encoding/jsonschema/testdata/def.txtar +++ b/encoding/jsonschema/testdata/def.txtar @@ -1,6 +1,6 @@ // This test tests the conversion and ordering of definitions. --- definition.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-07/schema#", @@ -38,7 +38,7 @@ } } --- out/decode/cue -- +-- out/decode/extract -- @jsonschema(schema="http://json-schema.org/draft-07/schema#") @jsonschema(id="http://cuelang.org/go/encoding/openapi/testdata/order.json") person?: #["per-son"] diff --git a/encoding/jsonschema/testdata/emptyobj.txtar b/encoding/jsonschema/testdata/emptyobj.txtar index 86be8daf9b5..2288fa529da 100644 --- a/encoding/jsonschema/testdata/emptyobj.txtar +++ b/encoding/jsonschema/testdata/emptyobj.txtar @@ -1,7 +1,7 @@ // Objects without properties should convert correctly. // // Issue #734 --- github-workflow.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { @@ -22,7 +22,7 @@ } } } --- out/decode/cue -- +-- out/decode/extract -- @jsonschema(schema="http://json-schema.org/draft-07/schema#") _ diff --git a/encoding/jsonschema/testdata/emptyobjinanyof.txtar b/encoding/jsonschema/testdata/emptyobjinanyof.txtar index abfb318ff37..fb566f4e9a0 100644 --- a/encoding/jsonschema/testdata/emptyobjinanyof.txtar +++ b/encoding/jsonschema/testdata/emptyobjinanyof.txtar @@ -1,4 +1,4 @@ --- emptyanyof.json -- +-- schema.json -- { "$defs": { "shell": { @@ -20,7 +20,7 @@ } } --- out/decode/cue -- +-- out/decode/extract -- _ #shell: string | ("bash" | "sh" | "cmd" | "powershell") diff --git a/encoding/jsonschema/testdata/enumexcluded.txtar b/encoding/jsonschema/testdata/enumexcluded.txtar index 8826045c127..46e9967a9f7 100644 --- a/encoding/jsonschema/testdata/enumexcluded.txtar +++ b/encoding/jsonschema/testdata/enumexcluded.txtar @@ -1,8 +1,9 @@ --- type.json -- +-- schema.json -- { "type": "object", "enum": ["x"] } --- out/decode/err -- +-- out/decode/extract -- +ERROR: constraints are not possible to satisfy: - type.json:1:1 + schema.json:1:1 diff --git a/encoding/jsonschema/testdata/id_in_oneOf.txtar b/encoding/jsonschema/testdata/id_in_oneOf.txtar index 62fd52636e3..e9be88abf50 100644 --- a/encoding/jsonschema/testdata/id_in_oneOf.txtar +++ b/encoding/jsonschema/testdata/id_in_oneOf.txtar @@ -16,7 +16,7 @@ ] } --- out/decode/cue -- +-- out/decode/extract -- @jsonschema(schema="http://json-schema.org/draft-07/schema#") @jsonschema(id="https://test.example/foo") { diff --git a/encoding/jsonschema/testdata/id_scalar.txtar b/encoding/jsonschema/testdata/id_scalar.txtar index ff86e853a2e..9cd747e7628 100644 --- a/encoding/jsonschema/testdata/id_scalar.txtar +++ b/encoding/jsonschema/testdata/id_scalar.txtar @@ -6,7 +6,7 @@ "type": "string" } --- out/decode/cue -- +-- out/decode/extract -- @jsonschema(schema="http://json-schema.org/draft-07/schema#") @jsonschema(id="http://cuelang.org/go/encoding/openapi/testdata/id_scalar.json") string diff --git a/encoding/jsonschema/testdata/impossibleallof.txtar b/encoding/jsonschema/testdata/impossibleallof.txtar index 5e9b8905d08..ed47f1af565 100644 --- a/encoding/jsonschema/testdata/impossibleallof.txtar +++ b/encoding/jsonschema/testdata/impossibleallof.txtar @@ -1,12 +1,13 @@ --- type.json -- +-- schema.json -- { "allOf": [ { "type": "object" }, { "type": "string" } ] } --- out/decode/err -- +-- out/decode/extract -- +ERROR: constraints are not possible to satisfy: - type.json:1:1 + schema.json:1:1 constraints are not possible to satisfy: - type.json:4:9 + schema.json:4:9 diff --git a/encoding/jsonschema/testdata/issue3176.txtar b/encoding/jsonschema/testdata/issue3176.txtar index 51884836449..b9d8348c991 100644 --- a/encoding/jsonschema/testdata/issue3176.txtar +++ b/encoding/jsonschema/testdata/issue3176.txtar @@ -1,4 +1,4 @@ --- definition.json -- +-- schema.json -- { "oneOf": [ { @@ -15,7 +15,7 @@ } } } --- out/decode/cue -- +-- out/decode/extract -- import "strings" ({ diff --git a/encoding/jsonschema/testdata/issue3351.txtar b/encoding/jsonschema/testdata/issue3351.txtar index 43643059752..73b3e9c79e9 100644 --- a/encoding/jsonschema/testdata/issue3351.txtar +++ b/encoding/jsonschema/testdata/issue3351.txtar @@ -1,4 +1,4 @@ --- json-e.json -- +-- schema.json -- { "$schema": "https://json-schema.org/draft/2019-09/schema", "$id": "https://www.sourcemeta.com/schemas/vendor/json-e@1.json", @@ -62,7 +62,7 @@ "title": "JSON-e templates", "type": "object" } --- out/decode/cue -- +-- out/decode/extract -- _schema _schema: { // JSON-e templates diff --git a/encoding/jsonschema/testdata/list.txtar b/encoding/jsonschema/testdata/list.txtar index 36f3fe662e4..bfb7fe9f955 100644 --- a/encoding/jsonschema/testdata/list.txtar +++ b/encoding/jsonschema/testdata/list.txtar @@ -1,4 +1,4 @@ --- list.yaml -- +-- schema.yaml -- type: object properties: @@ -35,7 +35,7 @@ properties: additionalProperties: false --- out/decode/cue -- +-- out/decode/extract -- import "list" foo?: [...string] diff --git a/encoding/jsonschema/testdata/newid_oldversion.txtar b/encoding/jsonschema/testdata/newid_oldversion.txtar index c0775c48f8d..8422aa98359 100644 --- a/encoding/jsonschema/testdata/newid_oldversion.txtar +++ b/encoding/jsonschema/testdata/newid_oldversion.txtar @@ -1,10 +1,11 @@ #strict --- definition.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-04/schema#", "$id": "http://example.test", "type": "string" } --- out/decode/err -- +-- out/decode/extract -- +ERROR: constraint "$id" is not supported in JSON schema version http://json-schema.org/draft-04/schema#: - definition.json:3:3 + schema.json:3:3 diff --git a/encoding/jsonschema/testdata/num.txtar b/encoding/jsonschema/testdata/num.txtar index ed5dbd03943..b3f8041ff0a 100644 --- a/encoding/jsonschema/testdata/num.txtar +++ b/encoding/jsonschema/testdata/num.txtar @@ -1,4 +1,4 @@ --- type.json -- +-- schema.json -- { "type": "object", @@ -37,7 +37,7 @@ }, "additionalProperties": false } --- out/decode/cue -- +-- out/decode/extract -- import ( "strings" "math" diff --git a/encoding/jsonschema/testdata/numericenum.txtar b/encoding/jsonschema/testdata/numericenum.txtar index 9059dfb3fdc..508c0a3cce8 100644 --- a/encoding/jsonschema/testdata/numericenum.txtar +++ b/encoding/jsonschema/testdata/numericenum.txtar @@ -3,10 +3,10 @@ # TODO fix this so it works. --- type.json -- +-- schema.json -- { "type": "number", "enum": [ 1, 2, 3] } --- out/decode/cue -- +-- out/decode/extract -- 1 | 2 | 3 diff --git a/encoding/jsonschema/testdata/object.txtar b/encoding/jsonschema/testdata/object.txtar index 0f791b86966..4777edf78f0 100644 --- a/encoding/jsonschema/testdata/object.txtar +++ b/encoding/jsonschema/testdata/object.txtar @@ -1,4 +1,4 @@ --- type.json -- +-- schema.json -- { "type": "object", "title": "Main schema", @@ -67,7 +67,7 @@ "additionalProperties": false } --- out/decode/cue -- +-- out/decode/extract -- import "struct" // Main schema diff --git a/encoding/jsonschema/testdata/oldid_newversion.txtar b/encoding/jsonschema/testdata/oldid_newversion.txtar index 1f827f9665d..0c7b95c8e98 100644 --- a/encoding/jsonschema/testdata/oldid_newversion.txtar +++ b/encoding/jsonschema/testdata/oldid_newversion.txtar @@ -1,10 +1,11 @@ #strict --- definition.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-07/schema#", "id": "http://example.test", "type": "string" } --- out/decode/err -- +-- out/decode/extract -- +ERROR: constraint "id" is not supported in JSON schema version http://json-schema.org/draft-07/schema#: - definition.json:3:3 + schema.json:3:3 diff --git a/encoding/jsonschema/testdata/openapi.txtar b/encoding/jsonschema/testdata/openapi.txtar index b1f81475b4a..3f1287c4a64 100644 --- a/encoding/jsonschema/testdata/openapi.txtar +++ b/encoding/jsonschema/testdata/openapi.txtar @@ -1,6 +1,6 @@ #openapi --- type.yaml -- +-- schema.yaml -- components: schemas: User: @@ -18,7 +18,7 @@ components: description: "The number to dial." type: string --- out/decode/cue -- +-- out/decode/extract -- // A User uses something. #User: { id?: int diff --git a/encoding/jsonschema/testdata/ref.txtar b/encoding/jsonschema/testdata/ref.txtar index 60ba0e4d85c..c159872585f 100644 --- a/encoding/jsonschema/testdata/ref.txtar +++ b/encoding/jsonschema/testdata/ref.txtar @@ -2,7 +2,7 @@ #noverify --- definition.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-07/schema#", @@ -63,7 +63,7 @@ } } --- out/decode/cue -- +-- out/decode/extract -- import ( "acme.com/external.json:external" "acme.com/external-foo.json:schema" diff --git a/encoding/jsonschema/testdata/refroot.txtar b/encoding/jsonschema/testdata/refroot.txtar index b716b512fde..3811492dba5 100644 --- a/encoding/jsonschema/testdata/refroot.txtar +++ b/encoding/jsonschema/testdata/refroot.txtar @@ -1,6 +1,6 @@ // This test tests the conversion and ordering of $defs. --- definition.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-07/schema#", @@ -12,7 +12,7 @@ } } --- out/decode/cue -- +-- out/decode/extract -- _schema _schema: { @jsonschema(schema="http://json-schema.org/draft-07/schema#") diff --git a/encoding/jsonschema/testdata/refroot2.txtar b/encoding/jsonschema/testdata/refroot2.txtar index 41c696dba28..8fec4af7202 100644 --- a/encoding/jsonschema/testdata/refroot2.txtar +++ b/encoding/jsonschema/testdata/refroot2.txtar @@ -1,6 +1,6 @@ // This test tests the conversion and ordering of $defs. --- definition.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-07/schema#", @@ -10,7 +10,7 @@ } } --- out/decode/cue -- +-- out/decode/extract -- _schema _schema: { @jsonschema(schema="http://json-schema.org/draft-07/schema#") diff --git a/encoding/jsonschema/testdata/required.txtar b/encoding/jsonschema/testdata/required.txtar index 3a45d5e34b7..3c4294cc151 100644 --- a/encoding/jsonschema/testdata/required.txtar +++ b/encoding/jsonschema/testdata/required.txtar @@ -18,7 +18,7 @@ } } --- out/decode/cue -- +-- out/decode/extract -- // example jsonschema @jsonschema(schema="http://json-schema.org/draft-04/schema#") @jsonschema(id="https://example.test/example") diff --git a/encoding/jsonschema/testdata/type.txtar b/encoding/jsonschema/testdata/type.txtar index 2ad36ca6e24..d9384c4ea76 100644 --- a/encoding/jsonschema/testdata/type.txtar +++ b/encoding/jsonschema/testdata/type.txtar @@ -1,4 +1,4 @@ --- type.json -- +-- schema.json -- { "type": "object", "title": "Main schema", @@ -29,7 +29,7 @@ "additionalProperties": false } --- out/decode/cue -- +-- out/decode/extract -- // Main schema intString?: null | bool | int | string | [...] object?: { diff --git a/encoding/jsonschema/testdata/typedis.txtar b/encoding/jsonschema/testdata/typedis.txtar index faf9e8c1313..b276f08c403 100644 --- a/encoding/jsonschema/testdata/typedis.txtar +++ b/encoding/jsonschema/testdata/typedis.txtar @@ -1,4 +1,4 @@ --- type.json -- +-- schema.json -- { "type": "object", "title": "Main schema", @@ -36,7 +36,7 @@ } } } --- out/decode/cue -- +-- out/decode/extract -- // Main schema intOrString1?: int | string intOrString2?: int | string diff --git a/encoding/jsonschema/testdata/typeexcluded.txtar b/encoding/jsonschema/testdata/typeexcluded.txtar index 8f4a92706f1..a2092b05dad 100644 --- a/encoding/jsonschema/testdata/typeexcluded.txtar +++ b/encoding/jsonschema/testdata/typeexcluded.txtar @@ -1,4 +1,4 @@ --- type.json -- +-- schema.json -- { "type": "object", "properties": { @@ -103,7 +103,7 @@ } } } --- out/decode/cue -- +-- out/decode/extract -- import ( "list" "strings" diff --git a/encoding/jsonschema/testdata/unsupported.txtar b/encoding/jsonschema/testdata/unsupported.txtar index f9d6a1f34e0..560bbcf460f 100644 --- a/encoding/jsonschema/testdata/unsupported.txtar +++ b/encoding/jsonschema/testdata/unsupported.txtar @@ -1,4 +1,4 @@ --- unsupported.json -- +-- schema.json -- { "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { @@ -34,7 +34,7 @@ } --- out/decode/cue -- +-- out/decode/extract -- @jsonschema(schema="http://json-schema.org/draft-07/schema#") _ diff --git a/encoding/jsonschema/testdata/used.txtar b/encoding/jsonschema/testdata/used.txtar index 25250346ef5..6ddc5e2291b 100644 --- a/encoding/jsonschema/testdata/used.txtar +++ b/encoding/jsonschema/testdata/used.txtar @@ -1,4 +1,4 @@ --- used.json -- +-- schema.json -- { "$defs": { "enum": { @@ -40,7 +40,7 @@ } } } --- out/decode/cue -- +-- out/decode/extract -- _ #enum: "a" | "b" | "c"