@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.
- package/LICENSE +21 -0
- package/README.md +223 -0
- package/lib/analysis/arktype.js.html +5406 -0
- package/lib/analysis/index.js.html +5406 -0
- package/lib/analysis/valibot.js.html +5406 -0
- package/lib/analysis/zod.js.html +5406 -0
- package/lib/arktype.js +88 -0
- package/lib/arktype.js.map +1 -0
- package/lib/index.js +223 -0
- package/lib/index.js.map +1 -0
- package/lib/types/arktype.d.ts +90 -0
- package/lib/types/arktype.d.ts.map +1 -0
- package/lib/types/arktype2.d.ts +51 -0
- package/lib/types/arktype2.d.ts.map +1 -0
- package/lib/types/index.d.ts +195 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/valibot.d.ts +89 -0
- package/lib/types/valibot.d.ts.map +1 -0
- package/lib/types/valibot2.d.ts +51 -0
- package/lib/types/valibot2.d.ts.map +1 -0
- package/lib/types/zod.d.ts +86 -0
- package/lib/types/zod.d.ts.map +1 -0
- package/lib/types/zod2.d.ts +71 -0
- package/lib/types/zod2.d.ts.map +1 -0
- package/lib/valibot.js +87 -0
- package/lib/valibot.js.map +1 -0
- package/lib/zod.js +84 -0
- package/lib/zod.js.map +1 -0
- package/package.json +81 -0
- package/src/arktype.ts +103 -0
- package/src/index.ts +13 -0
- package/src/tests/setup.ts +1 -0
- package/src/tests/validation.test.ts +451 -0
- package/src/types.ts +33 -0
- package/src/utils.ts +21 -0
- package/src/valibot.ts +119 -0
- package/src/zod.ts +102 -0
package/src/zod.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SchemaValidateFn,
|
|
3
|
+
ValidateFn,
|
|
4
|
+
ValidationError,
|
|
5
|
+
} from '@pyreon/form'
|
|
6
|
+
import type { ValidationIssue } from './types'
|
|
7
|
+
import { issuesToRecord } from './utils'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Minimal Zod-compatible interfaces so we don't require zod as a hard dep.
|
|
11
|
+
* These match Zod v3's public API surface.
|
|
12
|
+
*/
|
|
13
|
+
interface ZodIssue {
|
|
14
|
+
path: PropertyKey[]
|
|
15
|
+
message: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Duck-typed Zod schema interface — works with both Zod v3 and v4.
|
|
20
|
+
* Inlines the result shape to avoid version-specific type mismatches.
|
|
21
|
+
*/
|
|
22
|
+
interface ZodSchema<T = unknown> {
|
|
23
|
+
safeParse(data: unknown): {
|
|
24
|
+
success: boolean
|
|
25
|
+
data?: T
|
|
26
|
+
error?: { issues: ZodIssue[] }
|
|
27
|
+
}
|
|
28
|
+
safeParseAsync(
|
|
29
|
+
data: unknown,
|
|
30
|
+
): Promise<{ success: boolean; data?: T; error?: { issues: ZodIssue[] } }>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function zodIssuesToGeneric(issues: ZodIssue[]): ValidationIssue[] {
|
|
34
|
+
return issues.map((issue) => ({
|
|
35
|
+
path: issue.path.map(String).join('.'),
|
|
36
|
+
message: issue.message,
|
|
37
|
+
}))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Create a form-level schema validator from a Zod schema.
|
|
42
|
+
* Supports both sync and async Zod schemas (uses `safeParseAsync`).
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* import { z } from 'zod'
|
|
46
|
+
* import { zodSchema } from '@pyreon/validation/zod'
|
|
47
|
+
*
|
|
48
|
+
* const schema = z.object({
|
|
49
|
+
* email: z.string().email(),
|
|
50
|
+
* password: z.string().min(8),
|
|
51
|
+
* })
|
|
52
|
+
*
|
|
53
|
+
* const form = useForm({
|
|
54
|
+
* initialValues: { email: '', password: '' },
|
|
55
|
+
* schema: zodSchema(schema),
|
|
56
|
+
* onSubmit: (values) => { ... },
|
|
57
|
+
* })
|
|
58
|
+
*/
|
|
59
|
+
export function zodSchema<TValues extends Record<string, unknown>>(
|
|
60
|
+
schema: ZodSchema<TValues>,
|
|
61
|
+
): SchemaValidateFn<TValues> {
|
|
62
|
+
return async (values: TValues) => {
|
|
63
|
+
try {
|
|
64
|
+
const result = await schema.safeParseAsync(values)
|
|
65
|
+
if (result.success)
|
|
66
|
+
return {} as Partial<Record<keyof TValues, ValidationError>>
|
|
67
|
+
return issuesToRecord<TValues>(zodIssuesToGeneric(result.error!.issues))
|
|
68
|
+
} catch (err) {
|
|
69
|
+
return {
|
|
70
|
+
'': err instanceof Error ? err.message : String(err),
|
|
71
|
+
} as Partial<Record<keyof TValues, ValidationError>>
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Create a single-field validator from a Zod schema.
|
|
78
|
+
* Supports both sync and async Zod refinements.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* import { z } from 'zod'
|
|
82
|
+
* import { zodField } from '@pyreon/validation/zod'
|
|
83
|
+
*
|
|
84
|
+
* const form = useForm({
|
|
85
|
+
* initialValues: { email: '' },
|
|
86
|
+
* validators: {
|
|
87
|
+
* email: zodField(z.string().email('Invalid email')),
|
|
88
|
+
* },
|
|
89
|
+
* onSubmit: (values) => { ... },
|
|
90
|
+
* })
|
|
91
|
+
*/
|
|
92
|
+
export function zodField<T>(schema: ZodSchema<T>): ValidateFn<T> {
|
|
93
|
+
return async (value: T) => {
|
|
94
|
+
try {
|
|
95
|
+
const result = await schema.safeParseAsync(value)
|
|
96
|
+
if (result.success) return undefined
|
|
97
|
+
return result.error!.issues[0]?.message
|
|
98
|
+
} catch (err) {
|
|
99
|
+
return err instanceof Error ? err.message : String(err)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|