Skip to content

Commit

Permalink
backup: validate typesystem of generated schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Dec 11, 2023
1 parent cbbeaad commit 1e1dcd2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/cmd/backup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -12,6 +13,7 @@ import (
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/spicedb/pkg/schemadsl/compiler"
"github.com/authzed/spicedb/pkg/schemadsl/generator"
"github.com/authzed/spicedb/pkg/typesystem"
"github.com/jzelinskie/cobrautil/v2"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -109,6 +111,23 @@ func filterSchemaDefs(schema, prefix string) (filteredSchema string, err error)
if err != nil {
return "", fmt.Errorf("error generating filtered schema: %w", err)
}

// Validate that the type system for the generated schema is comprehensive.
compiledFilteredSchema, err := compiler.Compile(compiler.InputSchema{Source: "generated-schema", SchemaString: filteredSchema})
if err != nil {
return "", fmt.Errorf("generated invalid schema: %w", err)
}

for _, def := range compiledFilteredSchema.ObjectDefinitions {
ts, err := typesystem.NewNamespaceTypeSystem(def, typesystem.ResolverForSchema(*compiledFilteredSchema))
if err != nil {
return "", fmt.Errorf("generated invalid schema: %w", err)
}
if _, err := ts.Validate(context.Background()); err != nil {
return "", fmt.Errorf("generated invalid schema: %w", err)
}
}

return
}

Expand Down

0 comments on commit 1e1dcd2

Please sign in to comment.