@flightdev/forms 0.0.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/LICENSE +21 -0
- package/README.md +210 -0
- package/dist/adapters/valibot.d.ts +38 -0
- package/dist/adapters/valibot.js +66 -0
- package/dist/adapters/valibot.js.map +1 -0
- package/dist/adapters/yup.d.ts +44 -0
- package/dist/adapters/yup.js +56 -0
- package/dist/adapters/yup.js.map +1 -0
- package/dist/adapters/zod.d.ts +83 -0
- package/dist/adapters/zod.js +47 -0
- package/dist/adapters/zod.js.map +1 -0
- package/dist/chunk-5LLYBEXV.js +110 -0
- package/dist/chunk-5LLYBEXV.js.map +1 -0
- package/dist/chunk-DGUM43GV.js +10 -0
- package/dist/chunk-DGUM43GV.js.map +1 -0
- package/dist/frameworks/react.d.ts +85 -0
- package/dist/frameworks/react.js +195 -0
- package/dist/frameworks/react.js.map +1 -0
- package/dist/frameworks/solid.d.ts +63 -0
- package/dist/frameworks/solid.js +138 -0
- package/dist/frameworks/solid.js.map +1 -0
- package/dist/frameworks/svelte.d.ts +61 -0
- package/dist/frameworks/svelte.js +143 -0
- package/dist/frameworks/svelte.js.map +1 -0
- package/dist/frameworks/vue.d.ts +60 -0
- package/dist/frameworks/vue.js +127 -0
- package/dist/frameworks/vue.js.map +1 -0
- package/dist/index.d.ts +181 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/package.json +102 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Flight Contributors
|
|
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/README.md
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# @flight-framework/forms
|
|
2
|
+
|
|
3
|
+
Form handling package for Flight Framework with Zod, Yup, and Valibot validation adapters.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @flight-framework/forms
|
|
9
|
+
|
|
10
|
+
# Install your preferred validator:
|
|
11
|
+
npm install zod # TypeScript-first
|
|
12
|
+
npm install yup # Schema-based
|
|
13
|
+
npm install valibot # Lightweight
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { createForm } from '@flight-framework/forms';
|
|
20
|
+
import { zod } from '@flight-framework/forms/zod';
|
|
21
|
+
import { z } from 'zod';
|
|
22
|
+
|
|
23
|
+
const contactForm = createForm({
|
|
24
|
+
validator: zod(z.object({
|
|
25
|
+
email: z.string().email(),
|
|
26
|
+
message: z.string().min(10),
|
|
27
|
+
})),
|
|
28
|
+
action: async (data) => {
|
|
29
|
+
await sendEmail(data.email, data.message);
|
|
30
|
+
return { success: true };
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Validation Adapters
|
|
36
|
+
|
|
37
|
+
### Zod
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { zod } from '@flight-framework/forms/zod';
|
|
41
|
+
import { z } from 'zod';
|
|
42
|
+
|
|
43
|
+
const validator = zod(z.object({
|
|
44
|
+
email: z.string().email('Invalid email'),
|
|
45
|
+
password: z.string().min(8, 'Too short'),
|
|
46
|
+
}));
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Yup
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { yup } from '@flight-framework/forms/yup';
|
|
53
|
+
import * as y from 'yup';
|
|
54
|
+
|
|
55
|
+
const validator = yup(y.object({
|
|
56
|
+
email: y.string().email().required(),
|
|
57
|
+
password: y.string().min(8).required(),
|
|
58
|
+
}));
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Valibot
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { valibot } from '@flight-framework/forms/valibot';
|
|
65
|
+
import * as v from 'valibot';
|
|
66
|
+
|
|
67
|
+
const validator = valibot(v.object({
|
|
68
|
+
email: v.pipe(v.string(), v.email()),
|
|
69
|
+
password: v.pipe(v.string(), v.minLength(8)),
|
|
70
|
+
}));
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Framework Integration
|
|
74
|
+
|
|
75
|
+
### React
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
import { useForm, Form, Field } from '@flight-framework/forms/react';
|
|
79
|
+
|
|
80
|
+
function ContactPage() {
|
|
81
|
+
const { register, handleSubmit, errors, pending } = useForm(contactForm);
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<Form onSubmit={handleSubmit}>
|
|
85
|
+
<input {...register('email')} />
|
|
86
|
+
{errors.email && <span>{errors.email}</span>}
|
|
87
|
+
|
|
88
|
+
<textarea {...register('message')} />
|
|
89
|
+
{errors.message && <span>{errors.message}</span>}
|
|
90
|
+
|
|
91
|
+
<button disabled={pending}>Send</button>
|
|
92
|
+
</Form>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Vue
|
|
98
|
+
|
|
99
|
+
```vue
|
|
100
|
+
<script setup>
|
|
101
|
+
import { useForm } from '@flight-framework/forms/vue';
|
|
102
|
+
|
|
103
|
+
const { register, handleSubmit, errors, pending } = useForm(contactForm);
|
|
104
|
+
</script>
|
|
105
|
+
|
|
106
|
+
<template>
|
|
107
|
+
<form @submit.prevent="handleSubmit">
|
|
108
|
+
<input v-bind="register('email')" />
|
|
109
|
+
<span v-if="errors.email">{{ errors.email }}</span>
|
|
110
|
+
|
|
111
|
+
<button :disabled="pending">Send</button>
|
|
112
|
+
</form>
|
|
113
|
+
</template>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Svelte
|
|
117
|
+
|
|
118
|
+
```svelte
|
|
119
|
+
<script>
|
|
120
|
+
import { createFormStore } from '@flight-framework/forms/svelte';
|
|
121
|
+
|
|
122
|
+
const { values, errors, pending, register, submit } = createFormStore(contactForm);
|
|
123
|
+
</script>
|
|
124
|
+
|
|
125
|
+
<form on:submit|preventDefault={submit}>
|
|
126
|
+
<input use:register={'email'} />
|
|
127
|
+
{#if $errors.email}<span>{$errors.email}</span>{/if}
|
|
128
|
+
|
|
129
|
+
<button disabled={$pending}>Send</button>
|
|
130
|
+
</form>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Solid
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
import { useForm } from '@flight-framework/forms/solid';
|
|
137
|
+
import { Show } from 'solid-js';
|
|
138
|
+
|
|
139
|
+
function ContactPage() {
|
|
140
|
+
const { register, handleSubmit, errors, pending } = useForm(contactForm);
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<form onSubmit={handleSubmit}>
|
|
144
|
+
<input {...register('email')} />
|
|
145
|
+
<Show when={errors().email}>
|
|
146
|
+
<span>{errors().email}</span>
|
|
147
|
+
</Show>
|
|
148
|
+
|
|
149
|
+
<button disabled={pending()}>Send</button>
|
|
150
|
+
</form>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Server-Side Handling
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
// api/contact.ts
|
|
159
|
+
export async function POST(request: Request) {
|
|
160
|
+
return contactForm.handleSubmit(request);
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## API Reference
|
|
165
|
+
|
|
166
|
+
### createForm Options
|
|
167
|
+
|
|
168
|
+
| Option | Type | Description |
|
|
169
|
+
|--------|------|-------------|
|
|
170
|
+
| `validator` | ValidationAdapter | Validation schema |
|
|
171
|
+
| `action` | FormAction | Server action |
|
|
172
|
+
| `csrf` | boolean | Enable CSRF (default: true) |
|
|
173
|
+
| `progressive` | boolean | Works without JS |
|
|
174
|
+
|
|
175
|
+
### useForm Return
|
|
176
|
+
|
|
177
|
+
| Property | Type | Description |
|
|
178
|
+
|----------|------|-------------|
|
|
179
|
+
| `register(name)` | Function | Register a field |
|
|
180
|
+
| `handleSubmit` | Function | Submit handler |
|
|
181
|
+
| `values` | Object | Current values |
|
|
182
|
+
| `errors` | Object | Field errors |
|
|
183
|
+
| `pending` | boolean | Submitting state |
|
|
184
|
+
| `dirty` | boolean | Form modified |
|
|
185
|
+
|
|
186
|
+
## Creating Custom Validators
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import type { ValidationAdapter } from '@flight-framework/forms';
|
|
190
|
+
|
|
191
|
+
export function myValidator<T>(schema: MySchema<T>): ValidationAdapter<unknown, T> {
|
|
192
|
+
return {
|
|
193
|
+
name: 'my-validator',
|
|
194
|
+
_output: undefined as unknown as T,
|
|
195
|
+
|
|
196
|
+
validate(data) {
|
|
197
|
+
// Your validation logic
|
|
198
|
+
return { success: true, data: data as T };
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
async validateAsync(data) {
|
|
202
|
+
return this.validate(data);
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
|
|
210
|
+
MIT
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ValidationAdapter } from '../index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Valibot Validation Adapter for @flightdev/forms
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { createForm } from '@flightdev/forms';
|
|
9
|
+
* import { valibot } from '@flightdev/forms/valibot';
|
|
10
|
+
* import * as v from 'valibot';
|
|
11
|
+
*
|
|
12
|
+
* const schema = v.object({
|
|
13
|
+
* email: v.pipe(v.string(), v.email('Invalid email')),
|
|
14
|
+
* password: v.pipe(v.string(), v.minLength(8)),
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* const form = createForm({
|
|
18
|
+
* validator: valibot(schema),
|
|
19
|
+
* action: async (data) => { ... },
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** Valibot-like schema interface */
|
|
25
|
+
interface ValibotLikeSchema<T = unknown> {
|
|
26
|
+
_types?: {
|
|
27
|
+
output: T;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create a Valibot validation adapter
|
|
32
|
+
*
|
|
33
|
+
* @param schema - Valibot schema to validate against
|
|
34
|
+
* @returns ValidationAdapter instance
|
|
35
|
+
*/
|
|
36
|
+
declare function valibot<TOutput>(schema: ValibotLikeSchema<TOutput>): ValidationAdapter<unknown, TOutput>;
|
|
37
|
+
|
|
38
|
+
export { valibot as default, valibot };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { __require } from '../chunk-DGUM43GV.js';
|
|
2
|
+
|
|
3
|
+
// src/adapters/valibot.ts
|
|
4
|
+
var valibotModule = null;
|
|
5
|
+
async function getValibot() {
|
|
6
|
+
if (!valibotModule) {
|
|
7
|
+
try {
|
|
8
|
+
valibotModule = await import('valibot');
|
|
9
|
+
} catch {
|
|
10
|
+
throw new Error(
|
|
11
|
+
'@flightdev/forms: Valibot is not installed. Run: npm install valibot\nOr use a different validator:\n import { zod } from "@flightdev/forms/zod"'
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return valibotModule;
|
|
16
|
+
}
|
|
17
|
+
function valibot(schema) {
|
|
18
|
+
return {
|
|
19
|
+
name: "valibot",
|
|
20
|
+
_output: void 0,
|
|
21
|
+
validate(data) {
|
|
22
|
+
try {
|
|
23
|
+
const v = __require("valibot");
|
|
24
|
+
const result = v.safeParse(schema, data);
|
|
25
|
+
if (result.success) {
|
|
26
|
+
return {
|
|
27
|
+
success: true,
|
|
28
|
+
data: result.output
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
success: false,
|
|
33
|
+
errors: mapValibotErrors(result.issues ?? [])
|
|
34
|
+
};
|
|
35
|
+
} catch {
|
|
36
|
+
throw new Error("Valibot not available for synchronous validation. Use validateAsync instead.");
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
async validateAsync(data) {
|
|
40
|
+
const v = await getValibot();
|
|
41
|
+
const result = await v.safeParseAsync(schema, data);
|
|
42
|
+
if (result.success) {
|
|
43
|
+
return {
|
|
44
|
+
success: true,
|
|
45
|
+
data: result.output
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
success: false,
|
|
50
|
+
errors: mapValibotErrors(result.issues ?? [])
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function mapValibotErrors(issues) {
|
|
56
|
+
return issues.map((issue) => ({
|
|
57
|
+
path: issue.path?.map((p) => String(p.key)).join(".") ?? "",
|
|
58
|
+
message: issue.message,
|
|
59
|
+
code: issue.type
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
var valibot_default = valibot;
|
|
63
|
+
|
|
64
|
+
export { valibot_default as default, valibot };
|
|
65
|
+
//# sourceMappingURL=valibot.js.map
|
|
66
|
+
//# sourceMappingURL=valibot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/valibot.ts"],"names":[],"mappings":";;;AA8CA,IAAI,aAAA,GAAqB,IAAA;AAEzB,eAAe,UAAA,GAAa;AACxB,EAAA,IAAI,CAAC,aAAA,EAAe;AAChB,IAAA,IAAI;AACA,MAAA,aAAA,GAAgB,MAAM,OAAO,SAAS,CAAA;AAAA,IAC1C,CAAA,CAAA,MAAQ;AACJ,MAAA,MAAM,IAAI,KAAA;AAAA,QACN;AAAA,OAGJ;AAAA,IACJ;AAAA,EACJ;AACA,EAAA,OAAO,aAAA;AACX;AAYO,SAAS,QACZ,MAAA,EACmC;AACnC,EAAA,OAAO;AAAA,IACH,IAAA,EAAM,SAAA;AAAA,IACN,OAAA,EAAS,MAAA;AAAA,IAET,SAAS,IAAA,EAA0C;AAG/C,MAAA,IAAI;AACA,QAAA,MAAM,CAAA,GAAI,UAAQ,SAAS,CAAA;AAC3B,QAAA,MAAM,MAAA,GAAS,CAAA,CAAG,SAAA,CAAU,MAAA,EAAQ,IAAI,CAAA;AAExC,QAAA,IAAI,OAAO,OAAA,EAAS;AAChB,UAAA,OAAO;AAAA,YACH,OAAA,EAAS,IAAA;AAAA,YACT,MAAM,MAAA,CAAO;AAAA,WACjB;AAAA,QACJ;AAEA,QAAA,OAAO;AAAA,UACH,OAAA,EAAS,KAAA;AAAA,UACT,MAAA,EAAQ,gBAAA,CAAiB,MAAA,CAAO,MAAA,IAAU,EAAE;AAAA,SAChD;AAAA,MACJ,CAAA,CAAA,MAAQ;AACJ,QAAA,MAAM,IAAI,MAAM,8EAA8E,CAAA;AAAA,MAClG;AAAA,IACJ,CAAA;AAAA,IAEA,MAAM,cAAc,IAAA,EAAmD;AACnE,MAAA,MAAM,CAAA,GAAI,MAAM,UAAA,EAAW;AAC3B,MAAA,MAAM,MAAA,GAAS,MAAM,CAAA,CAAE,cAAA,CAAe,QAAQ,IAAI,CAAA;AAElD,MAAA,IAAI,OAAO,OAAA,EAAS;AAChB,QAAA,OAAO;AAAA,UACH,OAAA,EAAS,IAAA;AAAA,UACT,MAAM,MAAA,CAAO;AAAA,SACjB;AAAA,MACJ;AAEA,MAAA,OAAO;AAAA,QACH,OAAA,EAAS,KAAA;AAAA,QACT,MAAA,EAAQ,gBAAA,CAAiB,MAAA,CAAO,MAAA,IAAU,EAAE;AAAA,OAChD;AAAA,IACJ;AAAA,GACJ;AACJ;AAKA,SAAS,iBAAiB,MAAA,EAAsC;AAC5D,EAAA,OAAO,MAAA,CAAO,IAAI,CAAA,KAAA,MAAU;AAAA,IACxB,IAAA,EAAM,KAAA,CAAM,IAAA,EAAM,GAAA,CAAI,CAAA,CAAA,KAAK,MAAA,CAAO,CAAA,CAAE,GAAG,CAAC,CAAA,CAAE,IAAA,CAAK,GAAG,CAAA,IAAK,EAAA;AAAA,IACvD,SAAS,KAAA,CAAM,OAAA;AAAA,IACf,MAAM,KAAA,CAAM;AAAA,GAChB,CAAE,CAAA;AACN;AAEA,IAAO,eAAA,GAAQ","file":"valibot.js","sourcesContent":["/**\r\n * Valibot Validation Adapter for @flightdev/forms\r\n * \r\n * @example\r\n * ```typescript\r\n * import { createForm } from '@flightdev/forms';\r\n * import { valibot } from '@flightdev/forms/valibot';\r\n * import * as v from 'valibot';\r\n * \r\n * const schema = v.object({\r\n * email: v.pipe(v.string(), v.email('Invalid email')),\r\n * password: v.pipe(v.string(), v.minLength(8)),\r\n * });\r\n * \r\n * const form = createForm({\r\n * validator: valibot(schema),\r\n * action: async (data) => { ... },\r\n * });\r\n * ```\r\n */\r\n\r\nimport type { ValidationAdapter, ValidationResult, FieldError } from '../index.js';\r\n\r\n// ============================================================================\r\n// Types\r\n// ============================================================================\r\n\r\n/** Valibot-like schema interface */\r\ninterface ValibotLikeSchema<T = unknown> {\r\n _types?: { output: T };\r\n}\r\n\r\ninterface ValibotIssue {\r\n path?: Array<{ key: string | number }>;\r\n message: string;\r\n type?: string;\r\n}\r\n\r\ninterface ValibotResult<T> {\r\n success: boolean;\r\n output?: T;\r\n issues?: ValibotIssue[];\r\n}\r\n\r\n// Dynamic import helper - using any to avoid complex type incompatibilities\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nlet valibotModule: any = null;\r\n\r\nasync function getValibot() {\r\n if (!valibotModule) {\r\n try {\r\n valibotModule = await import('valibot');\r\n } catch {\r\n throw new Error(\r\n '@flightdev/forms: Valibot is not installed. Run: npm install valibot\\n' +\r\n 'Or use a different validator:\\n' +\r\n ' import { zod } from \"@flightdev/forms/zod\"'\r\n );\r\n }\r\n }\r\n return valibotModule;\r\n}\r\n\r\n// ============================================================================\r\n// Valibot Adapter\r\n// ============================================================================\r\n\r\n/**\r\n * Create a Valibot validation adapter\r\n * \r\n * @param schema - Valibot schema to validate against\r\n * @returns ValidationAdapter instance\r\n */\r\nexport function valibot<TOutput>(\r\n schema: ValibotLikeSchema<TOutput>\r\n): ValidationAdapter<unknown, TOutput> {\r\n return {\r\n name: 'valibot',\r\n _output: undefined as unknown as TOutput,\r\n\r\n validate(data: unknown): ValidationResult<TOutput> {\r\n // Synchronous validation - need to check if valibot is loaded\r\n // For sync, we'll use a cached version or throw\r\n try {\r\n const v = require('valibot') as typeof valibotModule;\r\n const result = v!.safeParse(schema, data);\r\n\r\n if (result.success) {\r\n return {\r\n success: true,\r\n data: result.output,\r\n };\r\n }\r\n\r\n return {\r\n success: false,\r\n errors: mapValibotErrors(result.issues ?? []),\r\n };\r\n } catch {\r\n throw new Error('Valibot not available for synchronous validation. Use validateAsync instead.');\r\n }\r\n },\r\n\r\n async validateAsync(data: unknown): Promise<ValidationResult<TOutput>> {\r\n const v = await getValibot();\r\n const result = await v.safeParseAsync(schema, data);\r\n\r\n if (result.success) {\r\n return {\r\n success: true,\r\n data: result.output,\r\n };\r\n }\r\n\r\n return {\r\n success: false,\r\n errors: mapValibotErrors(result.issues ?? []),\r\n };\r\n },\r\n };\r\n}\r\n\r\n/**\r\n * Map Valibot issues to FieldError array\r\n */\r\nfunction mapValibotErrors(issues: ValibotIssue[]): FieldError[] {\r\n return issues.map(issue => ({\r\n path: issue.path?.map(p => String(p.key)).join('.') ?? '',\r\n message: issue.message,\r\n code: issue.type,\r\n }));\r\n}\r\n\r\nexport default valibot;\r\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ValidationAdapter } from '../index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Yup Validation Adapter for @flightdev/forms
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { createForm } from '@flightdev/forms';
|
|
9
|
+
* import { yup } from '@flightdev/forms/yup';
|
|
10
|
+
* import * as y from 'yup';
|
|
11
|
+
*
|
|
12
|
+
* const schema = y.object({
|
|
13
|
+
* email: y.string().email('Invalid email').required(),
|
|
14
|
+
* password: y.string().min(8).required(),
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* const form = createForm({
|
|
18
|
+
* validator: yup(schema),
|
|
19
|
+
* action: async (data) => { ... },
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** Yup-like schema interface */
|
|
25
|
+
interface YupLikeSchema<T = unknown> {
|
|
26
|
+
validateSync(data: unknown, options?: {
|
|
27
|
+
abortEarly?: boolean;
|
|
28
|
+
}): T;
|
|
29
|
+
validate(data: unknown, options?: {
|
|
30
|
+
abortEarly?: boolean;
|
|
31
|
+
}): Promise<T>;
|
|
32
|
+
isValidSync(data: unknown): boolean;
|
|
33
|
+
isValid(data: unknown): Promise<boolean>;
|
|
34
|
+
__outputType?: T;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create a Yup validation adapter
|
|
38
|
+
*
|
|
39
|
+
* @param schema - Yup schema to validate against
|
|
40
|
+
* @returns ValidationAdapter instance
|
|
41
|
+
*/
|
|
42
|
+
declare function yup<TOutput>(schema: YupLikeSchema<TOutput>): ValidationAdapter<unknown, TOutput>;
|
|
43
|
+
|
|
44
|
+
export { yup as default, yup };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import '../chunk-DGUM43GV.js';
|
|
2
|
+
|
|
3
|
+
// src/adapters/yup.ts
|
|
4
|
+
function yup(schema) {
|
|
5
|
+
return {
|
|
6
|
+
name: "yup",
|
|
7
|
+
_output: void 0,
|
|
8
|
+
validate(data) {
|
|
9
|
+
try {
|
|
10
|
+
const validated = schema.validateSync(data, { abortEarly: false });
|
|
11
|
+
return {
|
|
12
|
+
success: true,
|
|
13
|
+
data: validated
|
|
14
|
+
};
|
|
15
|
+
} catch (error) {
|
|
16
|
+
return {
|
|
17
|
+
success: false,
|
|
18
|
+
errors: mapYupErrors(error)
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
async validateAsync(data) {
|
|
23
|
+
try {
|
|
24
|
+
const validated = await schema.validate(data, { abortEarly: false });
|
|
25
|
+
return {
|
|
26
|
+
success: true,
|
|
27
|
+
data: validated
|
|
28
|
+
};
|
|
29
|
+
} catch (error) {
|
|
30
|
+
return {
|
|
31
|
+
success: false,
|
|
32
|
+
errors: mapYupErrors(error)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function mapYupErrors(error) {
|
|
39
|
+
if (error.inner && error.inner.length > 0) {
|
|
40
|
+
return error.inner.map((inner) => ({
|
|
41
|
+
path: inner.path ?? "",
|
|
42
|
+
message: inner.message,
|
|
43
|
+
code: inner.type
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
return [{
|
|
47
|
+
path: error.path ?? "",
|
|
48
|
+
message: error.message,
|
|
49
|
+
code: error.type
|
|
50
|
+
}];
|
|
51
|
+
}
|
|
52
|
+
var yup_default = yup;
|
|
53
|
+
|
|
54
|
+
export { yup_default as default, yup };
|
|
55
|
+
//# sourceMappingURL=yup.js.map
|
|
56
|
+
//# sourceMappingURL=yup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/yup.ts"],"names":[],"mappings":";;;AAyDO,SAAS,IACZ,MAAA,EACmC;AACnC,EAAA,OAAO;AAAA,IACH,IAAA,EAAM,KAAA;AAAA,IACN,OAAA,EAAS,MAAA;AAAA,IAET,SAAS,IAAA,EAA0C;AAC/C,MAAA,IAAI;AACA,QAAA,MAAM,YAAY,MAAA,CAAO,YAAA,CAAa,MAAM,EAAE,UAAA,EAAY,OAAO,CAAA;AACjE,QAAA,OAAO;AAAA,UACH,OAAA,EAAS,IAAA;AAAA,UACT,IAAA,EAAM;AAAA,SACV;AAAA,MACJ,SAAS,KAAA,EAAO;AACZ,QAAA,OAAO;AAAA,UACH,OAAA,EAAS,KAAA;AAAA,UACT,MAAA,EAAQ,aAAa,KAA2B;AAAA,SACpD;AAAA,MACJ;AAAA,IACJ,CAAA;AAAA,IAEA,MAAM,cAAc,IAAA,EAAmD;AACnE,MAAA,IAAI;AACA,QAAA,MAAM,SAAA,GAAY,MAAM,MAAA,CAAO,QAAA,CAAS,MAAM,EAAE,UAAA,EAAY,OAAO,CAAA;AACnE,QAAA,OAAO;AAAA,UACH,OAAA,EAAS,IAAA;AAAA,UACT,IAAA,EAAM;AAAA,SACV;AAAA,MACJ,SAAS,KAAA,EAAO;AACZ,QAAA,OAAO;AAAA,UACH,OAAA,EAAS,KAAA;AAAA,UACT,MAAA,EAAQ,aAAa,KAA2B;AAAA,SACpD;AAAA,MACJ;AAAA,IACJ;AAAA,GACJ;AACJ;AAKA,SAAS,aAAa,KAAA,EAAyC;AAE3D,EAAA,IAAI,KAAA,CAAM,KAAA,IAAS,KAAA,CAAM,KAAA,CAAM,SAAS,CAAA,EAAG;AACvC,IAAA,OAAO,KAAA,CAAM,KAAA,CAAM,GAAA,CAAI,CAAA,KAAA,MAAU;AAAA,MAC7B,IAAA,EAAM,MAAM,IAAA,IAAQ,EAAA;AAAA,MACpB,SAAS,KAAA,CAAM,OAAA;AAAA,MACf,MAAM,KAAA,CAAM;AAAA,KAChB,CAAE,CAAA;AAAA,EACN;AAGA,EAAA,OAAO,CAAC;AAAA,IACJ,IAAA,EAAM,MAAM,IAAA,IAAQ,EAAA;AAAA,IACpB,SAAS,KAAA,CAAM,OAAA;AAAA,IACf,MAAM,KAAA,CAAM;AAAA,GACf,CAAA;AACL;AAEA,IAAO,WAAA,GAAQ","file":"yup.js","sourcesContent":["/**\r\n * Yup Validation Adapter for @flightdev/forms\r\n * \r\n * @example\r\n * ```typescript\r\n * import { createForm } from '@flightdev/forms';\r\n * import { yup } from '@flightdev/forms/yup';\r\n * import * as y from 'yup';\r\n * \r\n * const schema = y.object({\r\n * email: y.string().email('Invalid email').required(),\r\n * password: y.string().min(8).required(),\r\n * });\r\n * \r\n * const form = createForm({\r\n * validator: yup(schema),\r\n * action: async (data) => { ... },\r\n * });\r\n * ```\r\n */\r\n\r\nimport type { ValidationAdapter, ValidationResult, FieldError } from '../index.js';\r\n\r\n// ============================================================================\r\n// Types\r\n// ============================================================================\r\n\r\n/** Yup-like schema interface */\r\ninterface YupLikeSchema<T = unknown> {\r\n validateSync(data: unknown, options?: { abortEarly?: boolean }): T;\r\n validate(data: unknown, options?: { abortEarly?: boolean }): Promise<T>;\r\n isValidSync(data: unknown): boolean;\r\n isValid(data: unknown): Promise<boolean>;\r\n __outputType?: T;\r\n}\r\n\r\ninterface YupValidationError {\r\n inner: Array<{\r\n path: string;\r\n message: string;\r\n type?: string;\r\n }>;\r\n path?: string;\r\n message: string;\r\n type?: string;\r\n}\r\n\r\n// ============================================================================\r\n// Yup Adapter\r\n// ============================================================================\r\n\r\n/**\r\n * Create a Yup validation adapter\r\n * \r\n * @param schema - Yup schema to validate against\r\n * @returns ValidationAdapter instance\r\n */\r\nexport function yup<TOutput>(\r\n schema: YupLikeSchema<TOutput>\r\n): ValidationAdapter<unknown, TOutput> {\r\n return {\r\n name: 'yup',\r\n _output: undefined as unknown as TOutput,\r\n\r\n validate(data: unknown): ValidationResult<TOutput> {\r\n try {\r\n const validated = schema.validateSync(data, { abortEarly: false });\r\n return {\r\n success: true,\r\n data: validated,\r\n };\r\n } catch (error) {\r\n return {\r\n success: false,\r\n errors: mapYupErrors(error as YupValidationError),\r\n };\r\n }\r\n },\r\n\r\n async validateAsync(data: unknown): Promise<ValidationResult<TOutput>> {\r\n try {\r\n const validated = await schema.validate(data, { abortEarly: false });\r\n return {\r\n success: true,\r\n data: validated,\r\n };\r\n } catch (error) {\r\n return {\r\n success: false,\r\n errors: mapYupErrors(error as YupValidationError),\r\n };\r\n }\r\n },\r\n };\r\n}\r\n\r\n/**\r\n * Map Yup errors to FieldError array\r\n */\r\nfunction mapYupErrors(error: YupValidationError): FieldError[] {\r\n // If there are inner errors, use those\r\n if (error.inner && error.inner.length > 0) {\r\n return error.inner.map(inner => ({\r\n path: inner.path ?? '',\r\n message: inner.message,\r\n code: inner.type,\r\n }));\r\n }\r\n\r\n // Otherwise, single error\r\n return [{\r\n path: error.path ?? '',\r\n message: error.message,\r\n code: error.type,\r\n }];\r\n}\r\n\r\nexport default yup;\r\n"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ValidationAdapter } from '../index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Zod Validation Adapter for @flightdev/forms
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { createForm } from '@flightdev/forms';
|
|
9
|
+
* import { zod } from '@flightdev/forms/zod';
|
|
10
|
+
* import { z } from 'zod';
|
|
11
|
+
*
|
|
12
|
+
* const schema = z.object({
|
|
13
|
+
* email: z.string().email('Please enter a valid email'),
|
|
14
|
+
* password: z.string().min(8, 'Password must be at least 8 characters'),
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* const form = createForm({
|
|
18
|
+
* validator: zod(schema),
|
|
19
|
+
* action: async (data) => { ... },
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** Zod-like schema interface (to avoid direct Zod dependency) */
|
|
25
|
+
interface ZodLikeSchema<T = unknown> {
|
|
26
|
+
parse(data: unknown): T;
|
|
27
|
+
safeParse(data: unknown): {
|
|
28
|
+
success: true;
|
|
29
|
+
data: T;
|
|
30
|
+
} | {
|
|
31
|
+
success: false;
|
|
32
|
+
error: ZodLikeError;
|
|
33
|
+
};
|
|
34
|
+
safeParseAsync(data: unknown): Promise<{
|
|
35
|
+
success: true;
|
|
36
|
+
data: T;
|
|
37
|
+
} | {
|
|
38
|
+
success: false;
|
|
39
|
+
error: ZodLikeError;
|
|
40
|
+
}>;
|
|
41
|
+
parseAsync(data: unknown): Promise<T>;
|
|
42
|
+
_output?: T;
|
|
43
|
+
}
|
|
44
|
+
interface ZodLikeError {
|
|
45
|
+
issues: Array<{
|
|
46
|
+
path: (string | number)[];
|
|
47
|
+
message: string;
|
|
48
|
+
code: string;
|
|
49
|
+
}>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a Zod validation adapter
|
|
53
|
+
*
|
|
54
|
+
* @param schema - Zod schema to validate against
|
|
55
|
+
* @returns ValidationAdapter instance
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { zod } from '@flightdev/forms/zod';
|
|
60
|
+
* import { z } from 'zod';
|
|
61
|
+
*
|
|
62
|
+
* const validator = zod(z.object({
|
|
63
|
+
* name: z.string().min(2),
|
|
64
|
+
* email: z.string().email(),
|
|
65
|
+
* age: z.coerce.number().min(18),
|
|
66
|
+
* }));
|
|
67
|
+
*
|
|
68
|
+
* const result = validator.validate({ name: 'Jo', email: 'invalid', age: '15' });
|
|
69
|
+
* // result.success === false
|
|
70
|
+
* // result.errors === [
|
|
71
|
+
* // { path: 'name', message: 'String must contain at least 2 character(s)' },
|
|
72
|
+
* // { path: 'email', message: 'Invalid email' },
|
|
73
|
+
* // { path: 'age', message: 'Number must be greater than or equal to 18' },
|
|
74
|
+
* // ]
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare function zod<TOutput>(schema: ZodLikeSchema<TOutput>): ValidationAdapter<unknown, TOutput>;
|
|
78
|
+
/**
|
|
79
|
+
* Type helper to infer output type from Zod schema
|
|
80
|
+
*/
|
|
81
|
+
type InferZodOutput<T extends ZodLikeSchema> = T extends ZodLikeSchema<infer O> ? O : never;
|
|
82
|
+
|
|
83
|
+
export { type InferZodOutput, zod as default, zod };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import '../chunk-DGUM43GV.js';
|
|
2
|
+
|
|
3
|
+
// src/adapters/zod.ts
|
|
4
|
+
function zod(schema) {
|
|
5
|
+
return {
|
|
6
|
+
name: "zod",
|
|
7
|
+
_output: void 0,
|
|
8
|
+
validate(data) {
|
|
9
|
+
const result = schema.safeParse(data);
|
|
10
|
+
if (result.success) {
|
|
11
|
+
return {
|
|
12
|
+
success: true,
|
|
13
|
+
data: result.data
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
success: false,
|
|
18
|
+
errors: mapZodErrors(result.error)
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
async validateAsync(data) {
|
|
22
|
+
const result = await schema.safeParseAsync(data);
|
|
23
|
+
if (result.success) {
|
|
24
|
+
return {
|
|
25
|
+
success: true,
|
|
26
|
+
data: result.data
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
success: false,
|
|
31
|
+
errors: mapZodErrors(result.error)
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function mapZodErrors(error) {
|
|
37
|
+
return error.issues.map((issue) => ({
|
|
38
|
+
path: issue.path.join("."),
|
|
39
|
+
message: issue.message,
|
|
40
|
+
code: issue.code
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
var zod_default = zod;
|
|
44
|
+
|
|
45
|
+
export { zod_default as default, zod };
|
|
46
|
+
//# sourceMappingURL=zod.js.map
|
|
47
|
+
//# sourceMappingURL=zod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/zod.ts"],"names":[],"mappings":";;;AA0EO,SAAS,IACZ,MAAA,EACmC;AACnC,EAAA,OAAO;AAAA,IACH,IAAA,EAAM,KAAA;AAAA,IACN,OAAA,EAAS,MAAA;AAAA,IAET,SAAS,IAAA,EAA0C;AAC/C,MAAA,MAAM,MAAA,GAAS,MAAA,CAAO,SAAA,CAAU,IAAI,CAAA;AAEpC,MAAA,IAAI,OAAO,OAAA,EAAS;AAChB,QAAA,OAAO;AAAA,UACH,OAAA,EAAS,IAAA;AAAA,UACT,MAAM,MAAA,CAAO;AAAA,SACjB;AAAA,MACJ;AAEA,MAAA,OAAO;AAAA,QACH,OAAA,EAAS,KAAA;AAAA,QACT,MAAA,EAAQ,YAAA,CAAa,MAAA,CAAO,KAAK;AAAA,OACrC;AAAA,IACJ,CAAA;AAAA,IAEA,MAAM,cAAc,IAAA,EAAmD;AACnE,MAAA,MAAM,MAAA,GAAS,MAAM,MAAA,CAAO,cAAA,CAAe,IAAI,CAAA;AAE/C,MAAA,IAAI,OAAO,OAAA,EAAS;AAChB,QAAA,OAAO;AAAA,UACH,OAAA,EAAS,IAAA;AAAA,UACT,MAAM,MAAA,CAAO;AAAA,SACjB;AAAA,MACJ;AAEA,MAAA,OAAO;AAAA,QACH,OAAA,EAAS,KAAA;AAAA,QACT,MAAA,EAAQ,YAAA,CAAa,MAAA,CAAO,KAAK;AAAA,OACrC;AAAA,IACJ;AAAA,GACJ;AACJ;AAKA,SAAS,aAAa,KAAA,EAAmC;AACrD,EAAA,OAAO,KAAA,CAAM,MAAA,CAAO,GAAA,CAAI,CAAA,KAAA,MAAU;AAAA,IAC9B,IAAA,EAAM,KAAA,CAAM,IAAA,CAAK,IAAA,CAAK,GAAG,CAAA;AAAA,IACzB,SAAS,KAAA,CAAM,OAAA;AAAA,IACf,MAAM,KAAA,CAAM;AAAA,GAChB,CAAE,CAAA;AACN;AAOA,IAAO,WAAA,GAAQ","file":"zod.js","sourcesContent":["/**\r\n * Zod Validation Adapter for @flightdev/forms\r\n * \r\n * @example\r\n * ```typescript\r\n * import { createForm } from '@flightdev/forms';\r\n * import { zod } from '@flightdev/forms/zod';\r\n * import { z } from 'zod';\r\n * \r\n * const schema = z.object({\r\n * email: z.string().email('Please enter a valid email'),\r\n * password: z.string().min(8, 'Password must be at least 8 characters'),\r\n * });\r\n * \r\n * const form = createForm({\r\n * validator: zod(schema),\r\n * action: async (data) => { ... },\r\n * });\r\n * ```\r\n */\r\n\r\nimport type { ValidationAdapter, ValidationResult, FieldError } from '../index.js';\r\n\r\n// ============================================================================\r\n// Types\r\n// ============================================================================\r\n\r\n/** Zod-like schema interface (to avoid direct Zod dependency) */\r\ninterface ZodLikeSchema<T = unknown> {\r\n parse(data: unknown): T;\r\n safeParse(data: unknown): { success: true; data: T } | { success: false; error: ZodLikeError };\r\n safeParseAsync(data: unknown): Promise<{ success: true; data: T } | { success: false; error: ZodLikeError }>;\r\n parseAsync(data: unknown): Promise<T>;\r\n _output?: T;\r\n}\r\n\r\ninterface ZodLikeError {\r\n issues: Array<{\r\n path: (string | number)[];\r\n message: string;\r\n code: string;\r\n }>;\r\n}\r\n\r\n// ============================================================================\r\n// Zod Adapter\r\n// ============================================================================\r\n\r\n/**\r\n * Create a Zod validation adapter\r\n * \r\n * @param schema - Zod schema to validate against\r\n * @returns ValidationAdapter instance\r\n * \r\n * @example\r\n * ```typescript\r\n * import { zod } from '@flightdev/forms/zod';\r\n * import { z } from 'zod';\r\n * \r\n * const validator = zod(z.object({\r\n * name: z.string().min(2),\r\n * email: z.string().email(),\r\n * age: z.coerce.number().min(18),\r\n * }));\r\n * \r\n * const result = validator.validate({ name: 'Jo', email: 'invalid', age: '15' });\r\n * // result.success === false\r\n * // result.errors === [\r\n * // { path: 'name', message: 'String must contain at least 2 character(s)' },\r\n * // { path: 'email', message: 'Invalid email' },\r\n * // { path: 'age', message: 'Number must be greater than or equal to 18' },\r\n * // ]\r\n * ```\r\n */\r\nexport function zod<TOutput>(\r\n schema: ZodLikeSchema<TOutput>\r\n): ValidationAdapter<unknown, TOutput> {\r\n return {\r\n name: 'zod',\r\n _output: undefined as unknown as TOutput,\r\n\r\n validate(data: unknown): ValidationResult<TOutput> {\r\n const result = schema.safeParse(data);\r\n\r\n if (result.success) {\r\n return {\r\n success: true,\r\n data: result.data,\r\n };\r\n }\r\n\r\n return {\r\n success: false,\r\n errors: mapZodErrors(result.error),\r\n };\r\n },\r\n\r\n async validateAsync(data: unknown): Promise<ValidationResult<TOutput>> {\r\n const result = await schema.safeParseAsync(data);\r\n\r\n if (result.success) {\r\n return {\r\n success: true,\r\n data: result.data,\r\n };\r\n }\r\n\r\n return {\r\n success: false,\r\n errors: mapZodErrors(result.error),\r\n };\r\n },\r\n };\r\n}\r\n\r\n/**\r\n * Map Zod errors to FieldError array\r\n */\r\nfunction mapZodErrors(error: ZodLikeError): FieldError[] {\r\n return error.issues.map(issue => ({\r\n path: issue.path.join('.'),\r\n message: issue.message,\r\n code: issue.code,\r\n }));\r\n}\r\n\r\n/**\r\n * Type helper to infer output type from Zod schema\r\n */\r\nexport type InferZodOutput<T extends ZodLikeSchema> = T extends ZodLikeSchema<infer O> ? O : never;\r\n\r\nexport default zod;\r\n"]}
|