@punica/form-mcp 0.3.1 → 0.4.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -6
- package/bundled/form/define/Form.d.ts +41 -0
- package/bundled/form/define/index.d.ts +2 -0
- package/bundled/form/model/command/service.d.ts +7 -1
- package/bundled/form/model/constants/index.d.ts +4 -0
- package/bundled/form/model/form/index.d.ts +23 -0
- package/bundled/form/model/item/index.d.ts +28 -2
- package/bundled/form/package.json +1 -1
- package/bundled/form/utils/index.d.ts +1 -0
- package/bundled/form/utils/isEqual.d.ts +7 -0
- package/bundled/form-json/conditions.d.ts +8 -0
- package/bundled/form-json/definition/fromJsonSchema.d.ts +22 -0
- package/bundled/form-json/definition/index.d.ts +6 -0
- package/bundled/form-json/definition/limits.d.ts +31 -0
- package/bundled/form-json/definition/schema.d.ts +202 -0
- package/bundled/form-json/definition/types.d.ts +87 -0
- package/bundled/form-json/definition/validate.d.ts +15 -0
- package/bundled/form-json/fromSchema.d.ts +50 -0
- package/bundled/form-json/index.d.ts +4 -0
- package/bundled/form-json/package.json +1 -0
- package/bundled/form-json/rules.d.ts +19 -0
- package/bundled/form-kit/decorators.d.ts +4 -1
- package/bundled/form-kit/json/index.d.ts +33 -0
- package/bundled/form-kit/package.json +1 -1
- package/bundled/form-kit/react/index.d.ts +3 -1
- package/bundled/form-react/index.d.ts +2 -0
- package/bundled/form-react/package.json +1 -1
- package/bundled/form-react/useController.d.ts +15 -0
- package/bundled/form-react/useFormController.d.ts +2 -7
- package/bundled/form-react/useFormState.d.ts +7 -0
- package/bundled/form-service/index.d.ts +6 -0
- package/bundled/form-service/message.d.ts +2 -0
- package/bundled/form-service/package.json +1 -1
- package/bundled/form-service/readValues.d.ts +6 -0
- package/bundled/form-service/submit/index.d.ts +20 -0
- package/bundled/form-service/touch/index.d.ts +13 -0
- package/bundled/form-service/trigger/index.d.ts +3 -0
- package/bundled/form-service/validateItem.d.ts +25 -9
- package/package.json +6 -5
- package/src/create.mjs +78 -3
- package/src/guide.md +75 -4
- package/src/load.mjs +7 -2
- package/src/main.mjs +4 -2
package/README.md
CHANGED
|
@@ -10,12 +10,14 @@ and validates its answer.
|
|
|
10
10
|
|
|
11
11
|
## Tools
|
|
12
12
|
|
|
13
|
-
| Tool
|
|
14
|
-
|
|
|
15
|
-
| `get_guide`
|
|
16
|
-
| `search_api`
|
|
17
|
-
| `get_api`
|
|
18
|
-
| `validate_code`
|
|
13
|
+
| Tool | What it does |
|
|
14
|
+
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
15
|
+
| `get_guide` | The rules: what the packages provide and what your application builds, the lifecycle, services, events and a complete example |
|
|
16
|
+
| `search_api` | Finds exports of the Punica Form packages (core, services, React hooks, form kit) by name or documentation |
|
|
17
|
+
| `get_api` | The exact TypeScript declaration and documentation of one or more exports |
|
|
18
|
+
| `validate_code` | Checks code for imports that do not exist, wrong service names and service calls that do not exist; each problem with a fix |
|
|
19
|
+
| `get_form_schema` | The format of a form described as data (`@punica/form-json`): its JSON Schema, the types, rules and conditions |
|
|
20
|
+
| `validate_form` | Checks a form definition before it is rendered: each problem with its path. Also turns a flat JSON Schema into a form definition |
|
|
19
21
|
|
|
20
22
|
Resources: `punica-form://guide` and `punica-form://api/<name>`.
|
|
21
23
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { FormClassDecorator, FormPropertyDecorator } from './decorator';
|
|
2
|
+
/** A property of a form defined with `defineForm`. */
|
|
3
|
+
export type FormField<T> = {
|
|
4
|
+
/** The value a new form starts with. */
|
|
5
|
+
value: T;
|
|
6
|
+
decorators: FormPropertyDecorator[];
|
|
7
|
+
};
|
|
8
|
+
/** The entity of a form defined with `defineForm`. */
|
|
9
|
+
export type FormValues<S extends Record<string, FormField<unknown>>> = {
|
|
10
|
+
-readonly [K in keyof S]: S[K]['value'];
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* A property of a form defined with `defineForm`: the value it starts with
|
|
14
|
+
* and the decorators a form class would have on the property.
|
|
15
|
+
*
|
|
16
|
+
* field('', Text({ label: 'Name' }))
|
|
17
|
+
* field<number | undefined>(undefined, NumberField({ label: 'Age' }))
|
|
18
|
+
*/
|
|
19
|
+
export declare const field: <T>(value: T, ...decorators: FormPropertyDecorator[]) => FormField<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Defines a form class without decorator syntax, for projects whose build
|
|
22
|
+
* does not compile decorators. The class is what a decorated class would
|
|
23
|
+
* be: give its instances to `FormController.fromEntity`.
|
|
24
|
+
*
|
|
25
|
+
* const UserForm = defineForm(
|
|
26
|
+
* { name: field('', Text({ label: 'Name' })) },
|
|
27
|
+
* Services([GetValues, SetValue])
|
|
28
|
+
* );
|
|
29
|
+
* type User = InstanceType<typeof UserForm>;
|
|
30
|
+
*
|
|
31
|
+
* const controller = await FormController.fromEntity(new UserForm());
|
|
32
|
+
* Decorators are applied like the ones written above a class or a property:
|
|
33
|
+
* the last one first. The value of a field is read once, when the form is
|
|
34
|
+
* defined, and copied for every instance; give values that differ between
|
|
35
|
+
* instances, such as the current date, to the constructor.
|
|
36
|
+
* @param fields - The properties of the form, in the order of its items
|
|
37
|
+
* @param decorators - What a form class would have above it: `Services`, `Readers`, …
|
|
38
|
+
* @returns A class whose constructor takes values that replace the initial
|
|
39
|
+
* ones; a value that is `undefined` leaves the initial one in place
|
|
40
|
+
*/
|
|
41
|
+
export declare const defineForm: <S extends Record<string, FormField<unknown>>>(fields: S, ...decorators: FormClassDecorator[]) => new (values?: Partial<FormValues<S>>) => FormValues<S>;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export type { FormClassDecorator, FormPropertyDecorator } from './decorator';
|
|
2
|
+
export type { FormField, FormValues } from './Form';
|
|
3
|
+
export { defineForm, field } from './Form';
|
|
2
4
|
export { default as defineFormItem } from './FormItem';
|
|
3
5
|
export { default as defineFormItemProperty } from './FormItemProperty';
|
|
4
6
|
export { default as defineFormProperty } from './FormProperty';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CommandItem, Form, FormEventMap, FormItem } from '..';
|
|
1
|
+
import { CommandItem, Form, FormEventMap, FormItem, FormStatePatch } from '..';
|
|
2
2
|
/**
|
|
3
3
|
* What a service receives when it is initialized: the only way to read and
|
|
4
4
|
* change the state of the form.
|
|
@@ -15,8 +15,14 @@ export type CommandService<E, F extends FormItem<E>> = {
|
|
|
15
15
|
/**
|
|
16
16
|
* Replaces the items with the same properties. The form gets a new
|
|
17
17
|
* reference and UPDATE_FORM is fired. Unknown properties are ignored.
|
|
18
|
+
* `dirty` of the items is set by comparing their values with the initial ones.
|
|
18
19
|
*/
|
|
19
20
|
setItems: (items: Array<FormItem<E>>) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Changes the state of the form as a whole. The form gets a new reference
|
|
23
|
+
* and UPDATE_FORM is fired, unless nothing changed.
|
|
24
|
+
*/
|
|
25
|
+
setState: (state: FormStatePatch) => void;
|
|
20
26
|
/**
|
|
21
27
|
* Marks the pending asynchronous writes of a property, or of the whole
|
|
22
28
|
* form when no property is given, as superseded by a newer one.
|
|
@@ -9,6 +9,10 @@ export type FormEventMap<E, F extends FormItem<E>> = {
|
|
|
9
9
|
RESET: Form<E, F>;
|
|
10
10
|
/** A value was set: the changed item, then the items its control returned. */
|
|
11
11
|
UPDATE_ITEM: F[];
|
|
12
|
+
/** The form was submitted: the values the submit handler received. */
|
|
13
|
+
SUBMIT: E;
|
|
14
|
+
/** A submit failed: what the validation or the submit handler threw. */
|
|
15
|
+
SUBMIT_ERROR: unknown;
|
|
12
16
|
/** The registered form item types. Fired at the beginning of start(). */
|
|
13
17
|
REGISTER_ITEMS: string[];
|
|
14
18
|
};
|
|
@@ -1,10 +1,33 @@
|
|
|
1
1
|
import { FormItem, IReader, IService, IStarter } from '../..';
|
|
2
|
+
/**
|
|
3
|
+
* What a form knows about itself as a whole. A started form always has it.
|
|
4
|
+
*/
|
|
5
|
+
export type FormState = {
|
|
6
|
+
/** Whether the value of an item differs from the one the form was started with. */
|
|
7
|
+
dirty: boolean;
|
|
8
|
+
/** Whether the user has left a field. */
|
|
9
|
+
touched: boolean;
|
|
10
|
+
/** Whether no item is in error. */
|
|
11
|
+
valid: boolean;
|
|
12
|
+
/** Whether an item is being validated. */
|
|
13
|
+
validating: boolean;
|
|
14
|
+
/** Whether the form is being submitted. */
|
|
15
|
+
submitting: boolean;
|
|
16
|
+
/** How many times a submit was attempted. */
|
|
17
|
+
submitCount: number;
|
|
18
|
+
/** Errors of the form as a whole, such as a rejected submit. */
|
|
19
|
+
errors: string[];
|
|
20
|
+
};
|
|
21
|
+
/** The part of the state services write; the rest follows from the items. */
|
|
22
|
+
export type FormStatePatch = Partial<Pick<FormState, 'submitting' | 'submitCount' | 'errors'>>;
|
|
2
23
|
/**
|
|
3
24
|
* The state of a form. Readers and starters may attach their own properties
|
|
4
25
|
* (a title, a layout, …), which are read back as `unknown`.
|
|
5
26
|
*/
|
|
6
27
|
export type Form<E, F extends FormItem<E> = FormItem<E>> = {
|
|
7
28
|
items: F[];
|
|
29
|
+
/** Set when the form is started and kept up to date after every change. */
|
|
30
|
+
state?: FormState;
|
|
8
31
|
/** Service classes (one instance per form) or instances. */
|
|
9
32
|
services?: Array<IService | (new () => IService)>;
|
|
10
33
|
readers?: Array<IReader<E, F>>;
|
|
@@ -20,6 +20,18 @@ export type FormItem<E> = {
|
|
|
20
20
|
required?: boolean;
|
|
21
21
|
disabled?: boolean;
|
|
22
22
|
hidden?: boolean;
|
|
23
|
+
/** Whether the value differs from the one the form was started with. */
|
|
24
|
+
dirty?: boolean;
|
|
25
|
+
/** Whether the user has left the field. Set by the `Touch` service. */
|
|
26
|
+
touched?: boolean;
|
|
27
|
+
/** Whether the item is being validated. */
|
|
28
|
+
validating?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Milliseconds the `Trigger` service waits before it validates the item.
|
|
31
|
+
* A validation triggered again meanwhile starts the wait anew, so a
|
|
32
|
+
* validation that asks a server runs once the user stops typing.
|
|
33
|
+
*/
|
|
34
|
+
validateDebounce?: number;
|
|
23
35
|
/** Whether the last validation of the item failed. */
|
|
24
36
|
error?: boolean;
|
|
25
37
|
/** The messages of the last failed validation. */
|
|
@@ -28,8 +40,12 @@ export type FormItem<E> = {
|
|
|
28
40
|
initialize?(command: CommandItem<E, FormItem<E>>): Promise<Array<FormItem<E>>>;
|
|
29
41
|
/** Runs after the value of the item was set; the returned items are written to the form. */
|
|
30
42
|
control?(command: CommandItem<E, FormItem<E>>): Promise<Array<FormItem<E>>>;
|
|
31
|
-
/**
|
|
32
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Validates the item. The services pass a context whose signal is aborted
|
|
45
|
+
* when the result is not needed any more; give it to `fetch` to cancel
|
|
46
|
+
* the request: `fetch(url, { signal: context?.signal })`.
|
|
47
|
+
*/
|
|
48
|
+
validation?(command: CommandItem<E, FormItem<E>>, context?: ValidationContext): Promise<ErrorDetail>;
|
|
33
49
|
};
|
|
34
50
|
/**
|
|
35
51
|
* A form item whose `value` has the type of its property. Use it as the item
|
|
@@ -45,6 +61,16 @@ export type TypedFormItem<E, F extends FormItem<E> = FormItem<E>> = {
|
|
|
45
61
|
value?: E[K];
|
|
46
62
|
};
|
|
47
63
|
}[keyof E];
|
|
64
|
+
/**
|
|
65
|
+
* What a validation function receives besides its command.
|
|
66
|
+
*/
|
|
67
|
+
export type ValidationContext = {
|
|
68
|
+
/**
|
|
69
|
+
* Aborted when the item is validated again, changed or reset while the
|
|
70
|
+
* validation is running.
|
|
71
|
+
*/
|
|
72
|
+
signal: AbortSignal;
|
|
73
|
+
};
|
|
48
74
|
/**
|
|
49
75
|
* The result of a validation.
|
|
50
76
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"2.
|
|
1
|
+
{"version":"2.2.0-next.0"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether two values are deeply equal. Objects, arrays, dates, regular
|
|
3
|
+
* expressions, maps and sets are compared by their content, circular
|
|
4
|
+
* references included. Objects without own properties that are not plain,
|
|
5
|
+
* such as a `File`, are only equal to themselves.
|
|
6
|
+
*/
|
|
7
|
+
export declare const isEqual: (a: unknown, b: unknown, seen?: WeakMap<object, WeakSet<object>>) => boolean;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Condition } from './definition';
|
|
2
|
+
/** The properties whose values a condition reads. */
|
|
3
|
+
export declare const propertiesOf: (condition: Condition) => string[];
|
|
4
|
+
/**
|
|
5
|
+
* Whether a condition holds.
|
|
6
|
+
* @param valueOf - The current value of a property
|
|
7
|
+
*/
|
|
8
|
+
export declare const holds: (condition: Condition, valueOf: (property: string) => unknown) => boolean;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { FormDefinition } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Turns a JSON Schema into a form definition. It takes the flat schemas a
|
|
4
|
+
* form can show: an object whose properties are strings, numbers, booleans
|
|
5
|
+
* or choices, as the elicitation requests of the Model Context Protocol are.
|
|
6
|
+
*
|
|
7
|
+
* | JSON Schema | Form definition |
|
|
8
|
+
* |---|---|
|
|
9
|
+
* | `string` | `text`; `format` `email`, `uri` and `date` pick how it is entered |
|
|
10
|
+
* | `number`, `integer` | `number` |
|
|
11
|
+
* | `boolean` | `checkbox` |
|
|
12
|
+
* | `enum`, `oneOf` of constants | `select` |
|
|
13
|
+
* | `title`, `description`, `default` | `label`, `help`, `value` |
|
|
14
|
+
* | `required`, `minLength`, `maxLength`, `pattern`, `minimum`, `maximum`, `format: email`, `integer` | rules |
|
|
15
|
+
*
|
|
16
|
+
* A required boolean is not a box that must be ticked: `false` is an answer,
|
|
17
|
+
* so it only starts as `false`.
|
|
18
|
+
*
|
|
19
|
+
* Check the result with `validateDefinition` before it is used.
|
|
20
|
+
* @throws When the schema is not an object schema, or a property has a type a form cannot show
|
|
21
|
+
*/
|
|
22
|
+
export declare const fromJsonSchema: (schema: unknown) => FormDefinition;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './types';
|
|
2
|
+
export { BUILT_IN_RULES, LIMITS, RESERVED_PROPS } from './limits';
|
|
3
|
+
export { formDefinitionSchema } from './schema';
|
|
4
|
+
export { validateDefinition } from './validate';
|
|
5
|
+
export type { ValidateDefinitionOptions } from './validate';
|
|
6
|
+
export { fromJsonSchema } from './fromJsonSchema';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** How large a form definition may be. It may come from an untrusted source. */
|
|
2
|
+
export declare const LIMITS: {
|
|
3
|
+
/** Items of a form. */
|
|
4
|
+
readonly items: 200;
|
|
5
|
+
/** Choices of an item. */
|
|
6
|
+
readonly options: 500;
|
|
7
|
+
/** Rules of an item. */
|
|
8
|
+
readonly rules: 20;
|
|
9
|
+
/** Characters of a property, a type or a rule name. */
|
|
10
|
+
readonly name: 100;
|
|
11
|
+
/** Characters of a text. */
|
|
12
|
+
readonly text: 2000;
|
|
13
|
+
/** Characters of the expression of a `pattern` rule. */
|
|
14
|
+
readonly pattern: 500;
|
|
15
|
+
/** How deep conditions and values may be nested. */
|
|
16
|
+
readonly depth: 6;
|
|
17
|
+
};
|
|
18
|
+
/** The names of the rules that are built in. */
|
|
19
|
+
export declare const BUILT_IN_RULES: readonly ["required", "email", "integer", "min", "max", "minLength", "maxLength", "pattern"];
|
|
20
|
+
/**
|
|
21
|
+
* What a form item is made of. `props` of a definition cannot hold these:
|
|
22
|
+
* they belong to the engine, and some of them (`path`, `validation`) decide
|
|
23
|
+
* where values are written and what runs.
|
|
24
|
+
*/
|
|
25
|
+
export declare const RESERVED_PROPS: readonly ["property", "path", "label", "type", "value", "required", "disabled", "hidden", "dirty", "touched", "validating", "validateDebounce", "error", "errorMessages", "initialize", "control", "validation", "help", "placeholder", "options"];
|
|
26
|
+
/**
|
|
27
|
+
* Whether a regular expression repeats a group that itself repeats, such as
|
|
28
|
+
* `(a+)+`: the shape that can take forever on a value that does not match.
|
|
29
|
+
* It is a coarse check, not a proof that an expression is fast.
|
|
30
|
+
*/
|
|
31
|
+
export declare const isSlowPattern: (pattern: string) => boolean;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JSON Schema (draft 2020-12) of a form definition. Give it to a model
|
|
3
|
+
* as the schema of its output; check what comes back with
|
|
4
|
+
* `validateDefinition`, which also checks what a schema cannot express.
|
|
5
|
+
*/
|
|
6
|
+
export declare const formDefinitionSchema: {
|
|
7
|
+
readonly $schema: "https://json-schema.org/draft/2020-12/schema";
|
|
8
|
+
readonly $id: "https://form.punicalab.com/schema/form-definition-1.json";
|
|
9
|
+
readonly title: "Punica Form definition";
|
|
10
|
+
readonly type: "object";
|
|
11
|
+
readonly additionalProperties: false;
|
|
12
|
+
readonly required: readonly ["version", "items"];
|
|
13
|
+
readonly properties: {
|
|
14
|
+
readonly version: {
|
|
15
|
+
readonly const: 1;
|
|
16
|
+
};
|
|
17
|
+
readonly name: {
|
|
18
|
+
readonly type: "string";
|
|
19
|
+
readonly maxLength: 2000;
|
|
20
|
+
};
|
|
21
|
+
readonly title: {
|
|
22
|
+
readonly type: "string";
|
|
23
|
+
readonly maxLength: 2000;
|
|
24
|
+
};
|
|
25
|
+
readonly description: {
|
|
26
|
+
readonly type: "string";
|
|
27
|
+
readonly maxLength: 2000;
|
|
28
|
+
};
|
|
29
|
+
readonly columns: {
|
|
30
|
+
readonly enum: readonly [1, 2, 3, 4];
|
|
31
|
+
};
|
|
32
|
+
readonly items: {
|
|
33
|
+
readonly type: "array";
|
|
34
|
+
readonly maxItems: 200;
|
|
35
|
+
readonly items: {
|
|
36
|
+
readonly $ref: "#/$defs/item";
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
readonly $defs: {
|
|
41
|
+
readonly item: {
|
|
42
|
+
readonly type: "object";
|
|
43
|
+
readonly additionalProperties: false;
|
|
44
|
+
readonly required: readonly ["property", "type", "label"];
|
|
45
|
+
readonly properties: {
|
|
46
|
+
readonly property: {
|
|
47
|
+
readonly type: "string";
|
|
48
|
+
readonly minLength: 1;
|
|
49
|
+
readonly maxLength: 100;
|
|
50
|
+
};
|
|
51
|
+
readonly type: {
|
|
52
|
+
readonly type: "string";
|
|
53
|
+
readonly minLength: 1;
|
|
54
|
+
readonly maxLength: 100;
|
|
55
|
+
};
|
|
56
|
+
readonly label: {
|
|
57
|
+
readonly type: "string";
|
|
58
|
+
readonly maxLength: 2000;
|
|
59
|
+
};
|
|
60
|
+
readonly value: {};
|
|
61
|
+
readonly help: {
|
|
62
|
+
readonly type: "string";
|
|
63
|
+
readonly maxLength: 2000;
|
|
64
|
+
};
|
|
65
|
+
readonly placeholder: {
|
|
66
|
+
readonly type: "string";
|
|
67
|
+
readonly maxLength: 2000;
|
|
68
|
+
};
|
|
69
|
+
readonly disabled: {
|
|
70
|
+
readonly type: "boolean";
|
|
71
|
+
};
|
|
72
|
+
readonly hidden: {
|
|
73
|
+
readonly type: "boolean";
|
|
74
|
+
};
|
|
75
|
+
readonly options: {
|
|
76
|
+
readonly type: "array";
|
|
77
|
+
readonly maxItems: 500;
|
|
78
|
+
readonly items: {
|
|
79
|
+
readonly anyOf: readonly [{
|
|
80
|
+
readonly type: "string";
|
|
81
|
+
readonly maxLength: 2000;
|
|
82
|
+
}, {
|
|
83
|
+
readonly type: "object";
|
|
84
|
+
readonly additionalProperties: false;
|
|
85
|
+
readonly required: readonly ["value", "label"];
|
|
86
|
+
readonly properties: {
|
|
87
|
+
readonly value: {
|
|
88
|
+
readonly type: "string";
|
|
89
|
+
readonly maxLength: 2000;
|
|
90
|
+
};
|
|
91
|
+
readonly label: {
|
|
92
|
+
readonly type: "string";
|
|
93
|
+
readonly maxLength: 2000;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
}];
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
readonly rules: {
|
|
100
|
+
readonly type: "array";
|
|
101
|
+
readonly maxItems: 20;
|
|
102
|
+
readonly items: {
|
|
103
|
+
readonly $ref: "#/$defs/rule";
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
readonly visibleWhen: {
|
|
107
|
+
readonly $ref: "#/$defs/condition";
|
|
108
|
+
};
|
|
109
|
+
readonly disabledWhen: {
|
|
110
|
+
readonly $ref: "#/$defs/condition";
|
|
111
|
+
};
|
|
112
|
+
readonly props: {
|
|
113
|
+
readonly type: "object";
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
readonly rule: {
|
|
118
|
+
readonly type: "object";
|
|
119
|
+
readonly additionalProperties: false;
|
|
120
|
+
readonly required: readonly ["rule"];
|
|
121
|
+
readonly properties: {
|
|
122
|
+
readonly rule: {
|
|
123
|
+
readonly type: "string";
|
|
124
|
+
readonly minLength: 1;
|
|
125
|
+
readonly maxLength: 100;
|
|
126
|
+
};
|
|
127
|
+
readonly value: {};
|
|
128
|
+
readonly message: {
|
|
129
|
+
readonly type: "string";
|
|
130
|
+
readonly maxLength: 2000;
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
readonly condition: {
|
|
135
|
+
readonly anyOf: readonly [{
|
|
136
|
+
readonly type: "object";
|
|
137
|
+
readonly additionalProperties: false;
|
|
138
|
+
readonly required: readonly ["property", "equals"];
|
|
139
|
+
readonly properties: {
|
|
140
|
+
readonly property: {
|
|
141
|
+
readonly type: "string";
|
|
142
|
+
readonly minLength: 1;
|
|
143
|
+
readonly maxLength: 100;
|
|
144
|
+
};
|
|
145
|
+
readonly equals: {};
|
|
146
|
+
};
|
|
147
|
+
}, {
|
|
148
|
+
readonly type: "object";
|
|
149
|
+
readonly additionalProperties: false;
|
|
150
|
+
readonly required: readonly ["property", "notEquals"];
|
|
151
|
+
readonly properties: {
|
|
152
|
+
readonly property: {
|
|
153
|
+
readonly type: "string";
|
|
154
|
+
readonly minLength: 1;
|
|
155
|
+
readonly maxLength: 100;
|
|
156
|
+
};
|
|
157
|
+
readonly notEquals: {};
|
|
158
|
+
};
|
|
159
|
+
}, {
|
|
160
|
+
readonly type: "object";
|
|
161
|
+
readonly additionalProperties: false;
|
|
162
|
+
readonly required: readonly ["property", "in"];
|
|
163
|
+
readonly properties: {
|
|
164
|
+
readonly property: {
|
|
165
|
+
readonly type: "string";
|
|
166
|
+
readonly minLength: 1;
|
|
167
|
+
readonly maxLength: 100;
|
|
168
|
+
};
|
|
169
|
+
readonly in: {
|
|
170
|
+
readonly type: "array";
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
}, {
|
|
174
|
+
readonly type: "object";
|
|
175
|
+
readonly additionalProperties: false;
|
|
176
|
+
readonly required: readonly ["all"];
|
|
177
|
+
readonly properties: {
|
|
178
|
+
readonly all: {
|
|
179
|
+
readonly type: "array";
|
|
180
|
+
readonly minItems: 1;
|
|
181
|
+
readonly items: {
|
|
182
|
+
readonly $ref: "#/$defs/condition";
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
}, {
|
|
187
|
+
readonly type: "object";
|
|
188
|
+
readonly additionalProperties: false;
|
|
189
|
+
readonly required: readonly ["any"];
|
|
190
|
+
readonly properties: {
|
|
191
|
+
readonly any: {
|
|
192
|
+
readonly type: "array";
|
|
193
|
+
readonly minItems: 1;
|
|
194
|
+
readonly items: {
|
|
195
|
+
readonly $ref: "#/$defs/condition";
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
}];
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** A value JSON can hold. */
|
|
2
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
3
|
+
[key: string]: JsonValue;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Whether something holds for the values of the form: a comparison of the
|
|
7
|
+
* value of one item, or several conditions combined.
|
|
8
|
+
*/
|
|
9
|
+
export type Condition = {
|
|
10
|
+
property: string;
|
|
11
|
+
equals: JsonValue;
|
|
12
|
+
} | {
|
|
13
|
+
property: string;
|
|
14
|
+
notEquals: JsonValue;
|
|
15
|
+
} | {
|
|
16
|
+
property: string;
|
|
17
|
+
in: JsonValue[];
|
|
18
|
+
} | {
|
|
19
|
+
all: Condition[];
|
|
20
|
+
} | {
|
|
21
|
+
any: Condition[];
|
|
22
|
+
};
|
|
23
|
+
/** A validation rule of an item, by its name. */
|
|
24
|
+
export type RuleDefinition = {
|
|
25
|
+
/** `required`, `email`, `integer`, `min`, `max`, `minLength`, `maxLength`, `pattern`, or a rule of the application. */
|
|
26
|
+
rule: string;
|
|
27
|
+
/** What the rule compares with: the limit of `min`, the expression of `pattern`, … */
|
|
28
|
+
value?: JsonValue;
|
|
29
|
+
/** The message in place of the default one. */
|
|
30
|
+
message?: string;
|
|
31
|
+
};
|
|
32
|
+
/** A choice of an item: its value, or its value and the text to show. */
|
|
33
|
+
export type OptionDefinition = string | {
|
|
34
|
+
value: string;
|
|
35
|
+
label: string;
|
|
36
|
+
};
|
|
37
|
+
/** An item of a form definition. */
|
|
38
|
+
export type ItemDefinition = {
|
|
39
|
+
/** The key of the value in the values of the form. */
|
|
40
|
+
property: string;
|
|
41
|
+
/** The kind of field, one of the types the application renders. */
|
|
42
|
+
type: string;
|
|
43
|
+
label: string;
|
|
44
|
+
/** The value the form starts with. */
|
|
45
|
+
value?: JsonValue;
|
|
46
|
+
/** Text under the field. */
|
|
47
|
+
help?: string;
|
|
48
|
+
/** Shown while the field is empty. */
|
|
49
|
+
placeholder?: string;
|
|
50
|
+
disabled?: boolean;
|
|
51
|
+
hidden?: boolean;
|
|
52
|
+
/** The choices, for types that have them. */
|
|
53
|
+
options?: OptionDefinition[];
|
|
54
|
+
rules?: RuleDefinition[];
|
|
55
|
+
/** The item is hidden unless this holds. */
|
|
56
|
+
visibleWhen?: Condition;
|
|
57
|
+
/** The item is disabled while this holds. */
|
|
58
|
+
disabledWhen?: Condition;
|
|
59
|
+
/** What the component of the type takes besides: `rows`, `span`, … */
|
|
60
|
+
props?: {
|
|
61
|
+
[key: string]: JsonValue;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* A form as data. It holds no code: rules and conditions are named and
|
|
66
|
+
* declared, so a definition can come from an untrusted source such as a
|
|
67
|
+
* language model.
|
|
68
|
+
*/
|
|
69
|
+
export type FormDefinition = {
|
|
70
|
+
/** The version of this format. */
|
|
71
|
+
version: 1;
|
|
72
|
+
/** Identifies the form in developer tools. */
|
|
73
|
+
name?: string;
|
|
74
|
+
/** The heading of the form. */
|
|
75
|
+
title?: string;
|
|
76
|
+
/** Text under the heading. */
|
|
77
|
+
description?: string;
|
|
78
|
+
/** Columns the fields are laid out in. */
|
|
79
|
+
columns?: 1 | 2 | 3 | 4;
|
|
80
|
+
items: ItemDefinition[];
|
|
81
|
+
};
|
|
82
|
+
/** A problem of a form definition. */
|
|
83
|
+
export type DefinitionProblem = {
|
|
84
|
+
/** Where it is, such as `items[2].rules[0].value`. */
|
|
85
|
+
path: string;
|
|
86
|
+
message: string;
|
|
87
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DefinitionProblem } from './types';
|
|
2
|
+
export type ValidateDefinitionOptions = {
|
|
3
|
+
/** The types an item may have. Any type is accepted without it. */
|
|
4
|
+
types?: readonly string[];
|
|
5
|
+
/** The rules of the application, besides the built-in ones. */
|
|
6
|
+
rules?: readonly string[];
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Checks a form definition, whatever it is. Besides its shape this checks
|
|
10
|
+
* what makes a definition safe and usable: it holds nothing but data, its
|
|
11
|
+
* properties are unique, its conditions point to items that exist, its rules
|
|
12
|
+
* are known and its size is within the limits.
|
|
13
|
+
* @returns The problems of the definition; none when it is valid
|
|
14
|
+
*/
|
|
15
|
+
export declare const validateDefinition: (input: unknown, options?: ValidateDefinitionOptions) => DefinitionProblem[];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { FormController, FormItem, FormItemRegister, IService } from '@punica/form';
|
|
2
|
+
import { DefinitionProblem, OptionDefinition } from './definition';
|
|
3
|
+
import { Rule } from './rules';
|
|
4
|
+
/** The values of a form made from a definition, by the properties of its items. */
|
|
5
|
+
export type JsonFormValues = Record<string, unknown>;
|
|
6
|
+
/** An item of a form made from a definition. */
|
|
7
|
+
export type JsonFormItem = FormItem<JsonFormValues> & {
|
|
8
|
+
help?: string;
|
|
9
|
+
placeholder?: string;
|
|
10
|
+
options?: OptionDefinition[];
|
|
11
|
+
/** The `props` of the definition are properties of the item. */
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
};
|
|
14
|
+
export type FromSchemaOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* The types a definition may use, and the form item type each one stands
|
|
17
|
+
* for: `{ text: 'kit:text' }`. Without it a definition may use the types
|
|
18
|
+
* of the register, by their own names.
|
|
19
|
+
*/
|
|
20
|
+
types?: Record<string, string>;
|
|
21
|
+
/** Rules of the application, by the names definitions use them with. */
|
|
22
|
+
rules?: Record<string, Rule>;
|
|
23
|
+
/**
|
|
24
|
+
* Services in addition to GetValues, SetValue, Validate, Trigger, Reset,
|
|
25
|
+
* Touch and Submit.
|
|
26
|
+
*/
|
|
27
|
+
services?: Array<IService | (new () => IService)>;
|
|
28
|
+
/** The register whose form item types the form reports. */
|
|
29
|
+
register?: FormItemRegister;
|
|
30
|
+
/** Identifies the form in developer tools; the name of the definition by default. */
|
|
31
|
+
name?: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Thrown for a definition that is not valid. `problems` says what is wrong
|
|
35
|
+
* and where.
|
|
36
|
+
*/
|
|
37
|
+
export declare class FormDefinitionError extends Error {
|
|
38
|
+
readonly problems: DefinitionProblem[];
|
|
39
|
+
constructor(problems: DefinitionProblem[]);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Creates the controller of a form that is described as data. The
|
|
43
|
+
* definition is checked first; whatever it is, nothing of it is executed.
|
|
44
|
+
* Start the controller as usual, or give this function to `useController`.
|
|
45
|
+
*
|
|
46
|
+
* const controller = await fromSchema(definition, { types: { text: 'app:text' } });
|
|
47
|
+
* await controller.start();
|
|
48
|
+
* @throws {FormDefinitionError} When the definition is not valid
|
|
49
|
+
*/
|
|
50
|
+
export declare const fromSchema: (definition: unknown, options?: FromSchemaOptions) => Promise<FormController<JsonFormValues, JsonFormItem>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"0.1.0-next.0"}
|