feat(cabana): per-request options for plugin field types
- pact.FormFieldType gains RequestOptions, called with the application's database once per form schema request for each type the form uses - the form schema route merges the answer into a copy of each field's typeOptions; a fields.yaml key wins, the compiled schema never changes - an error logs one warning naming the type and serves the fields.yaml options only - TestFieldTypeRequestOptions covers every screen, the precedence, the call count, the error path, unchanged schemas, form extension fields and the authenticated handler; a runnable example backs the docs - the typeOptions description, the regenerated OpenAPI document and schema.d.ts, the field types page and the pact and cabana READMEs document it
This commit is contained in:
@@ -686,6 +686,15 @@ type AdminPartialData interface {
|
||||
// set, turns a stored value into the value the element receives. ListValue,
|
||||
// when set, returns the text a list column of this type shows. Upload, when
|
||||
// set, stores a file the element uploads and returns its URL.
|
||||
//
|
||||
// RequestOptions, when set, adds options that change per request, such as a
|
||||
// site setting the element follows. The admin calls it once per form schema
|
||||
// request for each type the form uses, with the application's database (nil
|
||||
// when none is configured), and merges the answer into every field's
|
||||
// options: an option the field sets in fields.yaml wins. An error is logged
|
||||
// and the schema is served with the fields.yaml options only. The options
|
||||
// reach every administrator who may open the form, so they must hold no
|
||||
// secret.
|
||||
type FormFieldType struct {
|
||||
Name string
|
||||
Element string
|
||||
@@ -698,6 +707,9 @@ type FormFieldType struct {
|
||||
Load func(ctx context.Context, in FormFieldValue) (any, error) `json:"-"`
|
||||
ListValue func(ctx context.Context, value any) string `json:"-"`
|
||||
Upload func(ctx context.Context, in FormFieldUpload) (FormFieldUploadResult, error) `json:"-"`
|
||||
// RequestOptions answers options for one form schema request; nil when
|
||||
// the type has none.
|
||||
RequestOptions func(ctx context.Context, db *gorm.DB) (map[string]any, error) `json:"-"`
|
||||
}
|
||||
|
||||
// FormFieldValue is what the framework hands Decode and Load. DB is the save
|
||||
|
||||
Reference in New Issue
Block a user