@reckona/mreact-forms 0.0.160 → 0.0.162
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/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/standard-schema.d.ts +6 -0
- package/dist/standard-schema.d.ts.map +1 -1
- package/dist/standard-schema.js +1 -0
- package/dist/standard-schema.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +24 -0
- package/src/standard-schema.ts +6 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { type ReadonlyCell } from "@reckona/mreact-reactive-core";
|
|
2
2
|
import { type StandardSchemaV1 } from "./standard-schema.js";
|
|
3
|
+
/** Represents the object shape managed by a form instance. */
|
|
3
4
|
export type FormValues = Record<string, unknown>;
|
|
5
|
+
/** Extracts string field names from a form value object. */
|
|
4
6
|
export type FieldName<TValues extends FormValues> = Extract<keyof TValues, string>;
|
|
7
|
+
/** Maps field names and the root form key to validation error messages. */
|
|
5
8
|
export type FormErrors<TValues extends FormValues> = Partial<Record<FieldName<TValues> | "root", string[]>>;
|
|
9
|
+
/** Validates one field value against the full form values object. */
|
|
6
10
|
export type FieldValidator<TValue, TValues extends FormValues> = (value: TValue, values: TValues) => readonly string[] | string | undefined | Promise<readonly string[] | string | undefined>;
|
|
11
|
+
/** Names the form events that can trigger validation. */
|
|
7
12
|
export type FormValidateMode = "change" | "blur" | "submit";
|
|
13
|
+
/** Describes the complete reactive state tracked by a form instance. */
|
|
8
14
|
export interface FormState<TValues extends FormValues> {
|
|
9
15
|
dirty: boolean;
|
|
10
16
|
errors: FormErrors<TValues>;
|
|
@@ -16,6 +22,7 @@ export interface FormState<TValues extends FormValues> {
|
|
|
16
22
|
validating: Partial<Record<FieldName<TValues>, boolean>>;
|
|
17
23
|
values: TValues;
|
|
18
24
|
}
|
|
25
|
+
/** Describes the derived state for one form field. */
|
|
19
26
|
export interface FieldState<TValue> {
|
|
20
27
|
dirty: boolean;
|
|
21
28
|
errors: string[];
|
|
@@ -23,15 +30,18 @@ export interface FieldState<TValue> {
|
|
|
23
30
|
validating: boolean;
|
|
24
31
|
value: TValue;
|
|
25
32
|
}
|
|
33
|
+
/** Provides DOM event handlers and current value for binding a field to an input. */
|
|
26
34
|
export interface FieldBinding<TValue> {
|
|
27
35
|
onBlur(event: Event): Promise<void>;
|
|
28
36
|
onChange(event: Event): Promise<void>;
|
|
29
37
|
onInput(event: Event): Promise<void>;
|
|
30
38
|
value: TValue;
|
|
31
39
|
}
|
|
40
|
+
/** Configures which DOM event updates a field binding. */
|
|
32
41
|
export interface FieldBindingOptions {
|
|
33
42
|
event?: "change" | "input" | undefined;
|
|
34
43
|
}
|
|
44
|
+
/** Exposes state, binding, blur, and value update controls for one field. */
|
|
35
45
|
export interface FieldApi<TValues extends FormValues, Name extends FieldName<TValues>> {
|
|
36
46
|
readonly state: ReadonlyCell<FieldState<TValues[Name]>>;
|
|
37
47
|
bind(options?: FieldBindingOptions): FieldBinding<TValues[Name]>;
|
|
@@ -45,13 +55,17 @@ interface BaseCreateFormOptions<TValues extends FormValues> {
|
|
|
45
55
|
}> | undefined;
|
|
46
56
|
validateOn?: FormValidateMode | readonly FormValidateMode[] | undefined;
|
|
47
57
|
}
|
|
58
|
+
/** Configures form creation without schema-level submit value transformation. */
|
|
48
59
|
export interface CreateFormOptionsWithoutSchema<TValues extends FormValues> extends BaseCreateFormOptions<TValues> {
|
|
49
60
|
schema?: undefined;
|
|
50
61
|
}
|
|
62
|
+
/** Configures form creation with a Standard Schema validator that may transform submit values. */
|
|
51
63
|
export interface CreateFormOptionsWithSchema<TValues extends FormValues, TSubmitValues> extends BaseCreateFormOptions<TValues> {
|
|
52
64
|
schema: StandardSchemaV1<TValues, TSubmitValues>;
|
|
53
65
|
}
|
|
66
|
+
/** Configures form creation with optional field validators and optional Standard Schema validation. */
|
|
54
67
|
export type CreateFormOptions<TValues extends FormValues, TSubmitValues = TValues> = CreateFormOptionsWithoutSchema<TValues> | CreateFormOptionsWithSchema<TValues, TSubmitValues>;
|
|
68
|
+
/** Reports either successful normalized submit values or form validation errors. */
|
|
55
69
|
export type FormValidationResult<TValues extends FormValues, TSubmitValues> = {
|
|
56
70
|
success: true;
|
|
57
71
|
value: TSubmitValues;
|
|
@@ -59,6 +73,7 @@ export type FormValidationResult<TValues extends FormValues, TSubmitValues> = {
|
|
|
59
73
|
errors: FormErrors<TValues>;
|
|
60
74
|
success: false;
|
|
61
75
|
};
|
|
76
|
+
/** Reports the result of a form submit handler after validation and error capture. */
|
|
62
77
|
export type FormSubmitResult<TValues extends FormValues, TResult> = {
|
|
63
78
|
data: TResult;
|
|
64
79
|
status: "success";
|
|
@@ -71,10 +86,12 @@ export type FormSubmitResult<TValues extends FormValues, TResult> = {
|
|
|
71
86
|
error: unknown;
|
|
72
87
|
status: "error";
|
|
73
88
|
};
|
|
89
|
+
/** Represents field and form errors returned by a server action. */
|
|
74
90
|
export interface ServerActionErrors<TValues extends FormValues> {
|
|
75
91
|
fieldErrors?: Partial<Record<FieldName<TValues>, readonly string[]>> | undefined;
|
|
76
92
|
formErrors?: readonly string[] | undefined;
|
|
77
93
|
}
|
|
94
|
+
/** Provides reactive form state, field access, validation, submit, reset, and error controls. */
|
|
78
95
|
export interface FormApi<TValues extends FormValues, TSubmitValues> {
|
|
79
96
|
readonly state: ReadonlyCell<FormState<TValues>>;
|
|
80
97
|
field<Name extends FieldName<TValues>>(name: Name): FieldApi<TValues, Name>;
|
|
@@ -86,8 +103,11 @@ export interface FormApi<TValues extends FormValues, TSubmitValues> {
|
|
|
86
103
|
submit<TResult>(handler: (values: TSubmitValues) => Promise<TResult> | TResult): Promise<FormSubmitResult<TValues, TResult>>;
|
|
87
104
|
validate(): Promise<FormValidationResult<TValues, TSubmitValues>>;
|
|
88
105
|
}
|
|
106
|
+
/** Creates a reactive form API from initial values, validators, and optional schema validation. */
|
|
89
107
|
export declare function createForm<TValues extends FormValues>(options: CreateFormOptionsWithoutSchema<TValues>): FormApi<TValues, TValues>;
|
|
90
108
|
export declare function createForm<TValues extends FormValues, TSubmitValues>(options: CreateFormOptionsWithSchema<TValues, TSubmitValues>): FormApi<TValues, TSubmitValues>;
|
|
109
|
+
/** Re-exports Standard Schema types used by form schema options. */
|
|
91
110
|
export type { InferStandardSchemaInput, InferStandardSchemaOutput, StandardSchemaV1, } from "./standard-schema.js";
|
|
111
|
+
/** Validates an unknown value with a Standard Schema and normalizes the result shape. */
|
|
92
112
|
export { validateStandardSchema } from "./standard-schema.js";
|
|
93
113
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,YAAY,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAE,KAAK,gBAAgB,EAA0B,MAAM,sBAAsB,CAAC;AAErF,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,YAAY,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAE,KAAK,gBAAgB,EAA0B,MAAM,sBAAsB,CAAC;AAErF,8DAA8D;AAC9D,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEjD,4DAA4D;AAC5D,MAAM,MAAM,SAAS,CAAC,OAAO,SAAS,UAAU,IAAI,OAAO,CAAC,MAAM,OAAO,EAAE,MAAM,CAAC,CAAC;AAEnF,2EAA2E;AAC3E,MAAM,MAAM,UAAU,CAAC,OAAO,SAAS,UAAU,IAAI,OAAO,CAC1D,MAAM,CAAC,SAAS,CAA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V,wBAAwB,EACxB,yBAAyB,EACzB,gBAAgB,GACjB,MAAM,sBAAsB,CAAC;AAC9B,yFAAyF;AACzF,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -306,5 +306,6 @@ function normalizeFieldErrors(errors) {
|
|
|
306
306
|
function cloneValues(values) {
|
|
307
307
|
return { ...values };
|
|
308
308
|
}
|
|
309
|
+
/** Validates an unknown value with a Standard Schema and normalizes the result shape. */
|
|
309
310
|
export { validateStandardSchema } from "./standard-schema.js";
|
|
310
311
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAqB,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAyB,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AA+HrF,MAAM,UAAU,UAAU,CACxB,OAAkD;IAElD,MAAM,UAAU,GAAG,IAAI,GAAG,CACxB,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,IAAI,QAAQ,CAAC,CAC1F,CAAC;IACF,IAAI,aAAa,GAAG,WAAW,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IACvD,MAAM,KAAK,GAAG,IAAI,CAAqB;QACrC,KAAK,EAAE,KAAK;QACZ,MAAM,EAAE,EAAE;QACV,aAAa;QACb,WAAW,EAAE,CAAC;QACd,UAAU,EAAE,KAAK;QACjB,OAAO,EAAE,EAAE;QACX,KAAK,EAAE,IAAI;QACX,UAAU,EAAE,EAAE;QACd,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,aAAa,CAAC;KAC3C,CAAC,CAAC;IACH,MAAM,qBAAqB,GAAG,IAAI,GAAG,EAA8B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AC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,EAAE,CAClD,QAAQ,KAAK,SAAS,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAC/E,CACqB,CAAC;AAC3B,CAAC;AAED,SAAS,oBAAoB,CAAC,GAAgB;IAC5C,OAAO,GAAG,KAAK,WAAW,IAAI,GAAG,KAAK,aAAa,IAAI,GAAG,KAAK,WAAW,CAAC;AAC7E,CAAC;AAED,SAAS,WAAW,CAA6B,MAA2B;IAC1E,OAAQ,MAAM,CAAC,MAAM,CAAC,MAAM,CAAiC,CAAC,KAAK,CACjE,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,KAAK,SAAS,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,CAC9D,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAAC,MAA8C;IAC1E,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;AAC7D,CAAC;AAED,SAAS,WAAW,CAA6B,MAAe;IAC9D,OAAO,EAAE,GAAG,MAAM,EAAE,CAAC;AACvB,CAAC;AAOD,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC","sourcesContent":["import { cell, type ReadonlyCell } from \"@reckona/mreact-reactive-core\";\nimport { type StandardSchemaV1, validateStandardSchema } from \"./standard-schema.js\";\n\nexport type FormValues = Record<string, unknown>;\nexport type FieldName<TValues extends FormValues> = Extract<keyof TValues, string>;\nexport type FormErrors<TValues extends FormValues> = Partial<\n Record<FieldName<TValues> | \"root\", string[]>\n>;\nexport type FieldValidator<TValue, TValues extends FormValues> = (\n value: TValue,\n values: TValues,\n) => readonly string[] | string | undefined | Promise<readonly string[] | string | undefined>;\nexport type FormValidateMode = \"change\" | \"blur\" | \"submit\";\n\nexport interface FormState<TValues extends FormValues> {\n dirty: boolean;\n errors: FormErrors<TValues>;\n initialValues: TValues;\n submitCount: number;\n submitting: boolean;\n touched: Partial<Record<FieldName<TValues>, boolean>>;\n valid: boolean;\n validating: Partial<Record<FieldName<TValues>, boolean>>;\n values: TValues;\n}\n\nexport interface FieldState<TValue> {\n dirty: boolean;\n errors: string[];\n touched: boolean;\n validating: boolean;\n value: TValue;\n}\n\nexport interface FieldBinding<TValue> {\n onBlur(event: Event): Promise<void>;\n onChange(event: Event): Promise<void>;\n onInput(event: Event): Promise<void>;\n value: TValue;\n}\n\nexport interface FieldBindingOptions {\n event?: \"change\" | \"input\" | undefined;\n}\n\nexport interface FieldApi<TValues extends FormValues, Name extends FieldName<TValues>> {\n readonly state: ReadonlyCell<FieldState<TValues[Name]>>;\n bind(options?: FieldBindingOptions): FieldBinding<TValues[Name]>;\n blur(): Promise<void>;\n setValue(value: TValues[Name]): Promise<void>;\n}\n\ninterface BaseCreateFormOptions<TValues extends FormValues> {\n initialValues: TValues;\n validate?:\n | Partial<{\n [Name in FieldName<TValues>]: FieldValidator<TValues[Name], TValues>;\n }>\n | undefined;\n validateOn?: FormValidateMode | readonly FormValidateMode[] | undefined;\n}\n\nexport interface CreateFormOptionsWithoutSchema<TValues extends FormValues>\n extends BaseCreateFormOptions<TValues> {\n schema?: undefined;\n}\n\nexport interface CreateFormOptionsWithSchema<TValues extends FormValues, TSubmitValues>\n extends BaseCreateFormOptions<TValues> {\n schema: StandardSchemaV1<TValues, TSubmitValues>;\n}\n\nexport type CreateFormOptions<TValues extends FormValues, TSubmitValues = TValues> =\n | CreateFormOptionsWithoutSchema<TValues>\n | CreateFormOptionsWithSchema<TValues, TSubmitValues>;\n\nexport type FormValidationResult<TValues extends FormValues, TSubmitValues> =\n | {\n success: true;\n value: TSubmitValues;\n }\n | {\n errors: FormErrors<TValues>;\n success: false;\n };\n\nexport type FormSubmitResult<TValues extends FormValues, TResult> =\n | {\n data: TResult;\n status: \"success\";\n }\n | {\n status: \"duplicate\";\n }\n | {\n errors: FormErrors<TValues>;\n status: \"invalid\";\n }\n | {\n error: unknown;\n status: \"error\";\n };\n\nexport interface ServerActionErrors<TValues extends FormValues> {\n fieldErrors?: Partial<Record<FieldName<TValues>, readonly string[]>> | undefined;\n formErrors?: readonly string[] | undefined;\n}\n\nexport interface FormApi<TValues extends FormValues, TSubmitValues> {\n readonly state: ReadonlyCell<FormState<TValues>>;\n field<Name extends FieldName<TValues>>(name: Name): FieldApi<TValues, Name>;\n getValues(): TValues;\n reset(values?: TValues): void;\n setErrors(errors: FormErrors<TValues>): void;\n setServerErrors(errors: ServerActionErrors<TValues>): void;\n setValue<Name extends FieldName<TValues>>(name: Name, value: TValues[Name]): Promise<void>;\n submit<TResult>(\n handler: (values: TSubmitValues) => Promise<TResult> | TResult,\n ): Promise<FormSubmitResult<TValues, TResult>>;\n validate(): Promise<FormValidationResult<TValues, TSubmitValues>>;\n}\n\nexport function createForm<TValues extends FormValues>(\n options: CreateFormOptionsWithoutSchema<TValues>,\n): FormApi<TValues, TValues>;\nexport function createForm<TValues extends FormValues, TSubmitValues>(\n options: CreateFormOptionsWithSchema<TValues, TSubmitValues>,\n): FormApi<TValues, TSubmitValues>;\nexport function createForm<TValues extends FormValues, TSubmitValues = TValues>(\n options: CreateFormOptions<TValues, TSubmitValues>,\n): FormApi<TValues, TSubmitValues> {\n const validateOn = new Set(\n Array.isArray(options.validateOn) ? options.validateOn : [options.validateOn ?? \"submit\"],\n );\n let initialValues = cloneValues(options.initialValues);\n const state = cell<FormState<TValues>>({\n dirty: false,\n errors: {},\n initialValues,\n submitCount: 0,\n submitting: false,\n touched: {},\n valid: true,\n validating: {},\n values: cloneValues(options.initialValues),\n });\n const validationGenerations = new Map<FieldName<TValues>, number>();\n const dirtyFields = new Set<FieldName<TValues>>();\n let activeSubmit: Promise<FormSubmitResult<TValues, unknown>> | undefined;\n\n function commit(patch: Partial<FormState<TValues>>, dirty = dirtyFields.size > 0): void {\n const previous = state.get();\n const next = {\n ...previous,\n ...patch,\n };\n next.dirty = dirty;\n next.valid = hasNoErrors(next.errors);\n state.set(next);\n }\n\n function setErrors(errors: FormErrors<TValues>): void {\n commit({ errors: normalizeErrors(errors) });\n }\n\n async function validateField<Name extends FieldName<TValues>>(name: Name): Promise<void> {\n const validator = options.validate?.[name];\n const generation = (validationGenerations.get(name) ?? 0) + 1;\n validationGenerations.set(name, generation);\n\n if (validator === undefined) {\n setFieldErrors(name, []);\n setFieldValidating(name, false);\n return;\n }\n\n const values = state.get().values;\n setFieldValidating(name, true);\n const errors = await validator(values[name], values);\n\n if (validationGenerations.get(name) !== generation) {\n return;\n }\n\n setFieldErrors(name, normalizeFieldErrors(errors));\n setFieldValidating(name, false);\n }\n\n function setFieldErrors<Name extends FieldName<TValues>>(\n name: Name,\n errors: readonly string[],\n ): void {\n const current = state.get().errors;\n setErrors({\n ...current,\n [name]: [...errors],\n } as FormErrors<TValues>);\n }\n\n function setFieldValidating<Name extends FieldName<TValues>>(\n name: Name,\n validating: boolean,\n ): void {\n const current = state.get().validating;\n commit({\n validating: {\n ...current,\n [name]: validating,\n },\n });\n }\n\n async function setValue<Name extends FieldName<TValues>>(\n name: Name,\n value: TValues[Name],\n ): Promise<void> {\n const previous = state.get();\n updateDirtyField(name, value);\n commit({\n values: {\n ...previous.values,\n [name]: value,\n },\n });\n\n if (validateOn.has(\"change\")) {\n await validateField(name);\n }\n }\n\n async function blurField<Name extends FieldName<TValues>>(name: Name): Promise<void> {\n commit({\n touched: {\n ...state.get().touched,\n [name]: true,\n },\n });\n\n if (validateOn.has(\"blur\")) {\n await validateField(name);\n }\n }\n\n async function validateForm(): Promise<FormValidationResult<TValues, TSubmitValues>> {\n const values = state.get().values;\n const errors: FormErrors<TValues> = {};\n\n for (const name of Object.keys(options.validate ?? {}) as Array<FieldName<TValues>>) {\n const validator = options.validate?.[name];\n if (validator === undefined) {\n continue;\n }\n const fieldErrors = normalizeFieldErrors(await validator(values[name], values));\n if (fieldErrors.length > 0) {\n errors[name] = fieldErrors;\n }\n }\n\n if (options.schema !== undefined) {\n const result = await validateStandardSchema(options.schema, values);\n\n if (!result.success) {\n mergeIssueErrors(errors, result.issues);\n } else if (Object.keys(errors).length === 0) {\n setErrors({});\n return {\n success: true,\n value: result.value as TSubmitValues,\n };\n }\n }\n\n if (Object.keys(errors).length > 0) {\n setErrors(errors);\n return {\n errors,\n success: false,\n };\n }\n\n setErrors({});\n return {\n success: true,\n value: values as TValues & TSubmitValues,\n };\n }\n\n return {\n state,\n field<Name extends FieldName<TValues>>(name: Name): FieldApi<TValues, Name> {\n return {\n state: {\n get: () => fieldState(state.get(), name),\n },\n bind(bindingOptions = {}) {\n const bindingEvent = bindingOptions.event ?? \"input\";\n const updateValue = async (inputEvent: Event) => {\n await setValue(name, eventValue(inputEvent, state.get().values[name]) as TValues[Name]);\n };\n\n return {\n onBlur: async () => {\n await blurField(name);\n },\n onChange: async (event) => {\n if (bindingEvent === \"change\") {\n await updateValue(event);\n }\n },\n onInput: async (event) => {\n if (bindingEvent === \"input\") {\n await updateValue(event);\n }\n },\n get value() {\n return state.get().values[name];\n },\n };\n },\n async blur() {\n await blurField(name);\n },\n setValue: (value) => setValue(name, value),\n };\n },\n getValues() {\n return state.get().values;\n },\n reset(values = initialValues): void {\n for (const name of Object.keys(options.validate ?? {}) as Array<FieldName<TValues>>) {\n validationGenerations.set(name, (validationGenerations.get(name) ?? 0) + 1);\n }\n\n initialValues = cloneValues(values);\n dirtyFields.clear();\n commit({\n errors: {},\n initialValues,\n submitCount: 0,\n submitting: false,\n touched: {},\n validating: {},\n values: cloneValues(values),\n });\n },\n setErrors,\n setServerErrors(errors): void {\n const next: FormErrors<TValues> = {};\n\n for (const [name, messages] of Object.entries(errors.fieldErrors ?? {})) {\n if (isDangerousObjectKey(name)) {\n continue;\n }\n\n if (messages !== undefined && messages.length > 0) {\n next[name as FieldName<TValues>] = [...messages];\n }\n }\n\n if (errors.formErrors !== undefined && errors.formErrors.length > 0) {\n next.root = [...errors.formErrors];\n }\n\n setErrors(next);\n },\n setValue,\n async submit<TResult>(\n handler: (values: TSubmitValues) => Promise<TResult> | TResult,\n ): Promise<FormSubmitResult<TValues, TResult>> {\n if (activeSubmit !== undefined) {\n return { status: \"duplicate\" };\n }\n\n const task = (async (): Promise<FormSubmitResult<TValues, TResult>> => {\n commit({\n submitCount: state.get().submitCount + 1,\n submitting: true,\n });\n\n try {\n const validation = await validateForm();\n\n if (!validation.success) {\n return {\n errors: validation.errors,\n status: \"invalid\",\n };\n }\n\n try {\n const data = await handler(validation.value);\n return {\n data,\n status: \"success\",\n };\n } catch (error) {\n return {\n error,\n status: \"error\",\n };\n }\n } finally {\n activeSubmit = undefined;\n commit({ submitting: false });\n }\n })();\n\n activeSubmit = task as Promise<FormSubmitResult<TValues, unknown>>;\n return task;\n },\n validate: validateForm,\n };\n\n function updateDirtyField<Name extends FieldName<TValues>>(\n name: Name,\n value: TValues[Name],\n ): void {\n if (Object.is(value, initialValues[name])) {\n dirtyFields.delete(name);\n return;\n }\n\n dirtyFields.add(name);\n }\n}\n\nfunction fieldState<TValues extends FormValues, Name extends FieldName<TValues>>(\n formState: FormState<TValues>,\n name: Name,\n): FieldState<TValues[Name]> {\n return {\n dirty: !Object.is(formState.values[name], formState.initialValues[name]),\n errors: [...(formState.errors[name] ?? [])],\n touched: formState.touched[name] === true,\n validating: formState.validating[name] === true,\n value: formState.values[name],\n };\n}\n\nfunction eventValue(event: Event, currentValue: unknown): unknown {\n const target = event.currentTarget ?? event.target;\n\n if (target !== null && typeof target === \"object\") {\n if (typeof currentValue === \"boolean\" && \"checked\" in target) {\n return Boolean((target as { checked: unknown }).checked);\n }\n\n if (\"value\" in target) {\n return (target as { value: unknown }).value;\n }\n }\n\n return currentValue;\n}\n\nfunction mergeIssueErrors<TValues extends FormValues>(\n errors: FormErrors<TValues>,\n issues: ReadonlyArray<StandardSchemaV1.Issue>,\n): void {\n for (const issue of issues) {\n const key = issuePathKey(issue.path);\n const name = (key ?? \"root\") as FieldName<TValues> | \"root\";\n const messages = errors[name] ?? [];\n messages.push(issue.message);\n errors[name] = messages;\n }\n}\n\nfunction issuePathKey(\n path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment> | undefined,\n): string | undefined {\n const first = path?.[0];\n\n if (first === undefined) {\n return undefined;\n }\n\n return typeof first === \"object\" && first !== null && \"key\" in first\n ? String(first.key)\n : String(first);\n}\n\nfunction normalizeErrors<TValues extends FormValues>(\n errors: FormErrors<TValues>,\n): FormErrors<TValues> {\n return Object.fromEntries(\n Object.entries(errors).flatMap(([name, messages]) =>\n messages === undefined || messages.length === 0 ? [] : [[name, [...messages]]],\n ),\n ) as FormErrors<TValues>;\n}\n\nfunction isDangerousObjectKey(key: PropertyKey): boolean {\n return key === \"__proto__\" || key === \"constructor\" || key === \"prototype\";\n}\n\nfunction hasNoErrors<TValues extends FormValues>(errors: FormErrors<TValues>): boolean {\n return (Object.values(errors) as Array<string[] | undefined>).every(\n (messages) => messages === undefined || messages.length === 0,\n );\n}\n\nfunction normalizeFieldErrors(errors: readonly string[] | string | undefined): string[] {\n if (errors === undefined) {\n return [];\n }\n\n return typeof errors === \"string\" ? [errors] : [...errors];\n}\n\nfunction cloneValues<TValues extends FormValues>(values: TValues): TValues {\n return { ...values };\n}\n\nexport type {\n InferStandardSchemaInput,\n InferStandardSchemaOutput,\n StandardSchemaV1,\n} from \"./standard-schema.js\";\nexport { validateStandardSchema } from \"./standard-schema.js\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAqB,MAAM,+BAA+B,CAAC;AACxE,OAAO,EAAyB,sBAAsB,EAAE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AAI,QAAQ,CAAC,MAAM,KAAK,CAAC,CAC9D,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAAC,MAA8C;IAC1E,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;AAC7D,CAAC;AAED,SAAS,WAAW,CAA6B,MAAe;IAC9D,OAAO,EAAE,GAAG,MAAM,EAAE,CAAC;AACvB,CAAC;AAQD,yFAAyF;AACzF,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC","sourcesContent":["import { cell, type ReadonlyCell } from \"@reckona/mreact-reactive-core\";\nimport { type StandardSchemaV1, validateStandardSchema } from \"./standard-schema.js\";\n\n/** Represents the object shape managed by a form instance. */\nexport type FormValues = Record<string, unknown>;\n\n/** Extracts string field names from a form value object. */\nexport type FieldName<TValues extends FormValues> = Extract<keyof TValues, string>;\n\n/** Maps field names and the root form key to validation error messages. */\nexport type FormErrors<TValues extends FormValues> = Partial<\n Record<FieldName<TValues> | \"root\", string[]>\n>;\n\n/** Validates one field value against the full form values object. */\nexport type FieldValidator<TValue, TValues extends FormValues> = (\n value: TValue,\n values: TValues,\n) => readonly string[] | string | undefined | Promise<readonly string[] | string | undefined>;\n\n/** Names the form events that can trigger validation. */\nexport type FormValidateMode = \"change\" | \"blur\" | \"submit\";\n\n/** Describes the complete reactive state tracked by a form instance. */\nexport interface FormState<TValues extends FormValues> {\n dirty: boolean;\n errors: FormErrors<TValues>;\n initialValues: TValues;\n submitCount: number;\n submitting: boolean;\n touched: Partial<Record<FieldName<TValues>, boolean>>;\n valid: boolean;\n validating: Partial<Record<FieldName<TValues>, boolean>>;\n values: TValues;\n}\n\n/** Describes the derived state for one form field. */\nexport interface FieldState<TValue> {\n dirty: boolean;\n errors: string[];\n touched: boolean;\n validating: boolean;\n value: TValue;\n}\n\n/** Provides DOM event handlers and current value for binding a field to an input. */\nexport interface FieldBinding<TValue> {\n onBlur(event: Event): Promise<void>;\n onChange(event: Event): Promise<void>;\n onInput(event: Event): Promise<void>;\n value: TValue;\n}\n\n/** Configures which DOM event updates a field binding. */\nexport interface FieldBindingOptions {\n event?: \"change\" | \"input\" | undefined;\n}\n\n/** Exposes state, binding, blur, and value update controls for one field. */\nexport interface FieldApi<TValues extends FormValues, Name extends FieldName<TValues>> {\n readonly state: ReadonlyCell<FieldState<TValues[Name]>>;\n bind(options?: FieldBindingOptions): FieldBinding<TValues[Name]>;\n blur(): Promise<void>;\n setValue(value: TValues[Name]): Promise<void>;\n}\n\ninterface BaseCreateFormOptions<TValues extends FormValues> {\n initialValues: TValues;\n validate?:\n | Partial<{\n [Name in FieldName<TValues>]: FieldValidator<TValues[Name], TValues>;\n }>\n | undefined;\n validateOn?: FormValidateMode | readonly FormValidateMode[] | undefined;\n}\n\n/** Configures form creation without schema-level submit value transformation. */\nexport interface CreateFormOptionsWithoutSchema<TValues extends FormValues>\n extends BaseCreateFormOptions<TValues> {\n schema?: undefined;\n}\n\n/** Configures form creation with a Standard Schema validator that may transform submit values. */\nexport interface CreateFormOptionsWithSchema<TValues extends FormValues, TSubmitValues>\n extends BaseCreateFormOptions<TValues> {\n schema: StandardSchemaV1<TValues, TSubmitValues>;\n}\n\n/** Configures form creation with optional field validators and optional Standard Schema validation. */\nexport type CreateFormOptions<TValues extends FormValues, TSubmitValues = TValues> =\n | CreateFormOptionsWithoutSchema<TValues>\n | CreateFormOptionsWithSchema<TValues, TSubmitValues>;\n\n/** Reports either successful normalized submit values or form validation errors. */\nexport type FormValidationResult<TValues extends FormValues, TSubmitValues> =\n | {\n success: true;\n value: TSubmitValues;\n }\n | {\n errors: FormErrors<TValues>;\n success: false;\n };\n\n/** Reports the result of a form submit handler after validation and error capture. */\nexport type FormSubmitResult<TValues extends FormValues, TResult> =\n | {\n data: TResult;\n status: \"success\";\n }\n | {\n status: \"duplicate\";\n }\n | {\n errors: FormErrors<TValues>;\n status: \"invalid\";\n }\n | {\n error: unknown;\n status: \"error\";\n };\n\n/** Represents field and form errors returned by a server action. */\nexport interface ServerActionErrors<TValues extends FormValues> {\n fieldErrors?: Partial<Record<FieldName<TValues>, readonly string[]>> | undefined;\n formErrors?: readonly string[] | undefined;\n}\n\n/** Provides reactive form state, field access, validation, submit, reset, and error controls. */\nexport interface FormApi<TValues extends FormValues, TSubmitValues> {\n readonly state: ReadonlyCell<FormState<TValues>>;\n field<Name extends FieldName<TValues>>(name: Name): FieldApi<TValues, Name>;\n getValues(): TValues;\n reset(values?: TValues): void;\n setErrors(errors: FormErrors<TValues>): void;\n setServerErrors(errors: ServerActionErrors<TValues>): void;\n setValue<Name extends FieldName<TValues>>(name: Name, value: TValues[Name]): Promise<void>;\n submit<TResult>(\n handler: (values: TSubmitValues) => Promise<TResult> | TResult,\n ): Promise<FormSubmitResult<TValues, TResult>>;\n validate(): Promise<FormValidationResult<TValues, TSubmitValues>>;\n}\n\n/** Creates a reactive form API from initial values, validators, and optional schema validation. */\nexport function createForm<TValues extends FormValues>(\n options: CreateFormOptionsWithoutSchema<TValues>,\n): FormApi<TValues, TValues>;\nexport function createForm<TValues extends FormValues, TSubmitValues>(\n options: CreateFormOptionsWithSchema<TValues, TSubmitValues>,\n): FormApi<TValues, TSubmitValues>;\nexport function createForm<TValues extends FormValues, TSubmitValues = TValues>(\n options: CreateFormOptions<TValues, TSubmitValues>,\n): FormApi<TValues, TSubmitValues> {\n const validateOn = new Set(\n Array.isArray(options.validateOn) ? options.validateOn : [options.validateOn ?? \"submit\"],\n );\n let initialValues = cloneValues(options.initialValues);\n const state = cell<FormState<TValues>>({\n dirty: false,\n errors: {},\n initialValues,\n submitCount: 0,\n submitting: false,\n touched: {},\n valid: true,\n validating: {},\n values: cloneValues(options.initialValues),\n });\n const validationGenerations = new Map<FieldName<TValues>, number>();\n const dirtyFields = new Set<FieldName<TValues>>();\n let activeSubmit: Promise<FormSubmitResult<TValues, unknown>> | undefined;\n\n function commit(patch: Partial<FormState<TValues>>, dirty = dirtyFields.size > 0): void {\n const previous = state.get();\n const next = {\n ...previous,\n ...patch,\n };\n next.dirty = dirty;\n next.valid = hasNoErrors(next.errors);\n state.set(next);\n }\n\n function setErrors(errors: FormErrors<TValues>): void {\n commit({ errors: normalizeErrors(errors) });\n }\n\n async function validateField<Name extends FieldName<TValues>>(name: Name): Promise<void> {\n const validator = options.validate?.[name];\n const generation = (validationGenerations.get(name) ?? 0) + 1;\n validationGenerations.set(name, generation);\n\n if (validator === undefined) {\n setFieldErrors(name, []);\n setFieldValidating(name, false);\n return;\n }\n\n const values = state.get().values;\n setFieldValidating(name, true);\n const errors = await validator(values[name], values);\n\n if (validationGenerations.get(name) !== generation) {\n return;\n }\n\n setFieldErrors(name, normalizeFieldErrors(errors));\n setFieldValidating(name, false);\n }\n\n function setFieldErrors<Name extends FieldName<TValues>>(\n name: Name,\n errors: readonly string[],\n ): void {\n const current = state.get().errors;\n setErrors({\n ...current,\n [name]: [...errors],\n } as FormErrors<TValues>);\n }\n\n function setFieldValidating<Name extends FieldName<TValues>>(\n name: Name,\n validating: boolean,\n ): void {\n const current = state.get().validating;\n commit({\n validating: {\n ...current,\n [name]: validating,\n },\n });\n }\n\n async function setValue<Name extends FieldName<TValues>>(\n name: Name,\n value: TValues[Name],\n ): Promise<void> {\n const previous = state.get();\n updateDirtyField(name, value);\n commit({\n values: {\n ...previous.values,\n [name]: value,\n },\n });\n\n if (validateOn.has(\"change\")) {\n await validateField(name);\n }\n }\n\n async function blurField<Name extends FieldName<TValues>>(name: Name): Promise<void> {\n commit({\n touched: {\n ...state.get().touched,\n [name]: true,\n },\n });\n\n if (validateOn.has(\"blur\")) {\n await validateField(name);\n }\n }\n\n async function validateForm(): Promise<FormValidationResult<TValues, TSubmitValues>> {\n const values = state.get().values;\n const errors: FormErrors<TValues> = {};\n\n for (const name of Object.keys(options.validate ?? {}) as Array<FieldName<TValues>>) {\n const validator = options.validate?.[name];\n if (validator === undefined) {\n continue;\n }\n const fieldErrors = normalizeFieldErrors(await validator(values[name], values));\n if (fieldErrors.length > 0) {\n errors[name] = fieldErrors;\n }\n }\n\n if (options.schema !== undefined) {\n const result = await validateStandardSchema(options.schema, values);\n\n if (!result.success) {\n mergeIssueErrors(errors, result.issues);\n } else if (Object.keys(errors).length === 0) {\n setErrors({});\n return {\n success: true,\n value: result.value as TSubmitValues,\n };\n }\n }\n\n if (Object.keys(errors).length > 0) {\n setErrors(errors);\n return {\n errors,\n success: false,\n };\n }\n\n setErrors({});\n return {\n success: true,\n value: values as TValues & TSubmitValues,\n };\n }\n\n return {\n state,\n field<Name extends FieldName<TValues>>(name: Name): FieldApi<TValues, Name> {\n return {\n state: {\n get: () => fieldState(state.get(), name),\n },\n bind(bindingOptions = {}) {\n const bindingEvent = bindingOptions.event ?? \"input\";\n const updateValue = async (inputEvent: Event) => {\n await setValue(name, eventValue(inputEvent, state.get().values[name]) as TValues[Name]);\n };\n\n return {\n onBlur: async () => {\n await blurField(name);\n },\n onChange: async (event) => {\n if (bindingEvent === \"change\") {\n await updateValue(event);\n }\n },\n onInput: async (event) => {\n if (bindingEvent === \"input\") {\n await updateValue(event);\n }\n },\n get value() {\n return state.get().values[name];\n },\n };\n },\n async blur() {\n await blurField(name);\n },\n setValue: (value) => setValue(name, value),\n };\n },\n getValues() {\n return state.get().values;\n },\n reset(values = initialValues): void {\n for (const name of Object.keys(options.validate ?? {}) as Array<FieldName<TValues>>) {\n validationGenerations.set(name, (validationGenerations.get(name) ?? 0) + 1);\n }\n\n initialValues = cloneValues(values);\n dirtyFields.clear();\n commit({\n errors: {},\n initialValues,\n submitCount: 0,\n submitting: false,\n touched: {},\n validating: {},\n values: cloneValues(values),\n });\n },\n setErrors,\n setServerErrors(errors): void {\n const next: FormErrors<TValues> = {};\n\n for (const [name, messages] of Object.entries(errors.fieldErrors ?? {})) {\n if (isDangerousObjectKey(name)) {\n continue;\n }\n\n if (messages !== undefined && messages.length > 0) {\n next[name as FieldName<TValues>] = [...messages];\n }\n }\n\n if (errors.formErrors !== undefined && errors.formErrors.length > 0) {\n next.root = [...errors.formErrors];\n }\n\n setErrors(next);\n },\n setValue,\n async submit<TResult>(\n handler: (values: TSubmitValues) => Promise<TResult> | TResult,\n ): Promise<FormSubmitResult<TValues, TResult>> {\n if (activeSubmit !== undefined) {\n return { status: \"duplicate\" };\n }\n\n const task = (async (): Promise<FormSubmitResult<TValues, TResult>> => {\n commit({\n submitCount: state.get().submitCount + 1,\n submitting: true,\n });\n\n try {\n const validation = await validateForm();\n\n if (!validation.success) {\n return {\n errors: validation.errors,\n status: \"invalid\",\n };\n }\n\n try {\n const data = await handler(validation.value);\n return {\n data,\n status: \"success\",\n };\n } catch (error) {\n return {\n error,\n status: \"error\",\n };\n }\n } finally {\n activeSubmit = undefined;\n commit({ submitting: false });\n }\n })();\n\n activeSubmit = task as Promise<FormSubmitResult<TValues, unknown>>;\n return task;\n },\n validate: validateForm,\n };\n\n function updateDirtyField<Name extends FieldName<TValues>>(\n name: Name,\n value: TValues[Name],\n ): void {\n if (Object.is(value, initialValues[name])) {\n dirtyFields.delete(name);\n return;\n }\n\n dirtyFields.add(name);\n }\n}\n\nfunction fieldState<TValues extends FormValues, Name extends FieldName<TValues>>(\n formState: FormState<TValues>,\n name: Name,\n): FieldState<TValues[Name]> {\n return {\n dirty: !Object.is(formState.values[name], formState.initialValues[name]),\n errors: [...(formState.errors[name] ?? [])],\n touched: formState.touched[name] === true,\n validating: formState.validating[name] === true,\n value: formState.values[name],\n };\n}\n\nfunction eventValue(event: Event, currentValue: unknown): unknown {\n const target = event.currentTarget ?? event.target;\n\n if (target !== null && typeof target === \"object\") {\n if (typeof currentValue === \"boolean\" && \"checked\" in target) {\n return Boolean((target as { checked: unknown }).checked);\n }\n\n if (\"value\" in target) {\n return (target as { value: unknown }).value;\n }\n }\n\n return currentValue;\n}\n\nfunction mergeIssueErrors<TValues extends FormValues>(\n errors: FormErrors<TValues>,\n issues: ReadonlyArray<StandardSchemaV1.Issue>,\n): void {\n for (const issue of issues) {\n const key = issuePathKey(issue.path);\n const name = (key ?? \"root\") as FieldName<TValues> | \"root\";\n const messages = errors[name] ?? [];\n messages.push(issue.message);\n errors[name] = messages;\n }\n}\n\nfunction issuePathKey(\n path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment> | undefined,\n): string | undefined {\n const first = path?.[0];\n\n if (first === undefined) {\n return undefined;\n }\n\n return typeof first === \"object\" && first !== null && \"key\" in first\n ? String(first.key)\n : String(first);\n}\n\nfunction normalizeErrors<TValues extends FormValues>(\n errors: FormErrors<TValues>,\n): FormErrors<TValues> {\n return Object.fromEntries(\n Object.entries(errors).flatMap(([name, messages]) =>\n messages === undefined || messages.length === 0 ? [] : [[name, [...messages]]],\n ),\n ) as FormErrors<TValues>;\n}\n\nfunction isDangerousObjectKey(key: PropertyKey): boolean {\n return key === \"__proto__\" || key === \"constructor\" || key === \"prototype\";\n}\n\nfunction hasNoErrors<TValues extends FormValues>(errors: FormErrors<TValues>): boolean {\n return (Object.values(errors) as Array<string[] | undefined>).every(\n (messages) => messages === undefined || messages.length === 0,\n );\n}\n\nfunction normalizeFieldErrors(errors: readonly string[] | string | undefined): string[] {\n if (errors === undefined) {\n return [];\n }\n\n return typeof errors === \"string\" ? [errors] : [...errors];\n}\n\nfunction cloneValues<TValues extends FormValues>(values: TValues): TValues {\n return { ...values };\n}\n\n/** Re-exports Standard Schema types used by form schema options. */\nexport type {\n InferStandardSchemaInput,\n InferStandardSchemaOutput,\n StandardSchemaV1,\n} from \"./standard-schema.js\";\n/** Validates an unknown value with a Standard Schema and normalizes the result shape. */\nexport { validateStandardSchema } from \"./standard-schema.js\";\n"]}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
/** Represents the Standard Schema v1 wrapper shape used by compatible validation libraries. */
|
|
1
2
|
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
2
3
|
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
3
4
|
}
|
|
5
|
+
/** Contains the Standard Schema v1 protocol helper types. */
|
|
4
6
|
export declare namespace StandardSchemaV1 {
|
|
5
7
|
interface Props<Input = unknown, Output = Input> {
|
|
6
8
|
readonly version: 1;
|
|
@@ -31,6 +33,7 @@ export declare namespace StandardSchemaV1 {
|
|
|
31
33
|
readonly output: Output;
|
|
32
34
|
}
|
|
33
35
|
}
|
|
36
|
+
/** Infers the input value type accepted by a Standard Schema. */
|
|
34
37
|
export type InferStandardSchemaInput<Schema> = Schema extends {
|
|
35
38
|
readonly "~standard": {
|
|
36
39
|
readonly types?: (infer Types) | undefined;
|
|
@@ -38,6 +41,7 @@ export type InferStandardSchemaInput<Schema> = Schema extends {
|
|
|
38
41
|
} ? NonNullable<Types> extends {
|
|
39
42
|
readonly input: infer Input;
|
|
40
43
|
} ? Input : never : never;
|
|
44
|
+
/** Infers the output value type produced by a Standard Schema. */
|
|
41
45
|
export type InferStandardSchemaOutput<Schema> = Schema extends {
|
|
42
46
|
readonly "~standard": {
|
|
43
47
|
readonly types?: (infer Types) | undefined;
|
|
@@ -45,6 +49,7 @@ export type InferStandardSchemaOutput<Schema> = Schema extends {
|
|
|
45
49
|
} ? NonNullable<Types> extends {
|
|
46
50
|
readonly output: infer Output;
|
|
47
51
|
} ? Output : never : never;
|
|
52
|
+
/** Represents the normalized result returned after Standard Schema validation. */
|
|
48
53
|
export type StandardSchemaValidationResult<Output> = {
|
|
49
54
|
success: true;
|
|
50
55
|
value: Output;
|
|
@@ -52,5 +57,6 @@ export type StandardSchemaValidationResult<Output> = {
|
|
|
52
57
|
issues: ReadonlyArray<StandardSchemaV1.Issue>;
|
|
53
58
|
success: false;
|
|
54
59
|
};
|
|
60
|
+
/** Validates an unknown value with a Standard Schema and normalizes success and issue results. */
|
|
55
61
|
export declare function validateStandardSchema<Schema extends StandardSchemaV1>(schema: Schema, value: unknown): Promise<StandardSchemaValidationResult<InferStandardSchemaOutput<Schema>>>;
|
|
56
62
|
//# sourceMappingURL=standard-schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../src/standard-schema.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;IAC/D,QAAQ,CAAC,WAAW,EAAE,gBAAgB,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;CAC7D;AAED,yBAAiB,gBAAgB,CAAC;IAChC,UAAiB,KAAK,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;QACpD,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QACxB,QAAQ,CAAC,QAAQ,EAAE,CACjB,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,KAC1B,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QAC9C,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;KACnD;IAED,UAAiB,OAAO;QACtB,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;KAC/D;IAED,KAAY,MAAM,CAAC,MAAM,IAAI,aAAa,CAAC,MAAM,CAAC,GAAG,aAAa,CAAC;IAEnE,UAAiB,aAAa,CAAC,MAAM;QACnC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;KAC7B;IAED,UAAiB,aAAa;QAC5B,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC;KACvC;IAED,UAAiB,KAAK;QACp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AAC;IACd,KAAK,EAAE,MAAM,CAAC;CACf,GACD;IACE,MAAM,EAAE,aAAa,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAC9C,OAAO,EAAE,KAAK,CAAC;CAChB,CAAC;AAEN,kGAAkG;AAClG,wBAAsB,sBAAsB,CAAC,MAAM,SAAS,gBAAgB,EAC1E,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,8BAA8B,CAAC,yBAAyB,CAAC,MAAM,CAAC,CAAC,CAAC,CAY5E"}
|
package/dist/standard-schema.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standard-schema.js","sourceRoot":"","sources":["../src/standard-schema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"standard-schema.js","sourceRoot":"","sources":["../src/standard-schema.ts"],"names":[],"mappings":"AA4EA,kGAAkG;AAClG,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,MAAc,EACd,KAAc;IAEd,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAEzD,OAAO,MAAM,CAAC,MAAM,KAAK,SAAS;QAChC,CAAC,CAAC;YACE,OAAO,EAAE,IAAI;YACb,KAAK,EAAE,MAAM,CAAC,KAA0C;SACzD;QACH,CAAC,CAAC;YACE,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,OAAO,EAAE,KAAK;SACf,CAAC;AACR,CAAC","sourcesContent":["/** Represents the Standard Schema v1 wrapper shape used by compatible validation libraries. */\nexport interface StandardSchemaV1<Input = unknown, Output = Input> {\n readonly \"~standard\": StandardSchemaV1.Props<Input, Output>;\n}\n\n/** Contains the Standard Schema v1 protocol helper types. */\nexport namespace StandardSchemaV1 {\n export interface Props<Input = unknown, Output = Input> {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (\n value: unknown,\n options?: Options | undefined,\n ) => Result<Output> | Promise<Result<Output>>;\n readonly types?: Types<Input, Output> | undefined;\n }\n\n export interface Options {\n readonly libraryOptions?: Record<string, unknown> | undefined;\n }\n\n export type Result<Output> = SuccessResult<Output> | FailureResult;\n\n export interface SuccessResult<Output> {\n readonly value: Output;\n readonly issues?: undefined;\n }\n\n export interface FailureResult {\n readonly issues: ReadonlyArray<Issue>;\n }\n\n export interface Issue {\n readonly message: string;\n readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;\n }\n\n export interface PathSegment {\n readonly key: PropertyKey;\n }\n\n export interface Types<Input = unknown, Output = Input> {\n readonly input: Input;\n readonly output: Output;\n }\n}\n\n/** Infers the input value type accepted by a Standard Schema. */\nexport type InferStandardSchemaInput<Schema> = Schema extends {\n readonly \"~standard\": { readonly types?: (infer Types) | undefined };\n}\n ? NonNullable<Types> extends { readonly input: infer Input }\n ? Input\n : never\n : never;\n\n/** Infers the output value type produced by a Standard Schema. */\nexport type InferStandardSchemaOutput<Schema> = Schema extends {\n readonly \"~standard\": { readonly types?: (infer Types) | undefined };\n}\n ? NonNullable<Types> extends { readonly output: infer Output }\n ? Output\n : never\n : never;\n\n/** Represents the normalized result returned after Standard Schema validation. */\nexport type StandardSchemaValidationResult<Output> =\n | {\n success: true;\n value: Output;\n }\n | {\n issues: ReadonlyArray<StandardSchemaV1.Issue>;\n success: false;\n };\n\n/** Validates an unknown value with a Standard Schema and normalizes success and issue results. */\nexport async function validateStandardSchema<Schema extends StandardSchemaV1>(\n schema: Schema,\n value: unknown,\n): Promise<StandardSchemaValidationResult<InferStandardSchemaOutput<Schema>>> {\n const result = await schema[\"~standard\"].validate(value);\n\n return result.issues === undefined\n ? {\n success: true,\n value: result.value as InferStandardSchemaOutput<Schema>,\n }\n : {\n issues: result.issues,\n success: false,\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reckona/mreact-forms",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.162",
|
|
4
4
|
"description": "Reactive form state and validation utilities for mreact.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"forms",
|
|
@@ -44,6 +44,6 @@
|
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@reckona/mreact-reactive-core": "0.0.
|
|
47
|
+
"@reckona/mreact-reactive-core": "0.0.162"
|
|
48
48
|
}
|
|
49
49
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
import { cell, type ReadonlyCell } from "@reckona/mreact-reactive-core";
|
|
2
2
|
import { type StandardSchemaV1, validateStandardSchema } from "./standard-schema.js";
|
|
3
3
|
|
|
4
|
+
/** Represents the object shape managed by a form instance. */
|
|
4
5
|
export type FormValues = Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
/** Extracts string field names from a form value object. */
|
|
5
8
|
export type FieldName<TValues extends FormValues> = Extract<keyof TValues, string>;
|
|
9
|
+
|
|
10
|
+
/** Maps field names and the root form key to validation error messages. */
|
|
6
11
|
export type FormErrors<TValues extends FormValues> = Partial<
|
|
7
12
|
Record<FieldName<TValues> | "root", string[]>
|
|
8
13
|
>;
|
|
14
|
+
|
|
15
|
+
/** Validates one field value against the full form values object. */
|
|
9
16
|
export type FieldValidator<TValue, TValues extends FormValues> = (
|
|
10
17
|
value: TValue,
|
|
11
18
|
values: TValues,
|
|
12
19
|
) => readonly string[] | string | undefined | Promise<readonly string[] | string | undefined>;
|
|
20
|
+
|
|
21
|
+
/** Names the form events that can trigger validation. */
|
|
13
22
|
export type FormValidateMode = "change" | "blur" | "submit";
|
|
14
23
|
|
|
24
|
+
/** Describes the complete reactive state tracked by a form instance. */
|
|
15
25
|
export interface FormState<TValues extends FormValues> {
|
|
16
26
|
dirty: boolean;
|
|
17
27
|
errors: FormErrors<TValues>;
|
|
@@ -24,6 +34,7 @@ export interface FormState<TValues extends FormValues> {
|
|
|
24
34
|
values: TValues;
|
|
25
35
|
}
|
|
26
36
|
|
|
37
|
+
/** Describes the derived state for one form field. */
|
|
27
38
|
export interface FieldState<TValue> {
|
|
28
39
|
dirty: boolean;
|
|
29
40
|
errors: string[];
|
|
@@ -32,6 +43,7 @@ export interface FieldState<TValue> {
|
|
|
32
43
|
value: TValue;
|
|
33
44
|
}
|
|
34
45
|
|
|
46
|
+
/** Provides DOM event handlers and current value for binding a field to an input. */
|
|
35
47
|
export interface FieldBinding<TValue> {
|
|
36
48
|
onBlur(event: Event): Promise<void>;
|
|
37
49
|
onChange(event: Event): Promise<void>;
|
|
@@ -39,10 +51,12 @@ export interface FieldBinding<TValue> {
|
|
|
39
51
|
value: TValue;
|
|
40
52
|
}
|
|
41
53
|
|
|
54
|
+
/** Configures which DOM event updates a field binding. */
|
|
42
55
|
export interface FieldBindingOptions {
|
|
43
56
|
event?: "change" | "input" | undefined;
|
|
44
57
|
}
|
|
45
58
|
|
|
59
|
+
/** Exposes state, binding, blur, and value update controls for one field. */
|
|
46
60
|
export interface FieldApi<TValues extends FormValues, Name extends FieldName<TValues>> {
|
|
47
61
|
readonly state: ReadonlyCell<FieldState<TValues[Name]>>;
|
|
48
62
|
bind(options?: FieldBindingOptions): FieldBinding<TValues[Name]>;
|
|
@@ -60,20 +74,24 @@ interface BaseCreateFormOptions<TValues extends FormValues> {
|
|
|
60
74
|
validateOn?: FormValidateMode | readonly FormValidateMode[] | undefined;
|
|
61
75
|
}
|
|
62
76
|
|
|
77
|
+
/** Configures form creation without schema-level submit value transformation. */
|
|
63
78
|
export interface CreateFormOptionsWithoutSchema<TValues extends FormValues>
|
|
64
79
|
extends BaseCreateFormOptions<TValues> {
|
|
65
80
|
schema?: undefined;
|
|
66
81
|
}
|
|
67
82
|
|
|
83
|
+
/** Configures form creation with a Standard Schema validator that may transform submit values. */
|
|
68
84
|
export interface CreateFormOptionsWithSchema<TValues extends FormValues, TSubmitValues>
|
|
69
85
|
extends BaseCreateFormOptions<TValues> {
|
|
70
86
|
schema: StandardSchemaV1<TValues, TSubmitValues>;
|
|
71
87
|
}
|
|
72
88
|
|
|
89
|
+
/** Configures form creation with optional field validators and optional Standard Schema validation. */
|
|
73
90
|
export type CreateFormOptions<TValues extends FormValues, TSubmitValues = TValues> =
|
|
74
91
|
| CreateFormOptionsWithoutSchema<TValues>
|
|
75
92
|
| CreateFormOptionsWithSchema<TValues, TSubmitValues>;
|
|
76
93
|
|
|
94
|
+
/** Reports either successful normalized submit values or form validation errors. */
|
|
77
95
|
export type FormValidationResult<TValues extends FormValues, TSubmitValues> =
|
|
78
96
|
| {
|
|
79
97
|
success: true;
|
|
@@ -84,6 +102,7 @@ export type FormValidationResult<TValues extends FormValues, TSubmitValues> =
|
|
|
84
102
|
success: false;
|
|
85
103
|
};
|
|
86
104
|
|
|
105
|
+
/** Reports the result of a form submit handler after validation and error capture. */
|
|
87
106
|
export type FormSubmitResult<TValues extends FormValues, TResult> =
|
|
88
107
|
| {
|
|
89
108
|
data: TResult;
|
|
@@ -101,11 +120,13 @@ export type FormSubmitResult<TValues extends FormValues, TResult> =
|
|
|
101
120
|
status: "error";
|
|
102
121
|
};
|
|
103
122
|
|
|
123
|
+
/** Represents field and form errors returned by a server action. */
|
|
104
124
|
export interface ServerActionErrors<TValues extends FormValues> {
|
|
105
125
|
fieldErrors?: Partial<Record<FieldName<TValues>, readonly string[]>> | undefined;
|
|
106
126
|
formErrors?: readonly string[] | undefined;
|
|
107
127
|
}
|
|
108
128
|
|
|
129
|
+
/** Provides reactive form state, field access, validation, submit, reset, and error controls. */
|
|
109
130
|
export interface FormApi<TValues extends FormValues, TSubmitValues> {
|
|
110
131
|
readonly state: ReadonlyCell<FormState<TValues>>;
|
|
111
132
|
field<Name extends FieldName<TValues>>(name: Name): FieldApi<TValues, Name>;
|
|
@@ -120,6 +141,7 @@ export interface FormApi<TValues extends FormValues, TSubmitValues> {
|
|
|
120
141
|
validate(): Promise<FormValidationResult<TValues, TSubmitValues>>;
|
|
121
142
|
}
|
|
122
143
|
|
|
144
|
+
/** Creates a reactive form API from initial values, validators, and optional schema validation. */
|
|
123
145
|
export function createForm<TValues extends FormValues>(
|
|
124
146
|
options: CreateFormOptionsWithoutSchema<TValues>,
|
|
125
147
|
): FormApi<TValues, TValues>;
|
|
@@ -512,9 +534,11 @@ function cloneValues<TValues extends FormValues>(values: TValues): TValues {
|
|
|
512
534
|
return { ...values };
|
|
513
535
|
}
|
|
514
536
|
|
|
537
|
+
/** Re-exports Standard Schema types used by form schema options. */
|
|
515
538
|
export type {
|
|
516
539
|
InferStandardSchemaInput,
|
|
517
540
|
InferStandardSchemaOutput,
|
|
518
541
|
StandardSchemaV1,
|
|
519
542
|
} from "./standard-schema.js";
|
|
543
|
+
/** Validates an unknown value with a Standard Schema and normalizes the result shape. */
|
|
520
544
|
export { validateStandardSchema } from "./standard-schema.js";
|
package/src/standard-schema.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
/** Represents the Standard Schema v1 wrapper shape used by compatible validation libraries. */
|
|
1
2
|
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
2
3
|
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
3
4
|
}
|
|
4
5
|
|
|
6
|
+
/** Contains the Standard Schema v1 protocol helper types. */
|
|
5
7
|
export namespace StandardSchemaV1 {
|
|
6
8
|
export interface Props<Input = unknown, Output = Input> {
|
|
7
9
|
readonly version: 1;
|
|
@@ -43,6 +45,7 @@ export namespace StandardSchemaV1 {
|
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
/** Infers the input value type accepted by a Standard Schema. */
|
|
46
49
|
export type InferStandardSchemaInput<Schema> = Schema extends {
|
|
47
50
|
readonly "~standard": { readonly types?: (infer Types) | undefined };
|
|
48
51
|
}
|
|
@@ -51,6 +54,7 @@ export type InferStandardSchemaInput<Schema> = Schema extends {
|
|
|
51
54
|
: never
|
|
52
55
|
: never;
|
|
53
56
|
|
|
57
|
+
/** Infers the output value type produced by a Standard Schema. */
|
|
54
58
|
export type InferStandardSchemaOutput<Schema> = Schema extends {
|
|
55
59
|
readonly "~standard": { readonly types?: (infer Types) | undefined };
|
|
56
60
|
}
|
|
@@ -59,6 +63,7 @@ export type InferStandardSchemaOutput<Schema> = Schema extends {
|
|
|
59
63
|
: never
|
|
60
64
|
: never;
|
|
61
65
|
|
|
66
|
+
/** Represents the normalized result returned after Standard Schema validation. */
|
|
62
67
|
export type StandardSchemaValidationResult<Output> =
|
|
63
68
|
| {
|
|
64
69
|
success: true;
|
|
@@ -69,6 +74,7 @@ export type StandardSchemaValidationResult<Output> =
|
|
|
69
74
|
success: false;
|
|
70
75
|
};
|
|
71
76
|
|
|
77
|
+
/** Validates an unknown value with a Standard Schema and normalizes success and issue results. */
|
|
72
78
|
export async function validateStandardSchema<Schema extends StandardSchemaV1>(
|
|
73
79
|
schema: Schema,
|
|
74
80
|
value: unknown,
|