@fluixi/forms-core 0.2.1-alpha.1 → 0.2.1-alpha.2

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 CHANGED
@@ -11,4 +11,5 @@ export { createField } from './field.js';
11
11
  export { createGroup, createForm } from './group.js';
12
12
  export { createFormArray, keyOf } from './array.js';
13
13
  export * from './validators.js';
14
+ export * from './standard.js';
14
15
  export type { Accessor, ValidationError, ValidatorContext, Validator, AsyncValidator, UpdateOptions, ControlKind, Control, AnyControl, FieldOptions, FieldControl, ControlsRecord, GroupValue, GroupOptions, GroupControl, ArrayOptions, ArrayControl, FormControl, PartialDeep, Path, } from './types.js';
package/dist/index.js CHANGED
@@ -11,3 +11,4 @@ export { createField } from './field.js';
11
11
  export { createGroup, createForm } from './group.js';
12
12
  export { createFormArray, keyOf } from './array.js';
13
13
  export * from './validators.js';
14
+ export * from './standard.js';
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Standard Schema (https://standardschema.dev): one adapter for every library that implements
3
+ * it, Zod 4, Valibot, ArkType among them. Only the interface is declared here, so nothing is
4
+ * installed for it.
5
+ */
6
+ import type { AsyncValidator, ValidationError, Validator } from './types.js';
7
+ /** A path segment: a key, or an object carrying one. */
8
+ export type StandardPathSegment = PropertyKey | {
9
+ readonly key: PropertyKey;
10
+ };
11
+ export interface StandardIssue {
12
+ readonly message: string;
13
+ readonly path?: ReadonlyArray<StandardPathSegment>;
14
+ }
15
+ export type StandardResult<Output> = {
16
+ readonly value: Output;
17
+ readonly issues?: undefined;
18
+ } | {
19
+ readonly issues: ReadonlyArray<StandardIssue>;
20
+ };
21
+ /** The Standard Schema v1 interface, as far as validation needs it. */
22
+ export interface StandardSchema<Input = unknown, Output = Input> {
23
+ readonly '~standard': {
24
+ readonly version: 1;
25
+ readonly vendor: string;
26
+ readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
27
+ readonly types?: {
28
+ readonly input: Input;
29
+ readonly output: Output;
30
+ };
31
+ };
32
+ }
33
+ /** The output type a Standard Schema validates to. */
34
+ export type InferStandard<S> = S extends StandardSchema<unknown, infer O> ? O : never;
35
+ /** `a.0.b`. Through String(): a symbol key cannot be joined directly. */
36
+ export declare function standardPath(path?: ReadonlyArray<StandardPathSegment>): string;
37
+ /**
38
+ * A validator from a Standard Schema, for a field or a whole form. Its first issue becomes the
39
+ * error; `meta.path` says where it belongs. A schema that validates asynchronously belongs in
40
+ * `asyncValidators` through `standardAsyncValidator`: here it is reported as an error rather
41
+ * than passing unchecked.
42
+ */
43
+ export declare function standardValidator<T>(schema: StandardSchema<unknown, T>): Validator<T>;
44
+ /** The asynchronous form, for `asyncValidators`: debounced and aborted like any other. */
45
+ export declare function standardAsyncValidator<T>(schema: StandardSchema<unknown, T>): AsyncValidator<T>;
46
+ /** Every issue of a failed validation, grouped by dotted path, for routing to fields. */
47
+ export declare function standardErrorsByPath(schema: StandardSchema, value: unknown): Promise<Record<string, ValidationError[]>>;
@@ -0,0 +1,40 @@
1
+ /** `a.0.b`. Through String(): a symbol key cannot be joined directly. */
2
+ export function standardPath(path = []) {
3
+ return path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.');
4
+ }
5
+ const toError = (issue) => ({
6
+ type: 'schema',
7
+ message: issue.message,
8
+ meta: { path: standardPath(issue.path) },
9
+ });
10
+ const firstError = (result) => result.issues?.length ? toError(result.issues[0]) : null;
11
+ function isPromise(value) {
12
+ return typeof value?.then === 'function';
13
+ }
14
+ /**
15
+ * A validator from a Standard Schema, for a field or a whole form. Its first issue becomes the
16
+ * error; `meta.path` says where it belongs. A schema that validates asynchronously belongs in
17
+ * `asyncValidators` through `standardAsyncValidator`: here it is reported as an error rather
18
+ * than passing unchecked.
19
+ */
20
+ export function standardValidator(schema) {
21
+ return (value) => {
22
+ const result = schema['~standard'].validate(value);
23
+ if (isPromise(result)) {
24
+ return { type: 'schema', message: 'This schema validates asynchronously: use standardAsyncValidator.' };
25
+ }
26
+ return firstError(result);
27
+ };
28
+ }
29
+ /** The asynchronous form, for `asyncValidators`: debounced and aborted like any other. */
30
+ export function standardAsyncValidator(schema) {
31
+ return async (value) => firstError(await schema['~standard'].validate(value));
32
+ }
33
+ /** Every issue of a failed validation, grouped by dotted path, for routing to fields. */
34
+ export async function standardErrorsByPath(schema, value) {
35
+ const result = await schema['~standard'].validate(value);
36
+ const out = {};
37
+ for (const issue of result.issues ?? [])
38
+ (out[standardPath(issue.path)] ??= []).push(toError(issue));
39
+ return out;
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluixi/forms-core",
3
- "version": "0.2.1-alpha.1",
3
+ "version": "0.2.1-alpha.2",
4
4
  "license": "MIT",
5
5
  "description": "Reactive forms core (FormControl/FormGroup/FormArray) for fine-grained signal frameworks.",
6
6
  "type": "module",