Skip to content

Commit

Permalink
loader: Insert root schema with key 'schema'
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tsandall committed Apr 27, 2021
1 parent d3adb90 commit 7d58f03
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Expand Down
9 changes: 7 additions & 2 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`,
},
},
{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7d58f03

Please sign in to comment.