feat: initial commit
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
This commit is contained in:
33
internal/schema/error.go
Normal file
33
internal/schema/error.go
Normal file
@ -0,0 +1,33 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/qri-io/jsonschema"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrSchemaNotFound = errors.New("schema not found")
|
||||
ErrInvalidData = errors.New("invalid data")
|
||||
)
|
||||
|
||||
type InvalidDataError struct {
|
||||
keyErrors []jsonschema.KeyError
|
||||
}
|
||||
|
||||
func (e *InvalidDataError) Is(err error) bool {
|
||||
return err == ErrInvalidData
|
||||
}
|
||||
|
||||
func (e *InvalidDataError) Error() string {
|
||||
return fmt.Sprintf("%s: %s", ErrInvalidData.Error(), e.keyErrors)
|
||||
}
|
||||
|
||||
func (e *InvalidDataError) KeyErrors() []jsonschema.KeyError {
|
||||
return e.keyErrors
|
||||
}
|
||||
|
||||
func NewInvalidDataError(keyErrors ...jsonschema.KeyError) *InvalidDataError {
|
||||
return &InvalidDataError{keyErrors}
|
||||
}
|
17
internal/schema/load.go
Normal file
17
internal/schema/load.go
Normal file
@ -0,0 +1,17 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/qri-io/jsonschema"
|
||||
)
|
||||
|
||||
func Parse(data []byte) (*jsonschema.Schema, error) {
|
||||
var schema jsonschema.Schema
|
||||
if err := json.Unmarshal(data, &schema); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return &schema, nil
|
||||
}
|
56
internal/schema/registry.go
Normal file
56
internal/schema/registry.go
Normal file
@ -0,0 +1,56 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/qri-io/jsonschema"
|
||||
)
|
||||
|
||||
var defaultRegistry = NewRegistry()
|
||||
|
||||
func RegisterLayerOptionsSchema(layerType store.LayerType, schema *jsonschema.Schema) {
|
||||
defaultRegistry.RegisterLayerOptionsSchema(layerType, schema)
|
||||
}
|
||||
|
||||
func ValidateLayerOptions(ctx context.Context, layerType store.LayerType, options *store.LayerOptions) error {
|
||||
if err := defaultRegistry.ValidateLayerOptions(ctx, layerType, options); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Registry struct {
|
||||
layerOptionSchemas map[store.LayerType]*jsonschema.Schema
|
||||
}
|
||||
|
||||
func (r *Registry) RegisterLayerOptionsSchema(layerType store.LayerType, schema *jsonschema.Schema) {
|
||||
r.layerOptionSchemas[layerType] = schema
|
||||
}
|
||||
|
||||
func (r *Registry) ValidateLayerOptions(ctx context.Context, layerType store.LayerType, options *store.LayerOptions) error {
|
||||
schema, exists := r.layerOptionSchemas[layerType]
|
||||
if !exists {
|
||||
return errors.WithStack(ErrSchemaNotFound)
|
||||
}
|
||||
|
||||
rawOptions := func(opts *store.LayerOptions) map[string]any {
|
||||
return *opts
|
||||
}(options)
|
||||
|
||||
state := schema.Validate(ctx, rawOptions)
|
||||
|
||||
if len(*state.Errs) > 0 {
|
||||
return errors.WithStack(NewInvalidDataError(*state.Errs...))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{
|
||||
layerOptionSchemas: make(map[store.LayerType]*jsonschema.Schema),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user