feat: initial commit
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good

This commit is contained in:
2023-04-24 20:52:12 +02:00
commit e66938f1d3
134 changed files with 8507 additions and 0 deletions

33
internal/schema/error.go Normal file
View 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
View 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
}

View 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),
}
}