-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from josephschorr/validate
- Loading branch information
Showing
6 changed files
with
305 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"strings" | ||
|
||
"github.com/jzelinskie/cobrautil" | ||
"github.com/spf13/cobra" | ||
|
||
v0 "github.com/authzed/authzed-go/proto/authzed/api/v0" | ||
"github.com/authzed/spicedb/pkg/development" | ||
"github.com/authzed/spicedb/pkg/tuple" | ||
"github.com/authzed/spicedb/pkg/validationfile" | ||
"github.com/authzed/spicedb/pkg/validationfile/blocks" | ||
"github.com/charmbracelet/lipgloss" | ||
|
||
"github.com/authzed/zed/internal/decode" | ||
) | ||
|
||
var ( | ||
success = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("10")).Render("Success!") | ||
errorPrefix = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("9")).Render("error: ") | ||
errorMessageStyle = lipgloss.NewStyle().Bold(true).Width(80) | ||
linePrefixStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("12")) | ||
highlightedSourceStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9")) | ||
highlightedLineStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9")) | ||
codeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")) | ||
highlightedCodeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("15")) | ||
) | ||
|
||
func registerValidateCmd(rootCmd *cobra.Command) { | ||
rootCmd.AddCommand(validateCmd) | ||
} | ||
|
||
var validateCmd = &cobra.Command{ | ||
Use: "validate <validation_file>", | ||
Short: "validate the given validation file", | ||
Example: ` | ||
From a local file (with prefix): | ||
zed validate file:///Users/zed/Downloads/authzed-x7izWU8_2Gw3.yaml | ||
From a local file (no prefix): | ||
zed validate authzed-x7izWU8_2Gw3.yaml | ||
From a gist: | ||
zed validate https://gist.github.com/ecordell/8e3b613a677e3c844742cf24421c08b6 | ||
From a playground link: | ||
zed validate https://play.authzed.com/s/iksdFvCtvnkR/schema | ||
From pastebin: | ||
zed validate https://pastebin.com/8qU45rVK | ||
From a devtools instance: | ||
zed validate https://localhost:8443/download`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: cobrautil.CommandStack(LogCmdFunc, validateCmdFunc), | ||
} | ||
|
||
func validateCmdFunc(cmd *cobra.Command, args []string) error { | ||
// Parse the URL of the validation document to import. | ||
u, err := url.Parse(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
decoder, err := decode.DecoderForURL(u) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Decode the validation document. | ||
var parsed validationfile.ValidationFile | ||
validateContents, err := decoder(&parsed) | ||
if err != nil { | ||
var errWithSource blocks.ErrorWithSource | ||
if errors.As(err, &errWithSource) { | ||
ouputErrorWithSource(validateContents, errWithSource) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// Create the development context. | ||
ctx := context.Background() | ||
tuples := make([]*v0.RelationTuple, 0, len(parsed.Relationships.Relationships)) | ||
for _, rel := range parsed.Relationships.Relationships { | ||
tuples = append(tuples, tuple.MustFromRelationship(rel)) | ||
} | ||
devCtx, devErrs, err := development.NewDevContext(ctx, &v0.RequestContext{ | ||
Schema: parsed.Schema.Schema, | ||
Relationships: tuples, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
if devErrs != nil { | ||
outputDeveloperErrors(validateContents, devErrs) | ||
} | ||
|
||
// Run assertions. | ||
adevErrs, aerr := development.RunAllAssertions(devCtx, &parsed.Assertions) | ||
if aerr != nil { | ||
return aerr | ||
} | ||
if adevErrs != nil { | ||
outputDeveloperErrors(validateContents, adevErrs) | ||
} | ||
|
||
// Run expected relations. | ||
_, erDevErrs, rerr := development.RunValidation(devCtx, &parsed.ExpectedRelations) | ||
if rerr != nil { | ||
return rerr | ||
} | ||
if erDevErrs != nil { | ||
outputDeveloperErrors(validateContents, erDevErrs) | ||
} | ||
|
||
fmt.Print(success) | ||
fmt.Printf(" - %d relationships loaded, %d assertions run, %d expected relations validated\n", | ||
len(tuples), | ||
len(parsed.Assertions.AssertTrue)+len(parsed.Assertions.AssertFalse), | ||
len(parsed.ExpectedRelations.ValidationMap), | ||
) | ||
return nil | ||
} | ||
|
||
func ouputErrorWithSource(validateContents []byte, errWithSource blocks.ErrorWithSource) { | ||
lines := strings.Split(string(validateContents), "\n") | ||
|
||
fmt.Printf("%s%s\n", errorPrefix, errorMessageStyle.Render(errWithSource.Error())) | ||
errorLineNumber := int(errWithSource.LineNumber) - 1 // errWithSource.LineNumber is 1-indexed | ||
for i := errorLineNumber - 3; i < errorLineNumber+3; i++ { | ||
if i == errorLineNumber { | ||
renderLine(lines, i, errWithSource.Source, errorLineNumber) | ||
} else { | ||
renderLine(lines, i, "", errorLineNumber) | ||
} | ||
} | ||
os.Exit(1) | ||
} | ||
|
||
func outputDeveloperErrors(validateContents []byte, devErrors *development.DeveloperErrors) { | ||
lines := strings.Split(string(validateContents), "\n") | ||
|
||
for _, inputErr := range devErrors.InputErrors { | ||
outputDeveloperError(inputErr, lines) | ||
} | ||
|
||
for _, validationErr := range devErrors.ValidationErrors { | ||
outputDeveloperError(validationErr, lines) | ||
} | ||
|
||
os.Exit(1) | ||
} | ||
|
||
func outputDeveloperError(devError *v0.DeveloperError, lines []string) { | ||
fmt.Printf("%s %s\n", errorPrefix, errorMessageStyle.Render(devError.Message)) | ||
errorLineNumber := int(devError.Line) - 1 // devError.Line is 1-indexed | ||
for i := errorLineNumber - 3; i < errorLineNumber+3; i++ { | ||
if i == errorLineNumber { | ||
renderLine(lines, i, devError.Context, errorLineNumber) | ||
} else { | ||
renderLine(lines, i, "", errorLineNumber) | ||
} | ||
} | ||
|
||
fmt.Printf("\n\n") | ||
} | ||
|
||
func renderLine(lines []string, index int, highlight string, highlightLineIndex int) { | ||
if index < 0 || index >= len(lines) { | ||
return | ||
} | ||
|
||
lineNumberLength := len(fmt.Sprintf("%d", len(lines))) | ||
lineContents := lines[index] | ||
highlightIndex := strings.Index(lineContents, highlight) | ||
lineNumberStr := fmt.Sprintf("%d", index+1) | ||
spacer := strings.Repeat(" ", lineNumberLength) | ||
|
||
lineNumberStyle := linePrefixStyle | ||
lineContentsStyle := codeStyle | ||
if index == highlightLineIndex { | ||
lineNumberStyle = highlightedLineStyle | ||
lineContentsStyle = highlightedCodeStyle | ||
} | ||
|
||
if highlightIndex < 0 || len(highlight) == 0 { | ||
fmt.Printf(" %s | %s\n", lineNumberStyle.Render(lineNumberStr), lineContentsStyle.Render(lineContents)) | ||
} else { | ||
fmt.Printf(" %s | %s%s%s\n", | ||
lineNumberStyle.Render(lineNumberStr), | ||
lineContentsStyle.Render(lineContents[0:highlightIndex]), | ||
highlightedSourceStyle.Render(highlight), | ||
lineContentsStyle.Render(lineContents[highlightIndex+len(highlight):]), | ||
) | ||
fmt.Printf(" %s | %s%s%s\n", | ||
lineNumberStyle.Render(spacer), | ||
strings.Repeat(" ", highlightIndex), | ||
highlightedSourceStyle.Render("^"), | ||
highlightedSourceStyle.Render(strings.Repeat("~", len(highlight)-1)), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.