11package cmd
22
33import (
4- "errors"
54 "fmt"
65 "io/fs"
7- "net/url"
86 "os"
97 "path/filepath"
108 "strconv"
@@ -19,7 +17,6 @@ import (
1917 "github.com/authzed/spicedb/pkg/development"
2018 core "github.com/authzed/spicedb/pkg/proto/core/v1"
2119 devinterface "github.com/authzed/spicedb/pkg/proto/developer/v1"
22- "github.com/authzed/spicedb/pkg/validationfile"
2320
2421 "github.com/authzed/zed/internal/commands"
2522 "github.com/authzed/zed/internal/console"
@@ -145,44 +142,11 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
145142 toPrint .WriteString (filename + "\n " )
146143 }
147144
148- u , err := url . Parse (filename )
145+ parsed , contents , err := decode . ValidationFileFromFilename (filename , fileType )
149146 if err != nil {
150147 return "" , true , err
151148 }
152149
153- d , err := decode .DecoderFromURL (u )
154- if err != nil {
155- return "" , true , err
156- }
157- validateContents := d .Contents
158-
159- // Root the filesystem at the directory containing the schema file, so
160- // that relative imports resolve correctly.
161- fileDir := filepath .Dir (filename )
162- if fileDir == "" {
163- fileDir = "."
164- }
165- filesystem := os .DirFS (fileDir )
166-
167- var parsed * validationfile.ValidationFile
168- switch fileType {
169- case decode .FileTypeYaml :
170- parsed , err = d .UnmarshalYAMLValidationFile ()
171- case decode .FileTypeZed :
172- parsed = d .UnmarshalSchemaValidationFile ()
173- default :
174- parsed , err = d .UnmarshalAsYAMLOrSchema ()
175- }
176- // This block handles the error regardless of which case statement is hit
177- if err != nil {
178- return "" , true , err
179- }
180-
181- // Ensure that either schema or schemaFile is present
182- if parsed .Schema .Schema == "" && parsed .SchemaFile == "" {
183- return "" , false , errors .New ("either schema or schemaFile must be present" )
184- }
185-
186150 // This logic will use the zero value of the struct, so we don't need
187151 // to do it conditionally.
188152 tuples := make ([]* core.RelationTuple , 0 )
@@ -193,6 +157,20 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
193157 tuples = append (tuples , rel .ToCoreTuple ())
194158 }
195159
160+ // Root the filesystem at the directory containing the schema file, so
161+ // that relative schema imports resolve correctly.
162+ fileDir := filepath .Dir (filename )
163+ if fileDir == "" {
164+ fileDir = "."
165+ }
166+ if parsed .SchemaFile != "" {
167+ // If the schemaFile is nonempty, we want to make sure that the import
168+ // filesystem is rooted in the dir of that schemafile.
169+ // TODO: it seems like there ought to be a better way to handle this.
170+ fileDir = filepath .Join (fileDir , filepath .Dir (parsed .SchemaFile ))
171+ }
172+ filesystem := os .DirFS (fileDir )
173+
196174 // Create the development context for each run
197175 ctx := cmd .Context ()
198176 devCtx , devErrs , err := development .NewDevContext (ctx , & devinterface.RequestContext {
@@ -204,9 +182,12 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
204182 }
205183 if devErrs != nil {
206184 schemaOffset := parsed .Schema .SourcePosition .LineNumber
185+ if parsed .SchemaFile != "" {
186+ contents = []byte (parsed .Schema .Schema )
187+ }
207188
208189 // Output errors
209- outputDeveloperErrorsWithLineOffset (toPrint , validateContents , devErrs .InputErrors , schemaOffset , filesystem )
190+ outputDeveloperErrorsWithLineOffset (toPrint , contents , devErrs .InputErrors , schemaOffset , filesystem )
210191 return toPrint .String (), true , nil
211192 }
212193 // Run assertions
@@ -215,7 +196,7 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
215196 return "" , false , aerr
216197 }
217198 if adevErrs != nil {
218- outputDeveloperErrors (toPrint , validateContents , adevErrs , filesystem )
199+ outputDeveloperErrors (toPrint , contents , adevErrs , filesystem )
219200 return toPrint .String (), true , nil
220201 }
221202 successfullyValidatedFiles ++
@@ -226,7 +207,7 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
226207 return "" , false , rerr
227208 }
228209 if erDevErrs != nil {
229- outputDeveloperErrors (toPrint , validateContents , erDevErrs , filesystem )
210+ outputDeveloperErrors (toPrint , contents , erDevErrs , filesystem )
230211 return toPrint .String (), true , nil
231212 }
232213 // Print out any warnings for file
@@ -238,7 +219,7 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
238219 if len (warnings ) > 0 {
239220 for _ , warning := range warnings {
240221 fmt .Fprintf (toPrint , "%s%s\n " , warningPrefix (), warning .Message )
241- outputForLine (toPrint , validateContents , uint64 (warning .Line ), warning .SourceCode , uint64 (warning .Column )) // warning.LineNumber is 1-indexed
222+ outputForLine (toPrint , contents , uint64 (warning .Line ), warning .SourceCode , uint64 (warning .Column )) // warning.LineNumber is 1-indexed
242223 toPrint .WriteString ("\n " )
243224 }
244225
0 commit comments