From 7d58f038c5d2e74e2b54d39b6cccbece11a90b74 Mon Sep 17 00:00:00 2001 From: Torin Sandall Date: Mon, 19 Apr 2021 16:13:28 -0400 Subject: [PATCH] loader: Insert root schema with key 'schema' Previously if there was only one schema it was inserted into the schema set with key 'input'. There was no good reason for this and 'schema' will do. This allows for us to have tighter validation of schema references. Signed-off-by: Torin Sandall --- ast/parser.go | 3 +++ loader/loader.go | 2 +- loader/loader_test.go | 9 +++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ast/parser.go b/ast/parser.go index 31f09b2616..10dc088aaa 100644 --- a/ast/parser.go +++ b/ast/parser.go @@ -1616,6 +1616,9 @@ func (b *metadataParser) Parse() (*Annotations, error) { var errInvalidSchemaRef = fmt.Errorf("invalid schema reference") +// NOTE(tsandall): 'schema' is not registered as a root because it's not +// supported by the compiler or evaluator today. Once we fix that, we can remove +// this function. func parseSchemaRef(s string) (Ref, error) { term, err := ParseTerm(s) diff --git a/loader/loader.go b/loader/loader.go index 970a950edc..605f598c56 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -282,7 +282,7 @@ func loadSchemas(schemaPath string) (*ast.SchemaSet, error) { if err != nil { return nil, err } - ss.Put(ast.InputRootRef, schema) + ss.Put(ast.SchemaRootRef, schema) return ss, nil } diff --git a/loader/loader_test.go b/loader/loader_test.go index 9d52fce9f4..7ee26dcec8 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -765,7 +765,7 @@ func TestSchemas(t *testing.T) { "foo/bar/baz.json": `{"type": "string"}`, }, exp: map[string]string{ - "input": `{"type": "string"}`, + "schema": `{"type": "string"}`, }, }, { @@ -802,7 +802,12 @@ func TestSchemas(t *testing.T) { t.Fatal("unexpected error:", err) } for k, v := range tc.exp { - key := ast.MustParseRef(k) + var key ast.Ref + if k == "schema" { + key = ast.SchemaRootRef.Copy() + } else { + key = ast.MustParseRef(k) + } var schema interface{} util.Unmarshal([]byte(v), &schema) result := ss.Get(key)