@buildnbuzz/buzzform 0.1.0 → 0.1.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 -21
- package/README.md +146 -138
- package/dist/{adapter-BT9v2OVg.d.mts → adapter-nQW28cyO.d.mts} +8 -2
- package/dist/{adapter-BT9v2OVg.d.ts → adapter-nQW28cyO.d.ts} +8 -2
- package/dist/chunk-63LF7K4O.mjs +171 -0
- package/dist/chunk-63LF7K4O.mjs.map +1 -0
- package/dist/chunk-HWDQN57Q.mjs +423 -0
- package/dist/chunk-HWDQN57Q.mjs.map +1 -0
- package/dist/{chunk-DDDGBPVU.mjs → chunk-IMJ5FRK5.mjs} +1 -1
- package/dist/chunk-IMJ5FRK5.mjs.map +1 -0
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +95 -176
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -12
- package/dist/index.mjs.map +1 -1
- package/dist/rhf.d.mts +1 -1
- package/dist/rhf.d.ts +1 -1
- package/dist/rhf.js.map +1 -1
- package/dist/rhf.mjs +1 -1
- package/dist/rhf.mjs.map +1 -1
- package/dist/schema.d.mts +5 -32
- package/dist/schema.d.ts +5 -32
- package/dist/schema.js +160 -171
- package/dist/schema.js.map +1 -1
- package/dist/schema.mjs +18 -12
- package/dist/{utils-BgwyUFGB.d.mts → utils-CS2VrTJU.d.ts} +64 -51
- package/dist/{utils-DVLpbOoW.d.ts → utils-Dzp_68i3.d.mts} +64 -51
- package/dist/zod.d.mts +6 -24
- package/dist/zod.d.ts +6 -24
- package/dist/zod.js +151 -17
- package/dist/zod.js.map +1 -1
- package/dist/zod.mjs +69 -16
- package/dist/zod.mjs.map +1 -1
- package/package.json +4 -3
- package/dist/chunk-DDDGBPVU.mjs.map +0 -1
- package/dist/chunk-K42S5YX3.mjs +0 -599
- package/dist/chunk-K42S5YX3.mjs.map +0 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Parth Lad / BuildnBuzz
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Parth Lad / BuildnBuzz
|
|
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
CHANGED
|
@@ -1,138 +1,146 @@
|
|
|
1
|
-
# BuzzForm
|
|
2
|
-
|
|
3
|
-
[](https://opensource.org/licenses/MIT)
|
|
4
|
-
|
|
5
|
-
A schema-driven form library for React + shadcn/ui. Declare fields once, get validated forms with minimal boilerplate.
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
- 🎯 **Schema-Driven** – Define fields as data, render forms automatically
|
|
10
|
-
- 🧩 **17+ Field Types** – Text, password, select, date, upload, arrays, tabs, and more
|
|
11
|
-
- ⚡ **Auto Validation** – Generates Zod schemas from your field definitions
|
|
12
|
-
- 🎨 **shadcn/ui Native** – Beautiful, accessible components out of the box
|
|
13
|
-
- 🔌 **Adapter Pattern** – Built for React Hook Form, extensible to others
|
|
14
|
-
- 📦 **Registry Ready** – Install components individually via shadcn CLI
|
|
15
|
-
|
|
16
|
-
## Installation
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
# Install
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
|
65
|
-
|
|
|
66
|
-
| `
|
|
67
|
-
| `
|
|
68
|
-
| `
|
|
69
|
-
| `
|
|
70
|
-
| `
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
|
75
|
-
|
|
|
76
|
-
| `
|
|
77
|
-
| `
|
|
78
|
-
| `
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
]
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
1
|
+
# BuzzForm
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
5
|
+
A schema-driven form library for React + shadcn/ui. Declare fields once, get validated forms with minimal boilerplate.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- 🎯 **Schema-Driven** – Define fields as data, render forms automatically
|
|
10
|
+
- 🧩 **17+ Field Types** – Text, password, select, date, upload, arrays, tabs, and more
|
|
11
|
+
- ⚡ **Auto Validation** – Generates Zod schemas from your field definitions
|
|
12
|
+
- 🎨 **shadcn/ui Native** – Beautiful, accessible components out of the box
|
|
13
|
+
- 🔌 **Adapter Pattern** – Built for React Hook Form, extensible to others
|
|
14
|
+
- 📦 **Registry Ready** – Install components individually via shadcn CLI
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# Recommended: Install everything with one command
|
|
20
|
+
npx shadcn@latest add https://form.buildnbuzz.com/r/starter
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
This installs `@buildnbuzz/buzzform`, required peer dependencies (`react-hook-form`, `zod`), and all UI components.
|
|
24
|
+
|
|
25
|
+
<details>
|
|
26
|
+
<summary>Manual installation</summary>
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @buildnbuzz/buzzform react-hook-form zod
|
|
30
|
+
npx shadcn@latest add https://form.buildnbuzz.com/r/starter
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
</details>
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
"use client";
|
|
39
|
+
|
|
40
|
+
import { createSchema, type InferSchema } from "@buildnbuzz/buzzform";
|
|
41
|
+
import { Form } from "@/components/buzzform/form";
|
|
42
|
+
|
|
43
|
+
const schema = createSchema([
|
|
44
|
+
{ type: "text", name: "name", label: "Name", required: true },
|
|
45
|
+
{ type: "email", name: "email", label: "Email", required: true },
|
|
46
|
+
{ type: "password", name: "password", label: "Password", minLength: 8 },
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
type FormData = InferSchema<typeof schema>;
|
|
50
|
+
|
|
51
|
+
export function LoginForm() {
|
|
52
|
+
const handleSubmit = async (data: FormData) => {
|
|
53
|
+
console.log(data);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return <Form schema={schema} onSubmit={handleSubmit} submitLabel="Sign In" />;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Field Types
|
|
61
|
+
|
|
62
|
+
### Data Fields
|
|
63
|
+
|
|
64
|
+
| Type | Description |
|
|
65
|
+
| ---------- | -------------------------------------------------------- |
|
|
66
|
+
| `text` | Single-line text input |
|
|
67
|
+
| `email` | Email input with validation |
|
|
68
|
+
| `password` | Password with strength indicator, requirements, generate |
|
|
69
|
+
| `textarea` | Multi-line text with auto-resize |
|
|
70
|
+
| `number` | Numeric input with steppers, formatting |
|
|
71
|
+
| `date` | Date picker with presets |
|
|
72
|
+
| `datetime` | Date + time picker |
|
|
73
|
+
| `select` | Dropdown with search, multi-select, async options |
|
|
74
|
+
| `checkbox` | Boolean checkbox |
|
|
75
|
+
| `switch` | Toggle switch |
|
|
76
|
+
| `radio` | Radio button group with card variant |
|
|
77
|
+
| `tags` | Tag/chip input |
|
|
78
|
+
| `upload` | File upload with drag-drop, previews |
|
|
79
|
+
|
|
80
|
+
### Layout Fields
|
|
81
|
+
|
|
82
|
+
| Type | Description |
|
|
83
|
+
| ------------- | ---------------------------------------- |
|
|
84
|
+
| `row` | Horizontal field layout |
|
|
85
|
+
| `group` | Named object container |
|
|
86
|
+
| `collapsible` | Expandable section |
|
|
87
|
+
| `tabs` | Tabbed interface |
|
|
88
|
+
| `array` | Repeatable fields with drag-drop sorting |
|
|
89
|
+
|
|
90
|
+
## Advanced Usage
|
|
91
|
+
|
|
92
|
+
### Conditional Fields
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
const fields: Field[] = [
|
|
96
|
+
{ type: "checkbox", name: "hasCompany", label: "I represent a company" },
|
|
97
|
+
{
|
|
98
|
+
type: "text",
|
|
99
|
+
name: "companyName",
|
|
100
|
+
label: "Company Name",
|
|
101
|
+
condition: (data) => data.hasCompany === true,
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Dynamic Options
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
const fields: Field[] = [
|
|
110
|
+
{ type: "select", name: "country", label: "Country", options: countries },
|
|
111
|
+
{
|
|
112
|
+
type: "select",
|
|
113
|
+
name: "city",
|
|
114
|
+
label: "City",
|
|
115
|
+
dependencies: ["country"],
|
|
116
|
+
options: async ({ data }) => {
|
|
117
|
+
const cities = await fetchCities(data.country);
|
|
118
|
+
return cities;
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
];
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Custom Validation
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
const fields: Field[] = [
|
|
128
|
+
{
|
|
129
|
+
type: "text",
|
|
130
|
+
name: "username",
|
|
131
|
+
label: "Username",
|
|
132
|
+
validate: async (value, { data }) => {
|
|
133
|
+
const available = await checkUsername(value);
|
|
134
|
+
return available || "Username is taken";
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
];
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Documentation
|
|
141
|
+
|
|
142
|
+
Full documentation and examples: **[form.buildnbuzz.com](https://form.buildnbuzz.com)**
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
MIT © [Parth Lad / BuildnBuzz](https://buildnbuzz.com)
|
|
@@ -2,12 +2,18 @@ import { ReactNode, ComponentType, FormEvent } from 'react';
|
|
|
2
2
|
import { ZodSchema } from 'zod';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Context passed to validation functions.
|
|
5
|
+
* Context passed to custom validation functions.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* validate: (value, { data }) => {
|
|
9
|
+
* if (value !== data.password) return "Passwords do not match";
|
|
10
|
+
* return true;
|
|
11
|
+
* }
|
|
6
12
|
*/
|
|
7
13
|
interface ValidationContext<TData = Record<string, unknown>> {
|
|
8
14
|
/** Complete form data */
|
|
9
15
|
data: TData;
|
|
10
|
-
/**
|
|
16
|
+
/** Parent object containing this field and its siblings */
|
|
11
17
|
siblingData: Record<string, unknown>;
|
|
12
18
|
/** Path segments to this field */
|
|
13
19
|
path: string[];
|
|
@@ -2,12 +2,18 @@ import { ReactNode, ComponentType, FormEvent } from 'react';
|
|
|
2
2
|
import { ZodSchema } from 'zod';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Context passed to validation functions.
|
|
5
|
+
* Context passed to custom validation functions.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* validate: (value, { data }) => {
|
|
9
|
+
* if (value !== data.password) return "Passwords do not match";
|
|
10
|
+
* return true;
|
|
11
|
+
* }
|
|
6
12
|
*/
|
|
7
13
|
interface ValidationContext<TData = Record<string, unknown>> {
|
|
8
14
|
/** Complete form data */
|
|
9
15
|
data: TData;
|
|
10
|
-
/**
|
|
16
|
+
/** Parent object containing this field and its siblings */
|
|
11
17
|
siblingData: Record<string, unknown>;
|
|
12
18
|
/** Path segments to this field */
|
|
13
19
|
path: string[];
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// src/schema/helpers.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
function extractValidationConfig(validate) {
|
|
4
|
+
if (!validate) {
|
|
5
|
+
return { fn: void 0, isLive: false };
|
|
6
|
+
}
|
|
7
|
+
if (typeof validate === "function") {
|
|
8
|
+
return { fn: validate, isLive: false };
|
|
9
|
+
}
|
|
10
|
+
if (typeof validate === "object" && "fn" in validate) {
|
|
11
|
+
const obj = validate;
|
|
12
|
+
const fn = typeof obj.fn === "function" ? obj.fn : void 0;
|
|
13
|
+
if (!obj.live) {
|
|
14
|
+
return { fn, isLive: false };
|
|
15
|
+
}
|
|
16
|
+
const debounceMs = typeof obj.live === "object" ? obj.live.debounceMs : void 0;
|
|
17
|
+
return { fn, isLive: true, debounceMs };
|
|
18
|
+
}
|
|
19
|
+
return { fn: void 0, isLive: false };
|
|
20
|
+
}
|
|
21
|
+
function collectFieldValidators(fields, basePath = "") {
|
|
22
|
+
const validators = [];
|
|
23
|
+
for (const field of fields) {
|
|
24
|
+
if ("name" in field && field.name) {
|
|
25
|
+
const fieldPath = basePath ? `${basePath}.${field.name}` : field.name;
|
|
26
|
+
if ("validate" in field && field.validate) {
|
|
27
|
+
const config = extractValidationConfig(field.validate);
|
|
28
|
+
if (config.fn) {
|
|
29
|
+
validators.push({
|
|
30
|
+
path: fieldPath,
|
|
31
|
+
fn: config.fn
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (field.type === "group" && "fields" in field) {
|
|
36
|
+
validators.push(...collectFieldValidators(field.fields, fieldPath));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (field.type === "row" && "fields" in field) {
|
|
40
|
+
validators.push(...collectFieldValidators(field.fields, basePath));
|
|
41
|
+
}
|
|
42
|
+
if (field.type === "collapsible" && "fields" in field) {
|
|
43
|
+
validators.push(...collectFieldValidators(field.fields, basePath));
|
|
44
|
+
}
|
|
45
|
+
if (field.type === "tabs" && "tabs" in field) {
|
|
46
|
+
for (const tab of field.tabs) {
|
|
47
|
+
const tabPath = tab.name ? basePath ? `${basePath}.${tab.name}` : tab.name : basePath;
|
|
48
|
+
validators.push(...collectFieldValidators(tab.fields, tabPath));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return validators;
|
|
53
|
+
}
|
|
54
|
+
function getSiblingData(data, path) {
|
|
55
|
+
const parts = path.split(".");
|
|
56
|
+
if (parts.length <= 1) {
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
const parentParts = parts.slice(0, -1);
|
|
60
|
+
let current = data;
|
|
61
|
+
for (const part of parentParts) {
|
|
62
|
+
if (current && typeof current === "object" && current !== null) {
|
|
63
|
+
current = current[part];
|
|
64
|
+
} else {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (current && typeof current === "object" && current !== null) {
|
|
69
|
+
return current;
|
|
70
|
+
}
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
function getValueByPath(data, path) {
|
|
74
|
+
const parts = path.split(".");
|
|
75
|
+
let current = data;
|
|
76
|
+
for (const part of parts) {
|
|
77
|
+
if (current && typeof current === "object" && current !== null) {
|
|
78
|
+
current = current[part];
|
|
79
|
+
} else {
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return current;
|
|
84
|
+
}
|
|
85
|
+
function makeOptional(schema, fieldType) {
|
|
86
|
+
switch (fieldType) {
|
|
87
|
+
case "text":
|
|
88
|
+
case "textarea":
|
|
89
|
+
case "email":
|
|
90
|
+
case "password":
|
|
91
|
+
return schema.optional().or(z.literal(""));
|
|
92
|
+
case "number":
|
|
93
|
+
case "date":
|
|
94
|
+
case "select":
|
|
95
|
+
case "radio":
|
|
96
|
+
return schema.optional().nullable();
|
|
97
|
+
case "checkbox":
|
|
98
|
+
case "switch":
|
|
99
|
+
return schema;
|
|
100
|
+
case "tags":
|
|
101
|
+
case "array":
|
|
102
|
+
return schema.optional().default([]);
|
|
103
|
+
case "upload":
|
|
104
|
+
return schema.optional().nullable();
|
|
105
|
+
default:
|
|
106
|
+
return schema.optional();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function coerceToNumber(val) {
|
|
110
|
+
if (val === "" || val === null || val === void 0) {
|
|
111
|
+
return void 0;
|
|
112
|
+
}
|
|
113
|
+
const num = Number(val);
|
|
114
|
+
return isNaN(num) ? void 0 : num;
|
|
115
|
+
}
|
|
116
|
+
function coerceToDate(val) {
|
|
117
|
+
if (val === "" || val === null || val === void 0) {
|
|
118
|
+
return void 0;
|
|
119
|
+
}
|
|
120
|
+
if (val instanceof Date) {
|
|
121
|
+
return isNaN(val.getTime()) ? void 0 : val;
|
|
122
|
+
}
|
|
123
|
+
if (typeof val === "string" || typeof val === "number") {
|
|
124
|
+
const d = new Date(val);
|
|
125
|
+
return isNaN(d.getTime()) ? void 0 : d;
|
|
126
|
+
}
|
|
127
|
+
return void 0;
|
|
128
|
+
}
|
|
129
|
+
var PATTERN_MESSAGES = {
|
|
130
|
+
"^[a-zA-Z0-9_]+$": "Only letters, numbers, and underscores allowed",
|
|
131
|
+
"^[a-z0-9-]+$": "Only lowercase letters, numbers, and hyphens allowed",
|
|
132
|
+
"^\\S+@\\S+\\.\\S+$": "Invalid email format",
|
|
133
|
+
"^https?://": "Must start with http:// or https://"
|
|
134
|
+
};
|
|
135
|
+
function getPatternErrorMessage(pattern) {
|
|
136
|
+
const patternStr = typeof pattern === "string" ? pattern : pattern.source;
|
|
137
|
+
return PATTERN_MESSAGES[patternStr] || `Must match pattern: ${patternStr}`;
|
|
138
|
+
}
|
|
139
|
+
function isFileLike(value) {
|
|
140
|
+
return typeof value === "object" && value !== null && "name" in value && "size" in value && "type" in value;
|
|
141
|
+
}
|
|
142
|
+
function isFileTypeAccepted(file, accept) {
|
|
143
|
+
if (accept === "*" || !accept) return true;
|
|
144
|
+
const acceptTypes = accept.split(",").map((t) => t.trim().toLowerCase());
|
|
145
|
+
const fileType = file.type.toLowerCase();
|
|
146
|
+
const fileName = file.name.toLowerCase();
|
|
147
|
+
return acceptTypes.some((acceptType) => {
|
|
148
|
+
if (acceptType.endsWith("/*")) {
|
|
149
|
+
const category = acceptType.replace("/*", "");
|
|
150
|
+
return fileType.startsWith(category + "/");
|
|
151
|
+
}
|
|
152
|
+
if (acceptType.startsWith(".")) {
|
|
153
|
+
return fileName.endsWith(acceptType);
|
|
154
|
+
}
|
|
155
|
+
return fileType === acceptType;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export {
|
|
160
|
+
extractValidationConfig,
|
|
161
|
+
collectFieldValidators,
|
|
162
|
+
getSiblingData,
|
|
163
|
+
getValueByPath,
|
|
164
|
+
makeOptional,
|
|
165
|
+
coerceToNumber,
|
|
166
|
+
coerceToDate,
|
|
167
|
+
getPatternErrorMessage,
|
|
168
|
+
isFileLike,
|
|
169
|
+
isFileTypeAccepted
|
|
170
|
+
};
|
|
171
|
+
//# sourceMappingURL=chunk-63LF7K4O.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schema/helpers.ts"],"sourcesContent":["import { z } from 'zod';\nimport type { Field, FieldType, ValidationContext, ValidationFn } from '../types';\n\n// =============================================================================\n// VALIDATION CONFIG EXTRACTION\n// =============================================================================\n\ntype ExtractableValidationFn = (\n value: unknown,\n context: ValidationContext\n) => true | string | Promise<true | string>;\n\nexport interface ExtractedValidationConfig {\n fn?: ExtractableValidationFn;\n isLive: boolean;\n debounceMs?: number;\n}\n\nexport function extractValidationConfig(\n validate?: unknown\n): ExtractedValidationConfig {\n if (!validate) {\n return { fn: undefined, isLive: false };\n }\n\n if (typeof validate === 'function') {\n return { fn: validate as ExtractableValidationFn, isLive: false };\n }\n\n if (typeof validate === 'object' && 'fn' in validate) {\n const obj = validate as { fn?: unknown; live?: boolean | { debounceMs?: number } };\n const fn = typeof obj.fn === 'function' ? obj.fn as ExtractableValidationFn : undefined;\n\n if (!obj.live) {\n return { fn, isLive: false };\n }\n\n const debounceMs = typeof obj.live === 'object' ? obj.live.debounceMs : undefined;\n return { fn, isLive: true, debounceMs };\n }\n\n return { fn: undefined, isLive: false };\n}\n\n// =============================================================================\n// FIELD VALIDATOR COLLECTION\n// =============================================================================\n\nexport interface FieldValidator {\n path: string;\n fn: ValidationFn;\n}\n\n/**\n * Recursively collects all field validators from a field array.\n */\nexport function collectFieldValidators(\n fields: readonly Field[],\n basePath: string = ''\n): FieldValidator[] {\n const validators: FieldValidator[] = [];\n\n for (const field of fields) {\n if ('name' in field && field.name) {\n const fieldPath = basePath ? `${basePath}.${field.name}` : field.name;\n\n if ('validate' in field && field.validate) {\n const config = extractValidationConfig(field.validate);\n if (config.fn) {\n validators.push({\n path: fieldPath,\n fn: config.fn as ValidationFn,\n });\n }\n }\n\n if (field.type === 'group' && 'fields' in field) {\n validators.push(...collectFieldValidators(field.fields, fieldPath));\n }\n }\n\n // Layout fields pass through without adding to path\n if (field.type === 'row' && 'fields' in field) {\n validators.push(...collectFieldValidators(field.fields, basePath));\n }\n if (field.type === 'collapsible' && 'fields' in field) {\n validators.push(...collectFieldValidators(field.fields, basePath));\n }\n if (field.type === 'tabs' && 'tabs' in field) {\n for (const tab of field.tabs) {\n const tabPath = tab.name\n ? (basePath ? `${basePath}.${tab.name}` : tab.name)\n : basePath;\n validators.push(...collectFieldValidators(tab.fields, tabPath));\n }\n }\n }\n\n return validators;\n}\n\n// =============================================================================\n// SIBLING DATA EXTRACTION\n// =============================================================================\n\n/**\n * Gets the parent object containing the field at the given path.\n */\nexport function getSiblingData(\n data: Record<string, unknown>,\n path: string\n): Record<string, unknown> {\n const parts = path.split('.');\n\n if (parts.length <= 1) {\n return data;\n }\n\n const parentParts = parts.slice(0, -1);\n let current: unknown = data;\n\n for (const part of parentParts) {\n if (current && typeof current === 'object' && current !== null) {\n current = (current as Record<string, unknown>)[part];\n } else {\n return {};\n }\n }\n\n if (current && typeof current === 'object' && current !== null) {\n return current as Record<string, unknown>;\n }\n\n return {};\n}\n\n/**\n * Gets a value at a dot-notation path.\n */\nexport function getValueByPath(\n data: Record<string, unknown>,\n path: string\n): unknown {\n const parts = path.split('.');\n let current: unknown = data;\n\n for (const part of parts) {\n if (current && typeof current === 'object' && current !== null) {\n current = (current as Record<string, unknown>)[part];\n } else {\n return undefined;\n }\n }\n\n return current;\n}\n\n/**\n * Creates a superRefine that runs all field validators with full form context.\n */\nexport function createRootValidationRefinement(\n validators: FieldValidator[]\n): (data: Record<string, unknown>, ctx: z.RefinementCtx) => Promise<void> {\n return async (data, ctx) => {\n const validationPromises = validators.map(async ({ path, fn }) => {\n const value = getValueByPath(data, path);\n const siblingData = getSiblingData(data, path);\n\n try {\n const result = await fn(value, {\n data,\n siblingData,\n path: path.split('.'),\n });\n\n if (result !== true) {\n ctx.addIssue({\n code: 'custom',\n path: path.split('.'),\n message: typeof result === 'string' ? result : 'Validation failed',\n });\n }\n } catch (error) {\n ctx.addIssue({\n code: 'custom',\n path: path.split('.'),\n message: error instanceof Error ? error.message : 'Validation error',\n });\n }\n });\n\n await Promise.all(validationPromises);\n };\n}\n\n// =============================================================================\n// OPTIONAL HANDLING\n// =============================================================================\n\nexport function makeOptional(\n schema: z.ZodTypeAny,\n fieldType: FieldType\n): z.ZodTypeAny {\n switch (fieldType) {\n case 'text':\n case 'textarea':\n case 'email':\n case 'password':\n return schema.optional().or(z.literal(''));\n\n case 'number':\n case 'date':\n case 'select':\n case 'radio':\n return schema.optional().nullable();\n\n case 'checkbox':\n case 'switch':\n return schema;\n\n case 'tags':\n case 'array':\n return schema.optional().default([]);\n\n case 'upload':\n return schema.optional().nullable();\n\n default:\n return schema.optional();\n }\n}\n\n// =============================================================================\n// COERCION HELPERS\n// =============================================================================\n\nexport function coerceToNumber(val: unknown): number | undefined {\n if (val === '' || val === null || val === undefined) {\n return undefined;\n }\n const num = Number(val);\n return isNaN(num) ? undefined : num;\n}\n\nexport function coerceToDate(val: unknown): Date | undefined {\n if (val === '' || val === null || val === undefined) {\n return undefined;\n }\n if (val instanceof Date) {\n return isNaN(val.getTime()) ? undefined : val;\n }\n if (typeof val === 'string' || typeof val === 'number') {\n const d = new Date(val);\n return isNaN(d.getTime()) ? undefined : d;\n }\n return undefined;\n}\n\n// =============================================================================\n// PATTERN VALIDATION\n// =============================================================================\n\nconst PATTERN_MESSAGES: Record<string, string> = {\n '^[a-zA-Z0-9_]+$': 'Only letters, numbers, and underscores allowed',\n '^[a-z0-9-]+$': 'Only lowercase letters, numbers, and hyphens allowed',\n '^\\\\S+@\\\\S+\\\\.\\\\S+$': 'Invalid email format',\n '^https?://': 'Must start with http:// or https://',\n};\n\nexport function getPatternErrorMessage(pattern: string | RegExp): string {\n const patternStr = typeof pattern === 'string' ? pattern : pattern.source;\n return PATTERN_MESSAGES[patternStr] || `Must match pattern: ${patternStr}`;\n}\n\n// =============================================================================\n// FILE VALIDATION HELPERS\n// =============================================================================\n\nexport function isFileLike(value: unknown): value is File {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'name' in value &&\n 'size' in value &&\n 'type' in value\n );\n}\n\nexport function isFileTypeAccepted(\n file: File,\n accept: string\n): boolean {\n if (accept === '*' || !accept) return true;\n\n const acceptTypes = accept.split(',').map(t => t.trim().toLowerCase());\n const fileType = file.type.toLowerCase();\n const fileName = file.name.toLowerCase();\n\n return acceptTypes.some(acceptType => {\n if (acceptType.endsWith('/*')) {\n const category = acceptType.replace('/*', '');\n return fileType.startsWith(category + '/');\n }\n if (acceptType.startsWith('.')) {\n return fileName.endsWith(acceptType);\n }\n return fileType === acceptType;\n });\n}\n"],"mappings":";AAAA,SAAS,SAAS;AAkBX,SAAS,wBACZ,UACyB;AACzB,MAAI,CAAC,UAAU;AACX,WAAO,EAAE,IAAI,QAAW,QAAQ,MAAM;AAAA,EAC1C;AAEA,MAAI,OAAO,aAAa,YAAY;AAChC,WAAO,EAAE,IAAI,UAAqC,QAAQ,MAAM;AAAA,EACpE;AAEA,MAAI,OAAO,aAAa,YAAY,QAAQ,UAAU;AAClD,UAAM,MAAM;AACZ,UAAM,KAAK,OAAO,IAAI,OAAO,aAAa,IAAI,KAAgC;AAE9E,QAAI,CAAC,IAAI,MAAM;AACX,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