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