@fluixi/forms-zod 0.2.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/LICENSE +21 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +82 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fluixi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Validator, ValidationError, FieldControl, FormControl } from '@fluixi/forms-core';
|
|
2
|
+
interface ZodIssue {
|
|
3
|
+
path: (string | number)[];
|
|
4
|
+
message: string;
|
|
5
|
+
code: string;
|
|
6
|
+
}
|
|
7
|
+
interface ZodResult<T> {
|
|
8
|
+
success: boolean;
|
|
9
|
+
data?: T;
|
|
10
|
+
error?: {
|
|
11
|
+
issues: ZodIssue[];
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
interface ZodLike<T> {
|
|
15
|
+
safeParse(value: unknown): ZodResult<T>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A group-level validator from a Zod schema. The returned errors carry `meta.path` so a
|
|
19
|
+
* UI can route each to its field; the group's `errors()` aggregates them.
|
|
20
|
+
*/
|
|
21
|
+
export declare function zodValidator<T>(schema: ZodLike<T>): Validator<T>;
|
|
22
|
+
/** Map a Zod safeParse failure to errors grouped by dotted field path (for routing to fields). */
|
|
23
|
+
export declare function zodErrorsByPath<T>(schema: ZodLike<T>, value: unknown): Record<string, ValidationError[]>;
|
|
24
|
+
/** Field-level validator from a Zod type (validates a single value). */
|
|
25
|
+
export declare function zodFieldValidator<T>(schema: ZodLike<T>, message?: string): Validator<T>;
|
|
26
|
+
export type InferZod<S> = S extends ZodLike<infer T> ? T : never;
|
|
27
|
+
interface ZodObjectLike<T> extends ZodLike<T> {
|
|
28
|
+
/** zod object's per-key sub-schemas. */
|
|
29
|
+
shape: {
|
|
30
|
+
[K in keyof T]: ZodLike<T[K]>;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Generate a fully-typed form from a Zod object schema.
|
|
35
|
+
*
|
|
36
|
+
* const Schema = z.object({ email: z.string().email(), age: z.number().min(18) });
|
|
37
|
+
* const form = fromZod(Schema, { email: '', age: 0 });
|
|
38
|
+
* form.get('email') // FieldControl<string>, validated by the email sub-schema
|
|
39
|
+
* form.value() // { email: string; age: number } (typed)
|
|
40
|
+
*
|
|
41
|
+
* Each key becomes a field validated by its sub-schema (field-level errors), and the whole
|
|
42
|
+
* schema is attached as a group-level validator too (so refinements / cross-field rules
|
|
43
|
+
* defined on the object surface as group errors, routed by `meta.path`).
|
|
44
|
+
*/
|
|
45
|
+
export declare function fromZod<T extends Record<string, unknown>>(schema: ZodObjectLike<T>, initial: T): FormControl<{
|
|
46
|
+
[K in keyof T]: FieldControl<T[K]>;
|
|
47
|
+
}>;
|
|
48
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fluixi/forms-zod — Zod adapter.
|
|
3
|
+
*
|
|
4
|
+
* Two integration levels:
|
|
5
|
+
* zodValidator(schema) — a form-level Validator that runs schema.safeParse and maps
|
|
6
|
+
* Zod issues to per-path ValidationErrors (attach to a group).
|
|
7
|
+
* zodField(schema, initial)— a single field whose validator is derived from a Zod type.
|
|
8
|
+
*
|
|
9
|
+
* Typing: `z.infer<typeof schema>` is the form value type, so values/errors stay typed.
|
|
10
|
+
*/
|
|
11
|
+
import { createField, createForm } from '@fluixi/forms-core';
|
|
12
|
+
const mapIssue = (issue) => ({
|
|
13
|
+
type: issue.code || 'zod',
|
|
14
|
+
message: issue.message,
|
|
15
|
+
meta: { path: issue.path.join('.') },
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* A group-level validator from a Zod schema. The returned errors carry `meta.path` so a
|
|
19
|
+
* UI can route each to its field; the group's `errors()` aggregates them.
|
|
20
|
+
*/
|
|
21
|
+
export function zodValidator(schema) {
|
|
22
|
+
return (value) => {
|
|
23
|
+
const res = schema.safeParse(value);
|
|
24
|
+
if (res.success)
|
|
25
|
+
return null;
|
|
26
|
+
const issues = res.error?.issues ?? [];
|
|
27
|
+
// Validators return one error; we fold the first and stash the rest in meta so the
|
|
28
|
+
// group sees the full set. (Field-level mapping uses `zodErrorsByPath` below.)
|
|
29
|
+
if (issues.length === 0)
|
|
30
|
+
return null;
|
|
31
|
+
return { ...mapIssue(issues[0]), meta: { ...issues[0], all: issues.map(mapIssue) } };
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Map a Zod safeParse failure to errors grouped by dotted field path (for routing to fields). */
|
|
35
|
+
export function zodErrorsByPath(schema, value) {
|
|
36
|
+
const res = schema.safeParse(value);
|
|
37
|
+
const out = {};
|
|
38
|
+
if (res.success)
|
|
39
|
+
return out;
|
|
40
|
+
for (const issue of res.error?.issues ?? []) {
|
|
41
|
+
const key = issue.path.join('.') || '';
|
|
42
|
+
(out[key] ??= []).push(mapIssue(issue));
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/** Field-level validator from a Zod type (validates a single value). */
|
|
47
|
+
export function zodFieldValidator(schema, message) {
|
|
48
|
+
return (value) => {
|
|
49
|
+
const res = schema.safeParse(value);
|
|
50
|
+
if (res.success)
|
|
51
|
+
return null;
|
|
52
|
+
const issue = res.error?.issues?.[0];
|
|
53
|
+
return issue ? { ...mapIssue(issue), message: message ?? issue.message } : { type: 'zod', message: message ?? 'Invalid' };
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Generate a fully-typed form from a Zod object schema.
|
|
58
|
+
*
|
|
59
|
+
* const Schema = z.object({ email: z.string().email(), age: z.number().min(18) });
|
|
60
|
+
* const form = fromZod(Schema, { email: '', age: 0 });
|
|
61
|
+
* form.get('email') // FieldControl<string>, validated by the email sub-schema
|
|
62
|
+
* form.value() // { email: string; age: number } (typed)
|
|
63
|
+
*
|
|
64
|
+
* Each key becomes a field validated by its sub-schema (field-level errors), and the whole
|
|
65
|
+
* schema is attached as a group-level validator too (so refinements / cross-field rules
|
|
66
|
+
* defined on the object surface as group errors, routed by `meta.path`).
|
|
67
|
+
*/
|
|
68
|
+
export function fromZod(schema, initial) {
|
|
69
|
+
const controls = {};
|
|
70
|
+
for (const key in schema.shape) {
|
|
71
|
+
// createField widens fresh literals (FieldControl<Widen<V>>); here V is the schema's
|
|
72
|
+
// concrete type, so Widen<V> ≡ V, but TS can't prove that for a generic — cast.
|
|
73
|
+
controls[key] = createField(initial[key], {
|
|
74
|
+
validators: [zodFieldValidator(schema.shape[key])],
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
// T is structurally GroupValue<{[K]: FieldControl<T[K]>}>, but TS can't equate the mapped
|
|
78
|
+
// type — cast the schema validator to the group's value validator.
|
|
79
|
+
return createForm(controls, {
|
|
80
|
+
validators: [zodValidator(schema)],
|
|
81
|
+
});
|
|
82
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fluixi/forms-zod",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Zod schema adapter for @fluixi/forms-core.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@fluixi/forms-core": "0.2.0"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"registry": "https://registry.npmjs.org/",
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/fluixi/forms.git",
|
|
29
|
+
"directory": "packages/forms-zod"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.json",
|
|
33
|
+
"test": "vitest run --passWithNoTests"
|
|
34
|
+
}
|
|
35
|
+
}
|