@pyreon/validation 0.0.1

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.
@@ -0,0 +1,195 @@
1
+ import { SchemaValidateFn, SchemaValidateFn as SchemaValidateFn$1, ValidateFn, ValidateFn as ValidateFn$1, ValidationError, ValidationError as ValidationError$1 } from "@pyreon/form";
2
+
3
+ //#region src/zod.d.ts
4
+ /**
5
+ * Minimal Zod-compatible interfaces so we don't require zod as a hard dep.
6
+ * These match Zod v3's public API surface.
7
+ */
8
+ interface ZodIssue {
9
+ path: PropertyKey[];
10
+ message: string;
11
+ }
12
+ /**
13
+ * Duck-typed Zod schema interface — works with both Zod v3 and v4.
14
+ * Inlines the result shape to avoid version-specific type mismatches.
15
+ */
16
+ interface ZodSchema<T = unknown> {
17
+ safeParse(data: unknown): {
18
+ success: boolean;
19
+ data?: T;
20
+ error?: {
21
+ issues: ZodIssue[];
22
+ };
23
+ };
24
+ safeParseAsync(data: unknown): Promise<{
25
+ success: boolean;
26
+ data?: T;
27
+ error?: {
28
+ issues: ZodIssue[];
29
+ };
30
+ }>;
31
+ }
32
+ /**
33
+ * Create a form-level schema validator from a Zod schema.
34
+ * Supports both sync and async Zod schemas (uses `safeParseAsync`).
35
+ *
36
+ * @example
37
+ * import { z } from 'zod'
38
+ * import { zodSchema } from '@pyreon/validation/zod'
39
+ *
40
+ * const schema = z.object({
41
+ * email: z.string().email(),
42
+ * password: z.string().min(8),
43
+ * })
44
+ *
45
+ * const form = useForm({
46
+ * initialValues: { email: '', password: '' },
47
+ * schema: zodSchema(schema),
48
+ * onSubmit: (values) => { ... },
49
+ * })
50
+ */
51
+ declare function zodSchema<TValues extends Record<string, unknown>>(schema: ZodSchema<TValues>): SchemaValidateFn$1<TValues>;
52
+ /**
53
+ * Create a single-field validator from a Zod schema.
54
+ * Supports both sync and async Zod refinements.
55
+ *
56
+ * @example
57
+ * import { z } from 'zod'
58
+ * import { zodField } from '@pyreon/validation/zod'
59
+ *
60
+ * const form = useForm({
61
+ * initialValues: { email: '' },
62
+ * validators: {
63
+ * email: zodField(z.string().email('Invalid email')),
64
+ * },
65
+ * onSubmit: (values) => { ... },
66
+ * })
67
+ */
68
+ declare function zodField<T>(schema: ZodSchema<T>): ValidateFn$1<T>;
69
+ //#endregion
70
+ //#region src/valibot.d.ts
71
+ /**
72
+ * Any function that takes (schema, input, ...rest) and returns a parse result.
73
+ * Valibot's safeParse/safeParseAsync have generic constraints on the schema
74
+ * parameter that can't be expressed without importing Valibot types. We accept
75
+ * any callable and cast internally.
76
+ */
77
+ type GenericSafeParseFn = Function;
78
+ /**
79
+ * Create a form-level schema validator from a Valibot schema.
80
+ *
81
+ * Valibot uses standalone functions rather than methods, so you must pass
82
+ * the `safeParseAsync` (or `safeParse`) function from valibot.
83
+ *
84
+ * @example
85
+ * import * as v from 'valibot'
86
+ * import { valibotSchema } from '@pyreon/validation/valibot'
87
+ *
88
+ * const schema = v.object({
89
+ * email: v.pipe(v.string(), v.email()),
90
+ * password: v.pipe(v.string(), v.minLength(8)),
91
+ * })
92
+ *
93
+ * const form = useForm({
94
+ * initialValues: { email: '', password: '' },
95
+ * schema: valibotSchema(schema, v.safeParseAsync),
96
+ * onSubmit: (values) => { ... },
97
+ * })
98
+ */
99
+ declare function valibotSchema<TValues extends Record<string, unknown>>(schema: unknown, safeParseFn: GenericSafeParseFn): SchemaValidateFn$1<TValues>;
100
+ /**
101
+ * Create a single-field validator from a Valibot schema.
102
+ *
103
+ * @example
104
+ * import * as v from 'valibot'
105
+ * import { valibotField } from '@pyreon/validation/valibot'
106
+ *
107
+ * const form = useForm({
108
+ * initialValues: { email: '' },
109
+ * validators: {
110
+ * email: valibotField(v.pipe(v.string(), v.email('Invalid email')), v.safeParseAsync),
111
+ * },
112
+ * onSubmit: (values) => { ... },
113
+ * })
114
+ */
115
+ declare function valibotField<T>(schema: unknown, safeParseFn: GenericSafeParseFn): ValidateFn$1<T>;
116
+ //#endregion
117
+ //#region src/arktype.d.ts
118
+ /**
119
+ * Internal callable interface matching ArkType's Type.
120
+ * Not exposed publicly — consumers pass their ArkType schema directly.
121
+ */
122
+ interface ArkTypeCallable {
123
+ (data: unknown): unknown;
124
+ }
125
+ /**
126
+ * Create a form-level schema validator from an ArkType schema.
127
+ *
128
+ * Accepts any callable ArkType `Type` instance. The schema is duck-typed —
129
+ * no ArkType import required.
130
+ *
131
+ * @example
132
+ * import { type } from 'arktype'
133
+ * import { arktypeSchema } from '@pyreon/validation/arktype'
134
+ *
135
+ * const schema = type({
136
+ * email: 'string.email',
137
+ * password: 'string >= 8',
138
+ * })
139
+ *
140
+ * const form = useForm({
141
+ * initialValues: { email: '', password: '' },
142
+ * schema: arktypeSchema(schema),
143
+ * onSubmit: (values) => { ... },
144
+ * })
145
+ */
146
+ declare function arktypeSchema<TValues extends Record<string, unknown>>(schema: ArkTypeCallable): SchemaValidateFn$1<TValues>;
147
+ /**
148
+ * Create a single-field validator from an ArkType schema.
149
+ *
150
+ * @example
151
+ * import { type } from 'arktype'
152
+ * import { arktypeField } from '@pyreon/validation/arktype'
153
+ *
154
+ * const form = useForm({
155
+ * initialValues: { email: '' },
156
+ * validators: {
157
+ * email: arktypeField(type('string.email')),
158
+ * },
159
+ * onSubmit: (values) => { ... },
160
+ * })
161
+ */
162
+ declare function arktypeField<T>(schema: ArkTypeCallable): ValidateFn$1<T>;
163
+ //#endregion
164
+ //#region src/types.d.ts
165
+ /**
166
+ * Generic issue produced by any schema library.
167
+ * Adapters normalize library-specific errors into this shape.
168
+ */
169
+ interface ValidationIssue {
170
+ /** Dot-separated field path (e.g. "address.city"). */
171
+ path: string;
172
+ /** Human-readable error message. */
173
+ message: string;
174
+ }
175
+ /**
176
+ * A generic schema adapter transforms library-specific parse results
177
+ * into a flat record of field → error message.
178
+ */
179
+ type SchemaAdapter<TSchema> = <TValues extends Record<string, unknown>>(schema: TSchema) => SchemaValidateFn<TValues>;
180
+ /**
181
+ * A generic field adapter transforms a library-specific schema
182
+ * into a single-field validator function.
183
+ */
184
+ type FieldAdapter<TSchema> = <T>(schema: TSchema) => ValidateFn<T>;
185
+ //#endregion
186
+ //#region src/utils.d.ts
187
+ /**
188
+ * Convert an array of validation issues into a flat field → error record.
189
+ * For nested paths like ["address", "city"], produces "address.city".
190
+ * When multiple issues exist for the same path, the first message wins.
191
+ */
192
+ declare function issuesToRecord<TValues extends Record<string, unknown>>(issues: ValidationIssue[]): Partial<Record<keyof TValues, ValidationError$1>>;
193
+ //#endregion
194
+ export { type FieldAdapter, type SchemaAdapter, type SchemaValidateFn, type ValidateFn, type ValidationError, type ValidationIssue, arktypeField, arktypeSchema, issuesToRecord, valibotField, valibotSchema, zodField, zodSchema };
195
+ //# sourceMappingURL=index2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index2.d.ts","names":[],"sources":["../../src/zod.ts","../../src/valibot.ts","../../src/arktype.ts","../../src/types.ts","../../src/utils.ts"],"mappings":";;;;;AAIqB;;UAQX,QAAA;EACR,IAAA,EAAM,WAAA;EACN,OAAA;AAAA;;;;AAAO;UAOC,SAAA;EACR,SAAA,CAAU,IAAA;IACR,OAAA;IACA,IAAA,GAAO,CAAA;IACP,KAAA;MAAU,MAAA,EAAQ,QAAA;IAAA;EAAA;EAEpB,cAAA,CACE,IAAA,YACC,OAAA;IAAU,OAAA;IAAkB,IAAA,GAAO,CAAA;IAAG,KAAA;MAAU,MAAA,EAAQ,QAAA;IAAA;EAAA;AAAA;;;;;;;;;;;;;;AA6B7D;;;;;;iBAAgB,SAAA,iBAA0B,MAAA,kBAAA,CACxC,MAAA,EAAQ,SAAA,CAAU,OAAA,IACjB,kBAAA,CAAiB,OAAA;;;;;;;;;;;;;AA+BpB;;;;iBAAgB,QAAA,GAAA,CAAY,MAAA,EAAQ,SAAA,CAAU,CAAA,IAAK,YAAA,CAAW,CAAA;;;;;AAvFzC;;;;KC6BhB,kBAAA,GAAqB,QAAA;;;;;ADnBjB;;;;;;;;;;;;;;;;;iBCsDO,aAAA,iBAA8B,MAAA,kBAAA,CAC5C,MAAA,WACA,WAAA,EAAa,kBAAA,GACZ,kBAAA,CAAiB,OAAA;;;;;;;;;;;;;ADbpB;;;iBC8CgB,YAAA,GAAA,CACd,MAAA,WACA,WAAA,EAAa,kBAAA,GACZ,YAAA,CAAW,CAAA;;;;;ADvGO;;UEoBX,eAAA;EAAA,CACP,IAAA;AAAA;;;;;AFXM;;;;;;;;;;;;;;;;;iBE8CO,aAAA,iBAA8B,MAAA,kBAAA,CAC5C,MAAA,EAAQ,eAAA,GACP,kBAAA,CAAiB,OAAA;;;;;;;;;;;;;AFJpB;;;iBEkCgB,YAAA,GAAA,CAAgB,MAAA,EAAQ,eAAA,GAAkB,YAAA,CAAW,CAAA;;;AFxFhD;;;;AAAA,UGSJ,eAAA;EHAT;EGEN,IAAA;EHDO;EGGP,OAAA;AAAA;;;;;KAOU,aAAA,6BAA0C,MAAA,mBACpD,MAAA,EAAQ,OAAA,KACL,gBAAA,CAAiB,OAAA;;;;;KAMV,YAAA,gBAA4B,MAAA,EAAQ,OAAA,KAAY,UAAA,CAAW,CAAA;;;;AH5BlD;;;;iBIIL,cAAA,iBAA+B,MAAA,kBAAA,CAC7C,MAAA,EAAQ,eAAA,KACP,OAAA,CAAQ,MAAA,OAAa,OAAA,EAAS,iBAAA"}
@@ -0,0 +1,89 @@
1
+ //#region src/utils.ts
2
+ /**
3
+ * Convert an array of validation issues into a flat field → error record.
4
+ * For nested paths like ["address", "city"], produces "address.city".
5
+ * When multiple issues exist for the same path, the first message wins.
6
+ */
7
+ function issuesToRecord(issues) {
8
+ const errors = {};
9
+ for (const issue of issues) {
10
+ const key = issue.path;
11
+ if (errors[key] === void 0) errors[key] = issue.message;
12
+ }
13
+ return errors;
14
+ }
15
+
16
+ //#endregion
17
+ //#region src/valibot.ts
18
+ function valibotIssuesToGeneric(issues) {
19
+ return issues.map(issue => ({
20
+ path: issue.path?.map(p => String(p.key)).join(".") ?? "",
21
+ message: issue.message
22
+ }));
23
+ }
24
+ /**
25
+ * Create a form-level schema validator from a Valibot schema.
26
+ *
27
+ * Valibot uses standalone functions rather than methods, so you must pass
28
+ * the `safeParseAsync` (or `safeParse`) function from valibot.
29
+ *
30
+ * @example
31
+ * import * as v from 'valibot'
32
+ * import { valibotSchema } from '@pyreon/validation/valibot'
33
+ *
34
+ * const schema = v.object({
35
+ * email: v.pipe(v.string(), v.email()),
36
+ * password: v.pipe(v.string(), v.minLength(8)),
37
+ * })
38
+ *
39
+ * const form = useForm({
40
+ * initialValues: { email: '', password: '' },
41
+ * schema: valibotSchema(schema, v.safeParseAsync),
42
+ * onSubmit: (values) => { ... },
43
+ * })
44
+ */
45
+ function valibotSchema(schema, safeParseFn) {
46
+ const parse = safeParseFn;
47
+ return async values => {
48
+ try {
49
+ const result = await parse(schema, values);
50
+ if (result.success) return {};
51
+ return issuesToRecord(valibotIssuesToGeneric(result.issues ?? []));
52
+ } catch (err) {
53
+ return {
54
+ "": err instanceof Error ? err.message : String(err)
55
+ };
56
+ }
57
+ };
58
+ }
59
+ /**
60
+ * Create a single-field validator from a Valibot schema.
61
+ *
62
+ * @example
63
+ * import * as v from 'valibot'
64
+ * import { valibotField } from '@pyreon/validation/valibot'
65
+ *
66
+ * const form = useForm({
67
+ * initialValues: { email: '' },
68
+ * validators: {
69
+ * email: valibotField(v.pipe(v.string(), v.email('Invalid email')), v.safeParseAsync),
70
+ * },
71
+ * onSubmit: (values) => { ... },
72
+ * })
73
+ */
74
+ function valibotField(schema, safeParseFn) {
75
+ const parse = safeParseFn;
76
+ return async value => {
77
+ try {
78
+ const result = await parse(schema, value);
79
+ if (result.success) return void 0;
80
+ return result.issues?.[0]?.message;
81
+ } catch (err) {
82
+ return err instanceof Error ? err.message : String(err);
83
+ }
84
+ };
85
+ }
86
+
87
+ //#endregion
88
+ export { valibotField, valibotSchema };
89
+ //# sourceMappingURL=valibot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valibot.d.ts","names":[],"sources":["../../src/utils.ts","../../src/valibot.ts"],"mappings":";;;;;;AAQA,SAAgB,cAAA,CACd,MAAA,EACiD;EACjD,MAAM,MAAA,GAAS,CAAA,CAAE;EACjB,KAAK,MAAM,KAAA,IAAS,MAAA,EAAQ;IAC1B,MAAM,GAAA,GAAM,KAAA,CAAM,IAAA;IAElB,IAAI,MAAA,CAAO,GAAA,CAAA,KAAS,KAAA,CAAA,EAClB,MAAA,CAAO,GAAA,CAAA,GAAO,KAAA,CAAM,OAAA;;EAGxB,OAAO,MAAA;;;;;ACgBT,SAAS,sBAAA,CAAuB,MAAA,EAA2C;EACzE,OAAO,MAAA,CAAO,GAAA,CAAK,KAAA,KAAW;IAC5B,IAAA,EAAM,KAAA,CAAM,IAAA,EAAM,GAAA,CAAK,CAAA,IAAM,MAAA,CAAO,CAAA,CAAE,GAAA,CAAI,CAAC,CAAC,IAAA,CAAK,GAAA,CAAI,IAAI,EAAA;IACzD,OAAA,EAAS,KAAA,CAAM;GAChB,CAAA,CAAE;;;;;;;;;;;;;;;;;;;;;;;AA6BL,SAAgB,aAAA,CACd,MAAA,EACA,WAAA,EAC2B;EAC3B,MAAM,KAAA,GAAQ,WAAA;EACd,OAAO,MAAO,MAAA,IAAoB;IAChC,IAAI;MACF,MAAM,MAAA,GAAS,MAAM,KAAA,CAAM,MAAA,EAAQ,MAAA,CAAO;MAC1C,IAAI,MAAA,CAAO,OAAA,EACT,OAAO,CAAA,CAAE;MACX,OAAO,cAAA,CACL,sBAAA,CAAuB,MAAA,CAAO,MAAA,IAAU,EAAE,CAAC,CAC5C;aACM,GAAA,EAAK;MACZ,OAAO;QACL,EAAA,EAAI,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,OAAA,GAAU,MAAA,CAAO,GAAA;MAAI,CACrD;;;;;;;;;;;;;;;;;;;AAoBP,SAAgB,YAAA,CACd,MAAA,EACA,WAAA,EACe;EACf,MAAM,KAAA,GAAQ,WAAA;EACd,OAAO,MAAO,KAAA,IAAa;IACzB,IAAI;MACF,MAAM,MAAA,GAAS,MAAM,KAAA,CAAM,MAAA,EAAQ,KAAA,CAAM;MACzC,IAAI,MAAA,CAAO,OAAA,EAAS,OAAO,KAAA,CAAA;MAC3B,OAAO,MAAA,CAAO,MAAA,GAAS,CAAA,CAAA,EAAI,OAAA;aACpB,GAAA,EAAK;MACZ,OAAO,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,OAAA,GAAU,MAAA,CAAO,GAAA,CAAI"}
@@ -0,0 +1,51 @@
1
+ import { SchemaValidateFn, ValidateFn } from "@pyreon/form";
2
+
3
+ //#region src/valibot.d.ts
4
+ /**
5
+ * Any function that takes (schema, input, ...rest) and returns a parse result.
6
+ * Valibot's safeParse/safeParseAsync have generic constraints on the schema
7
+ * parameter that can't be expressed without importing Valibot types. We accept
8
+ * any callable and cast internally.
9
+ */
10
+ type GenericSafeParseFn = Function;
11
+ /**
12
+ * Create a form-level schema validator from a Valibot schema.
13
+ *
14
+ * Valibot uses standalone functions rather than methods, so you must pass
15
+ * the `safeParseAsync` (or `safeParse`) function from valibot.
16
+ *
17
+ * @example
18
+ * import * as v from 'valibot'
19
+ * import { valibotSchema } from '@pyreon/validation/valibot'
20
+ *
21
+ * const schema = v.object({
22
+ * email: v.pipe(v.string(), v.email()),
23
+ * password: v.pipe(v.string(), v.minLength(8)),
24
+ * })
25
+ *
26
+ * const form = useForm({
27
+ * initialValues: { email: '', password: '' },
28
+ * schema: valibotSchema(schema, v.safeParseAsync),
29
+ * onSubmit: (values) => { ... },
30
+ * })
31
+ */
32
+ declare function valibotSchema<TValues extends Record<string, unknown>>(schema: unknown, safeParseFn: GenericSafeParseFn): SchemaValidateFn<TValues>;
33
+ /**
34
+ * Create a single-field validator from a Valibot schema.
35
+ *
36
+ * @example
37
+ * import * as v from 'valibot'
38
+ * import { valibotField } from '@pyreon/validation/valibot'
39
+ *
40
+ * const form = useForm({
41
+ * initialValues: { email: '' },
42
+ * validators: {
43
+ * email: valibotField(v.pipe(v.string(), v.email('Invalid email')), v.safeParseAsync),
44
+ * },
45
+ * onSubmit: (values) => { ... },
46
+ * })
47
+ */
48
+ declare function valibotField<T>(schema: unknown, safeParseFn: GenericSafeParseFn): ValidateFn<T>;
49
+ //#endregion
50
+ export { valibotField, valibotSchema };
51
+ //# sourceMappingURL=valibot2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valibot2.d.ts","names":[],"sources":["../../src/valibot.ts"],"mappings":";;;;;AAIqB;;;;KA6BhB,kBAAA,GAAqB,QAAA;AAmC1B;;;;;;;;;;;;;;;;;;AAoCA;;;AApCA,iBAAgB,aAAA,iBAA8B,MAAA,kBAAA,CAC5C,MAAA,WACA,WAAA,EAAa,kBAAA,GACZ,gBAAA,CAAiB,OAAA;;;;;;;;;;;;;;;;iBAiCJ,YAAA,GAAA,CACd,MAAA,WACA,WAAA,EAAa,kBAAA,GACZ,UAAA,CAAW,CAAA"}
@@ -0,0 +1,86 @@
1
+ //#region src/utils.ts
2
+ /**
3
+ * Convert an array of validation issues into a flat field → error record.
4
+ * For nested paths like ["address", "city"], produces "address.city".
5
+ * When multiple issues exist for the same path, the first message wins.
6
+ */
7
+ function issuesToRecord(issues) {
8
+ const errors = {};
9
+ for (const issue of issues) {
10
+ const key = issue.path;
11
+ if (errors[key] === void 0) errors[key] = issue.message;
12
+ }
13
+ return errors;
14
+ }
15
+
16
+ //#endregion
17
+ //#region src/zod.ts
18
+ function zodIssuesToGeneric(issues) {
19
+ return issues.map(issue => ({
20
+ path: issue.path.map(String).join("."),
21
+ message: issue.message
22
+ }));
23
+ }
24
+ /**
25
+ * Create a form-level schema validator from a Zod schema.
26
+ * Supports both sync and async Zod schemas (uses `safeParseAsync`).
27
+ *
28
+ * @example
29
+ * import { z } from 'zod'
30
+ * import { zodSchema } from '@pyreon/validation/zod'
31
+ *
32
+ * const schema = z.object({
33
+ * email: z.string().email(),
34
+ * password: z.string().min(8),
35
+ * })
36
+ *
37
+ * const form = useForm({
38
+ * initialValues: { email: '', password: '' },
39
+ * schema: zodSchema(schema),
40
+ * onSubmit: (values) => { ... },
41
+ * })
42
+ */
43
+ function zodSchema(schema) {
44
+ return async values => {
45
+ try {
46
+ const result = await schema.safeParseAsync(values);
47
+ if (result.success) return {};
48
+ return issuesToRecord(zodIssuesToGeneric(result.error.issues));
49
+ } catch (err) {
50
+ return {
51
+ "": err instanceof Error ? err.message : String(err)
52
+ };
53
+ }
54
+ };
55
+ }
56
+ /**
57
+ * Create a single-field validator from a Zod schema.
58
+ * Supports both sync and async Zod refinements.
59
+ *
60
+ * @example
61
+ * import { z } from 'zod'
62
+ * import { zodField } from '@pyreon/validation/zod'
63
+ *
64
+ * const form = useForm({
65
+ * initialValues: { email: '' },
66
+ * validators: {
67
+ * email: zodField(z.string().email('Invalid email')),
68
+ * },
69
+ * onSubmit: (values) => { ... },
70
+ * })
71
+ */
72
+ function zodField(schema) {
73
+ return async value => {
74
+ try {
75
+ const result = await schema.safeParseAsync(value);
76
+ if (result.success) return void 0;
77
+ return result.error.issues[0]?.message;
78
+ } catch (err) {
79
+ return err instanceof Error ? err.message : String(err);
80
+ }
81
+ };
82
+ }
83
+
84
+ //#endregion
85
+ export { zodField, zodSchema };
86
+ //# sourceMappingURL=zod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod.d.ts","names":[],"sources":["../../src/utils.ts","../../src/zod.ts"],"mappings":";;;;;;AAQA,SAAgB,cAAA,CACd,MAAA,EACiD;EACjD,MAAM,MAAA,GAAS,CAAA,CAAE;EACjB,KAAK,MAAM,KAAA,IAAS,MAAA,EAAQ;IAC1B,MAAM,GAAA,GAAM,KAAA,CAAM,IAAA;IAElB,IAAI,MAAA,CAAO,GAAA,CAAA,KAAS,KAAA,CAAA,EAClB,MAAA,CAAO,GAAA,CAAA,GAAO,KAAA,CAAM,OAAA;;EAGxB,OAAO,MAAA;;;;;ACaT,SAAS,kBAAA,CAAmB,MAAA,EAAuC;EACjE,OAAO,MAAA,CAAO,GAAA,CAAK,KAAA,KAAW;IAC5B,IAAA,EAAM,KAAA,CAAM,IAAA,CAAK,GAAA,CAAI,MAAA,CAAO,CAAC,IAAA,CAAK,GAAA,CAAI;IACtC,OAAA,EAAS,KAAA,CAAM;GAChB,CAAA,CAAE;;;;;;;;;;;;;;;;;;;;;AAsBL,SAAgB,SAAA,CACd,MAAA,EAC2B;EAC3B,OAAO,MAAO,MAAA,IAAoB;IAChC,IAAI;MACF,MAAM,MAAA,GAAS,MAAM,MAAA,CAAO,cAAA,CAAe,MAAA,CAAO;MAClD,IAAI,MAAA,CAAO,OAAA,EACT,OAAO,CAAA,CAAE;MACX,OAAO,cAAA,CAAwB,kBAAA,CAAmB,MAAA,CAAO,KAAA,CAAO,MAAA,CAAO,CAAC;aACjE,GAAA,EAAK;MACZ,OAAO;QACL,EAAA,EAAI,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,OAAA,GAAU,MAAA,CAAO,GAAA;MAAI,CACrD;;;;;;;;;;;;;;;;;;;;AAqBP,SAAgB,QAAA,CAAY,MAAA,EAAqC;EAC/D,OAAO,MAAO,KAAA,IAAa;IACzB,IAAI;MACF,MAAM,MAAA,GAAS,MAAM,MAAA,CAAO,cAAA,CAAe,KAAA,CAAM;MACjD,IAAI,MAAA,CAAO,OAAA,EAAS,OAAO,KAAA,CAAA;MAC3B,OAAO,MAAA,CAAO,KAAA,CAAO,MAAA,CAAO,CAAA,CAAA,EAAI,OAAA;aACzB,GAAA,EAAK;MACZ,OAAO,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,OAAA,GAAU,MAAA,CAAO,GAAA,CAAI"}
@@ -0,0 +1,71 @@
1
+ import { SchemaValidateFn, ValidateFn } from "@pyreon/form";
2
+
3
+ //#region src/zod.d.ts
4
+ /**
5
+ * Minimal Zod-compatible interfaces so we don't require zod as a hard dep.
6
+ * These match Zod v3's public API surface.
7
+ */
8
+ interface ZodIssue {
9
+ path: PropertyKey[];
10
+ message: string;
11
+ }
12
+ /**
13
+ * Duck-typed Zod schema interface — works with both Zod v3 and v4.
14
+ * Inlines the result shape to avoid version-specific type mismatches.
15
+ */
16
+ interface ZodSchema<T = unknown> {
17
+ safeParse(data: unknown): {
18
+ success: boolean;
19
+ data?: T;
20
+ error?: {
21
+ issues: ZodIssue[];
22
+ };
23
+ };
24
+ safeParseAsync(data: unknown): Promise<{
25
+ success: boolean;
26
+ data?: T;
27
+ error?: {
28
+ issues: ZodIssue[];
29
+ };
30
+ }>;
31
+ }
32
+ /**
33
+ * Create a form-level schema validator from a Zod schema.
34
+ * Supports both sync and async Zod schemas (uses `safeParseAsync`).
35
+ *
36
+ * @example
37
+ * import { z } from 'zod'
38
+ * import { zodSchema } from '@pyreon/validation/zod'
39
+ *
40
+ * const schema = z.object({
41
+ * email: z.string().email(),
42
+ * password: z.string().min(8),
43
+ * })
44
+ *
45
+ * const form = useForm({
46
+ * initialValues: { email: '', password: '' },
47
+ * schema: zodSchema(schema),
48
+ * onSubmit: (values) => { ... },
49
+ * })
50
+ */
51
+ declare function zodSchema<TValues extends Record<string, unknown>>(schema: ZodSchema<TValues>): SchemaValidateFn<TValues>;
52
+ /**
53
+ * Create a single-field validator from a Zod schema.
54
+ * Supports both sync and async Zod refinements.
55
+ *
56
+ * @example
57
+ * import { z } from 'zod'
58
+ * import { zodField } from '@pyreon/validation/zod'
59
+ *
60
+ * const form = useForm({
61
+ * initialValues: { email: '' },
62
+ * validators: {
63
+ * email: zodField(z.string().email('Invalid email')),
64
+ * },
65
+ * onSubmit: (values) => { ... },
66
+ * })
67
+ */
68
+ declare function zodField<T>(schema: ZodSchema<T>): ValidateFn<T>;
69
+ //#endregion
70
+ export { zodField, zodSchema };
71
+ //# sourceMappingURL=zod2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod2.d.ts","names":[],"sources":["../../src/zod.ts"],"mappings":";;;;;AAIqB;;UAQX,QAAA;EACR,IAAA,EAAM,WAAA;EACN,OAAA;AAAA;;;;AAAO;UAOC,SAAA;EACR,SAAA,CAAU,IAAA;IACR,OAAA;IACA,IAAA,GAAO,CAAA;IACP,KAAA;MAAU,MAAA,EAAQ,QAAA;IAAA;EAAA;EAEpB,cAAA,CACE,IAAA,YACC,OAAA;IAAU,OAAA;IAAkB,IAAA,GAAO,CAAA;IAAG,KAAA;MAAU,MAAA,EAAQ,QAAA;IAAA;EAAA;AAAA;;;;;;;;;;;;;;AA6B7D;;;;;;iBAAgB,SAAA,iBAA0B,MAAA,kBAAA,CACxC,MAAA,EAAQ,SAAA,CAAU,OAAA,IACjB,gBAAA,CAAiB,OAAA;;;;;;;;;;;;;AA+BpB;;;;iBAAgB,QAAA,GAAA,CAAY,MAAA,EAAQ,SAAA,CAAU,CAAA,IAAK,UAAA,CAAW,CAAA"}
package/lib/valibot.js ADDED
@@ -0,0 +1,87 @@
1
+ //#region src/utils.ts
2
+ /**
3
+ * Convert an array of validation issues into a flat field → error record.
4
+ * For nested paths like ["address", "city"], produces "address.city".
5
+ * When multiple issues exist for the same path, the first message wins.
6
+ */
7
+ function issuesToRecord(issues) {
8
+ const errors = {};
9
+ for (const issue of issues) {
10
+ const key = issue.path;
11
+ if (errors[key] === void 0) errors[key] = issue.message;
12
+ }
13
+ return errors;
14
+ }
15
+
16
+ //#endregion
17
+ //#region src/valibot.ts
18
+ function valibotIssuesToGeneric(issues) {
19
+ return issues.map((issue) => ({
20
+ path: issue.path?.map((p) => String(p.key)).join(".") ?? "",
21
+ message: issue.message
22
+ }));
23
+ }
24
+ /**
25
+ * Create a form-level schema validator from a Valibot schema.
26
+ *
27
+ * Valibot uses standalone functions rather than methods, so you must pass
28
+ * the `safeParseAsync` (or `safeParse`) function from valibot.
29
+ *
30
+ * @example
31
+ * import * as v from 'valibot'
32
+ * import { valibotSchema } from '@pyreon/validation/valibot'
33
+ *
34
+ * const schema = v.object({
35
+ * email: v.pipe(v.string(), v.email()),
36
+ * password: v.pipe(v.string(), v.minLength(8)),
37
+ * })
38
+ *
39
+ * const form = useForm({
40
+ * initialValues: { email: '', password: '' },
41
+ * schema: valibotSchema(schema, v.safeParseAsync),
42
+ * onSubmit: (values) => { ... },
43
+ * })
44
+ */
45
+ function valibotSchema(schema, safeParseFn) {
46
+ const parse = safeParseFn;
47
+ return async (values) => {
48
+ try {
49
+ const result = await parse(schema, values);
50
+ if (result.success) return {};
51
+ return issuesToRecord(valibotIssuesToGeneric(result.issues ?? []));
52
+ } catch (err) {
53
+ return { "": err instanceof Error ? err.message : String(err) };
54
+ }
55
+ };
56
+ }
57
+ /**
58
+ * Create a single-field validator from a Valibot schema.
59
+ *
60
+ * @example
61
+ * import * as v from 'valibot'
62
+ * import { valibotField } from '@pyreon/validation/valibot'
63
+ *
64
+ * const form = useForm({
65
+ * initialValues: { email: '' },
66
+ * validators: {
67
+ * email: valibotField(v.pipe(v.string(), v.email('Invalid email')), v.safeParseAsync),
68
+ * },
69
+ * onSubmit: (values) => { ... },
70
+ * })
71
+ */
72
+ function valibotField(schema, safeParseFn) {
73
+ const parse = safeParseFn;
74
+ return async (value) => {
75
+ try {
76
+ const result = await parse(schema, value);
77
+ if (result.success) return void 0;
78
+ return result.issues?.[0]?.message;
79
+ } catch (err) {
80
+ return err instanceof Error ? err.message : String(err);
81
+ }
82
+ };
83
+ }
84
+
85
+ //#endregion
86
+ export { valibotField, valibotSchema };
87
+ //# sourceMappingURL=valibot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valibot.js","names":[],"sources":["../src/utils.ts","../src/valibot.ts"],"sourcesContent":["import type { ValidationError } from '@pyreon/form'\nimport type { ValidationIssue } from './types'\n\n/**\n * Convert an array of validation issues into a flat field → error record.\n * For nested paths like [\"address\", \"city\"], produces \"address.city\".\n * When multiple issues exist for the same path, the first message wins.\n */\nexport function issuesToRecord<TValues extends Record<string, unknown>>(\n issues: ValidationIssue[],\n): Partial<Record<keyof TValues, ValidationError>> {\n const errors = {} as Partial<Record<keyof TValues, ValidationError>>\n for (const issue of issues) {\n const key = issue.path as keyof TValues\n // First error per field wins\n if (errors[key] === undefined) {\n errors[key] = issue.message\n }\n }\n return errors\n}\n","import type {\n SchemaValidateFn,\n ValidateFn,\n ValidationError,\n} from '@pyreon/form'\nimport type { ValidationIssue } from './types'\nimport { issuesToRecord } from './utils'\n\n/**\n * Minimal Valibot-compatible interfaces so we don't require valibot as a hard dep.\n */\ninterface ValibotPathItem {\n key: string | number\n}\n\ninterface ValibotIssue {\n path?: ValibotPathItem[]\n message: string\n}\n\ninterface ValibotSafeParseResult {\n success: boolean\n output?: unknown\n issues?: ValibotIssue[]\n}\n\n/**\n * Any function that takes (schema, input, ...rest) and returns a parse result.\n * Valibot's safeParse/safeParseAsync have generic constraints on the schema\n * parameter that can't be expressed without importing Valibot types. We accept\n * any callable and cast internally.\n */\n// biome-ignore lint/complexity/noBannedTypes: must accept any valibot parse function\ntype GenericSafeParseFn = Function\n\nfunction valibotIssuesToGeneric(issues: ValibotIssue[]): ValidationIssue[] {\n return issues.map((issue) => ({\n path: issue.path?.map((p) => String(p.key)).join('.') ?? '',\n message: issue.message,\n }))\n}\n\ntype InternalParseFn = (\n schema: unknown,\n input: unknown,\n) => ValibotSafeParseResult | Promise<ValibotSafeParseResult>\n\n/**\n * Create a form-level schema validator from a Valibot schema.\n *\n * Valibot uses standalone functions rather than methods, so you must pass\n * the `safeParseAsync` (or `safeParse`) function from valibot.\n *\n * @example\n * import * as v from 'valibot'\n * import { valibotSchema } from '@pyreon/validation/valibot'\n *\n * const schema = v.object({\n * email: v.pipe(v.string(), v.email()),\n * password: v.pipe(v.string(), v.minLength(8)),\n * })\n *\n * const form = useForm({\n * initialValues: { email: '', password: '' },\n * schema: valibotSchema(schema, v.safeParseAsync),\n * onSubmit: (values) => { ... },\n * })\n */\nexport function valibotSchema<TValues extends Record<string, unknown>>(\n schema: unknown,\n safeParseFn: GenericSafeParseFn,\n): SchemaValidateFn<TValues> {\n const parse = safeParseFn as InternalParseFn\n return async (values: TValues) => {\n try {\n const result = await parse(schema, values)\n if (result.success)\n return {} as Partial<Record<keyof TValues, ValidationError>>\n return issuesToRecord<TValues>(\n valibotIssuesToGeneric(result.issues ?? []),\n )\n } catch (err) {\n return {\n '': err instanceof Error ? err.message : String(err),\n } as Partial<Record<keyof TValues, ValidationError>>\n }\n }\n}\n\n/**\n * Create a single-field validator from a Valibot schema.\n *\n * @example\n * import * as v from 'valibot'\n * import { valibotField } from '@pyreon/validation/valibot'\n *\n * const form = useForm({\n * initialValues: { email: '' },\n * validators: {\n * email: valibotField(v.pipe(v.string(), v.email('Invalid email')), v.safeParseAsync),\n * },\n * onSubmit: (values) => { ... },\n * })\n */\nexport function valibotField<T>(\n schema: unknown,\n safeParseFn: GenericSafeParseFn,\n): ValidateFn<T> {\n const parse = safeParseFn as InternalParseFn\n return async (value: T) => {\n try {\n const result = await parse(schema, value)\n if (result.success) return undefined\n return result.issues?.[0]?.message\n } catch (err) {\n return err instanceof Error ? err.message : String(err)\n }\n }\n}\n"],"mappings":";;;;;;AAQA,SAAgB,eACd,QACiD;CACjD,MAAM,SAAS,EAAE;AACjB,MAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,MAAM,MAAM;AAElB,MAAI,OAAO,SAAS,OAClB,QAAO,OAAO,MAAM;;AAGxB,QAAO;;;;;ACgBT,SAAS,uBAAuB,QAA2C;AACzE,QAAO,OAAO,KAAK,WAAW;EAC5B,MAAM,MAAM,MAAM,KAAK,MAAM,OAAO,EAAE,IAAI,CAAC,CAAC,KAAK,IAAI,IAAI;EACzD,SAAS,MAAM;EAChB,EAAE;;;;;;;;;;;;;;;;;;;;;;;AA6BL,SAAgB,cACd,QACA,aAC2B;CAC3B,MAAM,QAAQ;AACd,QAAO,OAAO,WAAoB;AAChC,MAAI;GACF,MAAM,SAAS,MAAM,MAAM,QAAQ,OAAO;AAC1C,OAAI,OAAO,QACT,QAAO,EAAE;AACX,UAAO,eACL,uBAAuB,OAAO,UAAU,EAAE,CAAC,CAC5C;WACM,KAAK;AACZ,UAAO,EACL,IAAI,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI,EACrD;;;;;;;;;;;;;;;;;;;AAoBP,SAAgB,aACd,QACA,aACe;CACf,MAAM,QAAQ;AACd,QAAO,OAAO,UAAa;AACzB,MAAI;GACF,MAAM,SAAS,MAAM,MAAM,QAAQ,MAAM;AACzC,OAAI,OAAO,QAAS,QAAO;AAC3B,UAAO,OAAO,SAAS,IAAI;WACpB,KAAK;AACZ,UAAO,eAAe,QAAQ,IAAI,UAAU,OAAO,IAAI"}