From c396d74359111f390e93eecbf18e1db0c9cd7c6b Mon Sep 17 00:00:00 2001 From: Kartikay Date: Wed, 15 Jan 2025 02:12:08 +0530 Subject: [PATCH] throw err if schema and schemaFile not specified Signed-off-by: Kartikay --- pkg/validationfile/fileformat.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/validationfile/fileformat.go b/pkg/validationfile/fileformat.go index 026ca4cde9..6e72aa929c 100644 --- a/pkg/validationfile/fileformat.go +++ b/pkg/validationfile/fileformat.go @@ -1,8 +1,12 @@ package validationfile import ( + "errors" + "fmt" + yamlv3 "gopkg.in/yaml.v3" + "github.com/authzed/spicedb/pkg/spiceerrors" "github.com/authzed/spicedb/pkg/validationfile/blocks" ) @@ -43,6 +47,32 @@ type ValidationFile struct { SchemaFile string `yaml:"schemaFile"` } +func (vf *ValidationFile) UnmarshalYAML(node *yamlv3.Node) error { + type alias ValidationFile // To avoid recursion during decoding + temp := &alias{} + if err := node.Decode(temp); err != nil { + return spiceerrors.NewWithSourceError( + fmt.Errorf("error decoding validation file: %w", err), + "validation file", + uint64(node.Line), + uint64(node.Column), + ) + } + + *vf = ValidationFile(*temp) + + // Enforce either one of schema or schemaFile is set. + if vf.Schema == (blocks.ParsedSchema{}) && temp.SchemaFile == "" { + return spiceerrors.NewWithSourceError( + errors.New("validation file must specify either 'schema' or 'schemaFile'"), + "validation file", + uint64(node.Line), + uint64(node.Column), + ) + } + return nil +} + // ParseAssertionsBlock parses the given contents as an assertions block. func ParseAssertionsBlock(contents []byte) (*blocks.Assertions, error) { return blocks.ParseAssertionsBlock(contents)