-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Extend Governor APIs] Governor JSON Validator (#74)
* add validator * Update pkg/jsonschema/unique_constraint.go Co-authored-by: E Camden Fisher <[email protected]> * address review suggestions --------- Co-authored-by: E Camden Fisher <[email protected]>
- Loading branch information
Showing
8 changed files
with
645 additions
and
31 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
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,75 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/metal-toolbox/governor-api/internal/models" | ||
"github.com/santhosh-tekuri/jsonschema/v5" | ||
"github.com/volatiletech/sqlboiler/v4/boil" | ||
) | ||
|
||
// Compiler is a struct for a JSON schema compiler | ||
type Compiler struct { | ||
jsonschema.Compiler | ||
|
||
extensionID string | ||
erdSlugPlural string | ||
version string | ||
} | ||
|
||
// Option is a functional configuration option for JSON schema compiler | ||
type Option func(c *Compiler) | ||
|
||
// NewCompiler configures and creates a new JSON schema compiler | ||
func NewCompiler( | ||
extensionID, slugPlural, version string, | ||
opts ...Option, | ||
) *Compiler { | ||
c := &Compiler{*jsonschema.NewCompiler(), extensionID, slugPlural, version} | ||
|
||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
|
||
return c | ||
} | ||
|
||
// WithUniqueConstraint enables the unique constraint extension for a JSON | ||
// schema. An extra `unique` field can be added to the JSON schema, and the | ||
// Validator will ensure that the combination of every properties in the | ||
// array is unique within the given extension resource definition. | ||
// Note that unique constraint validation will be skipped if db is nil. | ||
func WithUniqueConstraint( | ||
ctx context.Context, | ||
extensionResourceDefinition *models.ExtensionResourceDefinition, | ||
resourceID *string, | ||
db boil.ContextExecutor, | ||
) Option { | ||
return func(c *Compiler) { | ||
c.RegisterExtension( | ||
"uniqueConstraint", | ||
JSONSchemaUniqueConstraint, | ||
&UniqueConstraintCompiler{extensionResourceDefinition, resourceID, ctx, db}, | ||
) | ||
} | ||
} | ||
|
||
func (c *Compiler) schemaURL() string { | ||
return fmt.Sprintf( | ||
"https://governor/extensions/%s/erds/%s/%s/schema.json", | ||
c.extensionID, c.erdSlugPlural, c.version, | ||
) | ||
} | ||
|
||
// Compile compiles the schema string | ||
func (c *Compiler) Compile(schema string) (*jsonschema.Schema, error) { | ||
url := c.schemaURL() | ||
|
||
if err := c.AddResource(url, strings.NewReader(schema)); err != nil { | ||
return nil, err | ||
} | ||
|
||
return c.Compiler.Compile(url) | ||
} |
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,3 @@ | ||
// Package jsonschema provides a JSON schema validator that is tailored for | ||
// validations of governor's Extension Resources | ||
package jsonschema |
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,13 @@ | ||
package jsonschema | ||
|
||
import "errors" | ||
|
||
var ( | ||
// ErrInvalidUniqueProperty is returned when the schema's unique property | ||
// is invalid | ||
ErrInvalidUniqueProperty = errors.New(`property "unique" is invalid`) | ||
|
||
// ErrUniqueConstraintViolation is returned when an object violates the unique | ||
// constrain | ||
ErrUniqueConstraintViolation = errors.New("unique constraint violation") | ||
) |
Oops, something went wrong.