Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/cmd/validate-test/yaml-with-zed-extension.zed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
schema: |-
definition user {}

definition resource {
relation user: user
permission view = user
}
relationships: >-
resource:1#user@user:1
assertions:
assertTrue:
- "resource:1#user@user:1"
assertFalse:
- "resource:1#user@user:2"
15 changes: 14 additions & 1 deletion internal/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ func validateCmdFunc(cmd *cobra.Command, filenames []string) (string, bool, erro
case decode.FileTypeYaml:
parsed, err = d.UnmarshalYAMLValidationFile()
case decode.FileTypeZed:
if decode.LooksLikeYAMLValidationFile(string(d.Contents)) {
fmt.Fprintf(toPrint, "%sfile %q has a .zed extension but appears to be a YAML validation file.\n"+
" Rename the file to use a .yaml extension, or use --type yaml to override:\n"+
" zed validate %s --type yaml\n\n",
errorPrefix(), filename, filename,
)
shouldExit = true
continue
}
parsed = d.UnmarshalSchemaValidationFile()
default:
parsed, err = d.UnmarshalAsYAMLOrSchema()
Expand Down Expand Up @@ -306,7 +315,11 @@ func outputDeveloperErrorsWithLineOffset(sb *strings.Builder, validateContents [
}

func outputDeveloperError(sb *strings.Builder, devError *devinterface.DeveloperError, lines []string, lineOffset int) {
lineContext := fmt.Sprintf("parse error in `%s`, line %d, column %d:", devError.Context, devError.Line, devError.Column)
errorSource := devError.Context
if len(devError.Path) > 0 && devError.Path[0] != "" {
errorSource = devError.Path[0]
}
lineContext := fmt.Sprintf("parse error in `%s`, line %d, column %d:", errorSource, devError.Line, devError.Column)
fmt.Fprintf(sb, "%s%s %s\n", errorPrefix(), lineContext, errorMessageStyle().Render(devError.Message))
errorLineNumber := int(devError.Line) - 1 + lineOffset // devError.Line is 1-indexed
for i := errorLineNumber - 3; i < errorLineNumber+3; i++ {
Expand Down
19 changes: 13 additions & 6 deletions internal/cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ total files: 2, successfully validated files: 2
},
expectStr: filepath.Join("validate-test", "standard-validation.yaml") + `
Success! - 1 relationships loaded, 2 assertions run, 0 expected relations validated
` + filepath.Join("validate-test", "invalid-schema.zed") + "\nerror: parse error in `something`, line 1, column 1: Unexpected token at root level: TokenTypeIdentifier \n 1 > something something {}\n > ^~~~~~~~~\n 2 | \n\n\n",
` + filepath.Join("validate-test", "invalid-schema.zed") + "\nerror: parse error in `invalid-schema.zed`, line 1, column 1: Unexpected token at root level: TokenTypeIdentifier \n 1 > something something {}\n > ^~~~~~~~~\n 2 | \n\n\n",
expectNonZeroStatusCode: true,
},
`schema_only_passes`: {
Expand All @@ -116,7 +116,7 @@ Success! - 1 relationships loaded, 2 assertions run, 0 expected relations valida
files: []string{
filepath.Join("validate-test", "invalid-schema.zed"),
},
expectStr: "error: parse error in `something`, line 1, column 1: Unexpected token at root level: TokenTypeIdentifier \n 1 > something something {}\n > ^~~~~~~~~\n 2 | \n\n\n",
expectStr: "error: parse error in `invalid-schema.zed`, line 1, column 1: Unexpected token at root level: TokenTypeIdentifier \n 1 > something something {}\n > ^~~~~~~~~\n 2 | \n\n\n",
expectNonZeroStatusCode: true,
},
`without_schema_fails`: {
Expand Down Expand Up @@ -192,7 +192,7 @@ complete - 0 relationships loaded, 0 assertions run, 0 expected relations valida
filepath.Join("validate-test", "missing-relation.zed"),
},
expectNonZeroStatusCode: true,
expectStr: "error: parse error in `write`, line 2, column 21: relation/permission `write` not found under definition `test` \n" +
expectStr: "error: parse error in `missing-relation.zed`, line 2, column 21: relation/permission `write` not found under definition `test` \n" +
" 1 | definition test {\n" +
" 2 > permission view = write\n" +
" > ^~~~~\n " +
Expand All @@ -204,7 +204,7 @@ complete - 0 relationships loaded, 0 assertions run, 0 expected relations valida
filepath.Join("validate-test", "missing-relation.yaml"),
},
expectNonZeroStatusCode: true,
expectStr: "error: parse error in `write`, line 4, column 21: relation/permission `write` not found under definition `test` \n" +
expectStr: "error: parse error in `missing-relation.yaml`, line 4, column 21: relation/permission `write` not found under definition `test` \n" +
" 6 | definition user {}\n" +
" 7 | definition test {\n" +
" 8 | relation viewer: user\n" +
Expand Down Expand Up @@ -268,14 +268,21 @@ complete - 0 relationships loaded, 0 assertions run, 0 expected relations valida
filepath.Join("validate-test", "composable-schema-imports-file-with-error.zed"),
},
expectNonZeroStatusCode: true,
expectStr: "error: parse error in `unknownrel`, line 5, column 23: relation/permission `unknownrel` not found under definition `group` \n 2 | definition user {}\n 3 | \n 4 | definition group {\n 5 > permission view = unknownrel\n > ^~~~~~~~~~\n 6 | }\n 7 | \n\n\n",
expectStr: "error: parse error in `composable-schema-imported-with-error.zed`, line 5, column 23: relation/permission `unknownrel` not found under definition `group` \n 2 | definition user {}\n 3 | \n 4 | definition group {\n 5 > permission view = unknownrel\n > ^~~~~~~~~~\n 6 | }\n 7 | \n\n\n",
},
`yaml_with_composable_schemaFile_with_import_error`: {
files: []string{
filepath.Join("validate-test", "external-composable-with-error.yaml"),
},
expectNonZeroStatusCode: true,
expectStr: "error: parse error in `unknownrel`, line 5, column 23: relation/permission `unknownrel` not found under definition `group` \n 3 | definition group {\n 4 | relation member: user\n 5 | permission view = unknownrel\n 6 > }\n 7 | \n\n\n",
expectStr: "error: parse error in `composable-schema-with-import-error-imported.zed`, line 5, column 23: relation/permission `unknownrel` not found under definition `group` \n 3 | definition group {\n 4 | relation member: user\n 5 | permission view = unknownrel\n 6 > }\n 7 | \n\n\n",
},
`yaml_content_with_zed_extension_gives_hint`: {
files: []string{
filepath.Join("validate-test", "yaml-with-zed-extension.zed"),
},
expectStr: "error: file \"" + filepath.Join("validate-test", "yaml-with-zed-extension.zed") + "\" has a .zed extension but appears to be a YAML validation file.\n Rename the file to use a .yaml extension, or use --type yaml to override:\n zed validate " + filepath.Join("validate-test", "yaml-with-zed-extension.zed") + " --type yaml\n\n",
expectNonZeroStatusCode: true,
},
`yaml_with_schemaFile_escape_attempt_fails`: {
files: []string{
Expand Down
9 changes: 8 additions & 1 deletion internal/decode/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (d *Decoder) UnmarshalYAMLValidationFile() (*validationfile.ValidationFile,
inputString := string(d.Contents)

// Only attempt YAML unmarshaling if the input looks like a YAML validation file.
if !hasYAMLSchemaKey(inputString) && !hasYAMLSchemaFileKey(inputString) && !yamlRelationshipsKeyPattern.MatchString(inputString) {
if !LooksLikeYAMLValidationFile(inputString) {
return nil, fmt.Errorf("%w: input does not appear to be a YAML validation file", ErrInvalidYamlTryZed)
}

Expand Down Expand Up @@ -213,3 +213,10 @@ func hasYAMLSchemaKey(input string) bool {
func hasYAMLSchemaFileKey(input string) bool {
return yamlSchemaFileKeyPattern.MatchString(input)
}

// LooksLikeYAMLValidationFile returns true if the input appears to be a YAML
// validation file based on the presence of top-level YAML keys (schema:,
// schemaFile:, or relationships:).
func LooksLikeYAMLValidationFile(input string) bool {
return hasYAMLSchemaKey(input) || hasYAMLSchemaFileKey(input) || yamlRelationshipsKeyPattern.MatchString(input)
}
Loading