zustic 1.1.1 → 1.1.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.
@@ -1,16 +1,68 @@
1
- import * as react from 'react';
1
+ import React$1 from 'react';
2
2
 
3
+ /**
4
+ * Required field validation rule.
5
+ * Can be a simple boolean or an object with custom error message.
6
+ *
7
+ * @example
8
+ * // Simple required
9
+ * required: true
10
+ *
11
+ * // With custom message
12
+ * required: { value: true, message: "Email is required" }
13
+ */
3
14
  type RequiredRule = boolean | {
4
15
  value: boolean;
5
16
  message: string;
6
17
  };
18
+ /**
19
+ * Numeric validation rule for min/max constraints.
20
+ * Can be a plain number or an object with custom error message.
21
+ *
22
+ * @example
23
+ * // Simple min value
24
+ * min: 18
25
+ *
26
+ * // With custom message
27
+ * min: { value: 18, message: "Must be 18 or older" }
28
+ */
7
29
  type NumberRule = number | {
8
30
  value: number;
9
31
  message: string;
10
32
  };
33
+ /**
34
+ * Individual form field state and configuration.
35
+ * Tracks value, error, validation rules, and user interaction state.
36
+ *
37
+ * @template T - The type of the field value
38
+ *
39
+ * @property value - Current field value
40
+ * @property error - Current error message (if any)
41
+ * @property touched - Whether field has been focused/interacted with
42
+ * @property isDirty - Whether field has been modified from initial value
43
+ * @property required - Required validation rule
44
+ * @property pattern - Regex pattern for string validation
45
+ * @property min - Minimum value or length
46
+ * @property max - Maximum value or length
47
+ *
48
+ * @example
49
+ * const emailField: Field<string> = {
50
+ * value: "",
51
+ * error: null,
52
+ * touched: false,
53
+ * isDirty: false,
54
+ * required: { value: true, message: "Email is required" },
55
+ * pattern: {
56
+ * value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
57
+ * message: "Invalid email format"
58
+ * }
59
+ * }
60
+ */
11
61
  type Field<T> = {
12
62
  value: T;
13
63
  error?: string | null;
64
+ touched?: boolean;
65
+ isDirty?: boolean;
14
66
  required?: RequiredRule;
15
67
  pattern?: {
16
68
  value: RegExp;
@@ -19,22 +71,111 @@ type Field<T> = {
19
71
  min?: NumberRule;
20
72
  max?: NumberRule;
21
73
  };
22
- type ResolverResult<T> = {
74
+ /**
75
+ * Validation resolver result containing validated values and errors.
76
+ * Used as return type for custom validation resolvers (Zod, Yup, etc).
77
+ *
78
+ * @template T - Type of form values
79
+ * @property values - Validated values if validation passes
80
+ * @property errors - Object containing field-level errors if validation fails
81
+ *
82
+ * @example
83
+ * // Successful validation
84
+ * { values: { email: "user@example.com", age: 25 } }
85
+ *
86
+ * // Failed validation
87
+ * { errors: { email: "Invalid email", age: "Must be 18+" } }
88
+ */
89
+ type ResolverResult<T extends Record<string, any>> = {
23
90
  values?: T;
24
91
  errors?: Partial<Record<keyof T, string>>;
25
92
  };
26
- type Resolver<T> = (values: T) => ResolverResult<T> | Promise<ResolverResult<T>>;
27
- interface HookFormParams<T> {
93
+ /**
94
+ * Custom validation resolver function for external validation libraries.
95
+ * Can be synchronous or asynchronous.
96
+ *
97
+ * @template T - Type of form values
98
+ * @param values - Current form values to validate
99
+ * @returns Validation result with values or errors
100
+ *
101
+ * @example
102
+ * // Zod resolver
103
+ * const schema = z.object({
104
+ * email: z.string().email(),
105
+ * password: z.string().min(8)
106
+ * });
107
+ *
108
+ * const resolver = async (values) => {
109
+ * const result = schema.safeParse(values);
110
+ * if (result.success) {
111
+ * return { values: result.data };
112
+ * }
113
+ * const errors = {};
114
+ * result.error.issues.forEach(issue => {
115
+ * errors[issue.path[0]] = issue.message;
116
+ * });
117
+ * return { errors };
118
+ * };
119
+ */
120
+ type Resolver<T extends Record<string, any>> = (values: T) => ResolverResult<T> | Promise<ResolverResult<T>>;
121
+ /**
122
+ * Configuration parameters for form creation.
123
+ * Defines initial values and optional custom validation.
124
+ *
125
+ * @template T - Type of form values (must be an object)
126
+ * @property defaultValues - Initial field values with optional validation rules
127
+ * @property resolver - Optional custom validation function (Zod, Yup, etc)
128
+ *
129
+ * @example
130
+ * const params: HookFormParams<{ email: string; age: number }> = {
131
+ * defaultValues: {
132
+ * email: {
133
+ * value: "",
134
+ * required: { value: true, message: "Email is required" }
135
+ * },
136
+ * age: { value: 0, min: 18 }
137
+ * },
138
+ * resolver: zodResolver(schema)
139
+ * }
140
+ */
141
+ interface HookFormParams<T extends Record<string, any>> {
28
142
  defaultValues: {
29
143
  [K in keyof T]: Field<T[K]> | T[K];
30
144
  };
31
145
  resolver?: Resolver<T>;
32
146
  }
33
- interface ControllerProps<T> {
147
+ /**
148
+ * Props for the Controller component.
149
+ * Configures controlled input rendering and additional input properties.
150
+ *
151
+ * @template T - Type of form values
152
+ * @property field - The field name being controlled
153
+ * @property render - Function that renders the input element with form props
154
+ * @property autoFocus - Auto-focus this input on mount
155
+ * @property onBlur - Custom callback when input loses focus
156
+ *
157
+ * @example
158
+ * const props: ControllerProps<FormData> = {
159
+ * field: "email",
160
+ * onBlur: () => console.log("Email field blurred"),
161
+ * render: (field) => (
162
+ * <input
163
+ * value={field.value}
164
+ * onChange={(e) => field.onChange(e.target.value)}
165
+ * ref={field.ref}
166
+ * disabled={field.disabled}
167
+ * />
168
+ * )
169
+ * }
170
+ */
171
+ interface ControllerProps<T extends Record<string, any>> {
34
172
  field: keyof T;
35
- render: (value: string, error: string, onChange: (value: string) => void) => React.ReactNode;
173
+ render: (field: {
174
+ value: any;
175
+ error: string;
176
+ onChange: (value: string) => void;
177
+ }) => React.ReactNode;
36
178
  }
37
- type HandleSubmitType<T> = (cb: (data: T) => void) => (e: React.FormEvent<HTMLFormElement>) => void;
38
179
 
39
180
  /**
40
181
  * Creates a Zod resolver for form validation.
@@ -72,9 +213,79 @@ declare const yupResolver: (schema: any) => (values: any) => Promise<{
72
213
  values?: undefined;
73
214
  }>;
74
215
 
75
- declare function createForm<T>(params: HookFormParams<T>): () => {
76
- handleSubmit: HandleSubmitType<T>;
77
- Controller: ({ field, render }: ControllerProps<T>) => react.ReactNode;
216
+ /**
217
+ * Creates a type-safe form with validation, state management, and controller components.
218
+ * Supports both built-in validation and custom resolvers (Zod, Yup).
219
+ *
220
+ * @template T - Form values type (must be an object)
221
+ * @param params - Form configuration
222
+ * @param params.defaultValues - Default field values with optional validation rules
223
+ * @param params.resolver - Optional custom validation resolver (Zod or Yup)
224
+ * @returns A hook function that returns form methods and Controller component
225
+ *
226
+ * @example
227
+ * // Using built-in validation
228
+ * const form = createForm<{ email: string; age: number }>({
229
+ * defaultValues: {
230
+ * email: {
231
+ * value: "",
232
+ * required: { value: true, message: "Email is required" },
233
+ * pattern: { value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, message: "Invalid email" }
234
+ * },
235
+ * age: {
236
+ * value: 0,
237
+ * required: true,
238
+ * min: { value: 18, message: "Must be 18+" }
239
+ * }
240
+ * }
241
+ * });
242
+ *
243
+ * const { handleSubmit, Controller } = form();
244
+ *
245
+ * // In your component:
246
+ * // <form onSubmit={handleSubmit((data) => console.log(data))}>
247
+ * // <Controller
248
+ * // field="email"
249
+ * // render={(value, error, onChange) => (
250
+ * // <div>
251
+ * // <input value={value} onChange={(e) => onChange(e.target.value)} />
252
+ * // {error && <span>{error}</span>}
253
+ * // </div>
254
+ * // )}
255
+ * // />
256
+ * // </form>
257
+ *
258
+ * @example
259
+ * // Using Zod resolver
260
+ * import { z } from 'zod';
261
+ * import { zodResolver } from './index';
262
+ *
263
+ * const schema = z.object({
264
+ * email: z.string().email("Invalid email"),
265
+ * password: z.string().min(8, "Min 8 characters")
266
+ * });
267
+ *
268
+ * const form = createForm({
269
+ * defaultValues: {
270
+ * email: "",
271
+ * password: ""
272
+ * },
273
+ * resolver: zodResolver(schema)
274
+ * });
275
+ */
276
+ declare function createForm<T extends Record<string, any>>(params: HookFormParams<T>): () => {
277
+ handleSubmit: (cb: (data: T) => void) => (e: React$1.FormEvent<HTMLFormElement>) => Promise<void>;
278
+ Controller: ({ field, render }: ControllerProps<T>) => React$1.ReactNode;
279
+ setValue: (key: keyof T, value: T[keyof T]) => void;
280
+ getvalues: (key?: keyof T | undefined) => void;
281
+ getErrors: (key?: keyof T | undefined) => string | Partial<Record<keyof T, string>>;
282
+ setError: (field: keyof T, error: string) => void;
283
+ reset: () => void;
284
+ watch: (key: keyof T) => T[keyof T];
285
+ setTouched: (field: keyof T, touched: boolean) => void;
286
+ isDirty: (field?: keyof T | undefined) => boolean;
287
+ clearAllErrors: () => void;
288
+ clearFieldError: (field: keyof T) => void;
78
289
  };
79
290
 
80
291
  export { createForm, yupResolver, zodResolver };
@@ -1,16 +1,68 @@
1
- import * as react from 'react';
1
+ import React$1 from 'react';
2
2
 
3
+ /**
4
+ * Required field validation rule.
5
+ * Can be a simple boolean or an object with custom error message.
6
+ *
7
+ * @example
8
+ * // Simple required
9
+ * required: true
10
+ *
11
+ * // With custom message
12
+ * required: { value: true, message: "Email is required" }
13
+ */
3
14
  type RequiredRule = boolean | {
4
15
  value: boolean;
5
16
  message: string;
6
17
  };
18
+ /**
19
+ * Numeric validation rule for min/max constraints.
20
+ * Can be a plain number or an object with custom error message.
21
+ *
22
+ * @example
23
+ * // Simple min value
24
+ * min: 18
25
+ *
26
+ * // With custom message
27
+ * min: { value: 18, message: "Must be 18 or older" }
28
+ */
7
29
  type NumberRule = number | {
8
30
  value: number;
9
31
  message: string;
10
32
  };
33
+ /**
34
+ * Individual form field state and configuration.
35
+ * Tracks value, error, validation rules, and user interaction state.
36
+ *
37
+ * @template T - The type of the field value
38
+ *
39
+ * @property value - Current field value
40
+ * @property error - Current error message (if any)
41
+ * @property touched - Whether field has been focused/interacted with
42
+ * @property isDirty - Whether field has been modified from initial value
43
+ * @property required - Required validation rule
44
+ * @property pattern - Regex pattern for string validation
45
+ * @property min - Minimum value or length
46
+ * @property max - Maximum value or length
47
+ *
48
+ * @example
49
+ * const emailField: Field<string> = {
50
+ * value: "",
51
+ * error: null,
52
+ * touched: false,
53
+ * isDirty: false,
54
+ * required: { value: true, message: "Email is required" },
55
+ * pattern: {
56
+ * value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
57
+ * message: "Invalid email format"
58
+ * }
59
+ * }
60
+ */
11
61
  type Field<T> = {
12
62
  value: T;
13
63
  error?: string | null;
64
+ touched?: boolean;
65
+ isDirty?: boolean;
14
66
  required?: RequiredRule;
15
67
  pattern?: {
16
68
  value: RegExp;
@@ -19,22 +71,111 @@ type Field<T> = {
19
71
  min?: NumberRule;
20
72
  max?: NumberRule;
21
73
  };
22
- type ResolverResult<T> = {
74
+ /**
75
+ * Validation resolver result containing validated values and errors.
76
+ * Used as return type for custom validation resolvers (Zod, Yup, etc).
77
+ *
78
+ * @template T - Type of form values
79
+ * @property values - Validated values if validation passes
80
+ * @property errors - Object containing field-level errors if validation fails
81
+ *
82
+ * @example
83
+ * // Successful validation
84
+ * { values: { email: "user@example.com", age: 25 } }
85
+ *
86
+ * // Failed validation
87
+ * { errors: { email: "Invalid email", age: "Must be 18+" } }
88
+ */
89
+ type ResolverResult<T extends Record<string, any>> = {
23
90
  values?: T;
24
91
  errors?: Partial<Record<keyof T, string>>;
25
92
  };
26
- type Resolver<T> = (values: T) => ResolverResult<T> | Promise<ResolverResult<T>>;
27
- interface HookFormParams<T> {
93
+ /**
94
+ * Custom validation resolver function for external validation libraries.
95
+ * Can be synchronous or asynchronous.
96
+ *
97
+ * @template T - Type of form values
98
+ * @param values - Current form values to validate
99
+ * @returns Validation result with values or errors
100
+ *
101
+ * @example
102
+ * // Zod resolver
103
+ * const schema = z.object({
104
+ * email: z.string().email(),
105
+ * password: z.string().min(8)
106
+ * });
107
+ *
108
+ * const resolver = async (values) => {
109
+ * const result = schema.safeParse(values);
110
+ * if (result.success) {
111
+ * return { values: result.data };
112
+ * }
113
+ * const errors = {};
114
+ * result.error.issues.forEach(issue => {
115
+ * errors[issue.path[0]] = issue.message;
116
+ * });
117
+ * return { errors };
118
+ * };
119
+ */
120
+ type Resolver<T extends Record<string, any>> = (values: T) => ResolverResult<T> | Promise<ResolverResult<T>>;
121
+ /**
122
+ * Configuration parameters for form creation.
123
+ * Defines initial values and optional custom validation.
124
+ *
125
+ * @template T - Type of form values (must be an object)
126
+ * @property defaultValues - Initial field values with optional validation rules
127
+ * @property resolver - Optional custom validation function (Zod, Yup, etc)
128
+ *
129
+ * @example
130
+ * const params: HookFormParams<{ email: string; age: number }> = {
131
+ * defaultValues: {
132
+ * email: {
133
+ * value: "",
134
+ * required: { value: true, message: "Email is required" }
135
+ * },
136
+ * age: { value: 0, min: 18 }
137
+ * },
138
+ * resolver: zodResolver(schema)
139
+ * }
140
+ */
141
+ interface HookFormParams<T extends Record<string, any>> {
28
142
  defaultValues: {
29
143
  [K in keyof T]: Field<T[K]> | T[K];
30
144
  };
31
145
  resolver?: Resolver<T>;
32
146
  }
33
- interface ControllerProps<T> {
147
+ /**
148
+ * Props for the Controller component.
149
+ * Configures controlled input rendering and additional input properties.
150
+ *
151
+ * @template T - Type of form values
152
+ * @property field - The field name being controlled
153
+ * @property render - Function that renders the input element with form props
154
+ * @property autoFocus - Auto-focus this input on mount
155
+ * @property onBlur - Custom callback when input loses focus
156
+ *
157
+ * @example
158
+ * const props: ControllerProps<FormData> = {
159
+ * field: "email",
160
+ * onBlur: () => console.log("Email field blurred"),
161
+ * render: (field) => (
162
+ * <input
163
+ * value={field.value}
164
+ * onChange={(e) => field.onChange(e.target.value)}
165
+ * ref={field.ref}
166
+ * disabled={field.disabled}
167
+ * />
168
+ * )
169
+ * }
170
+ */
171
+ interface ControllerProps<T extends Record<string, any>> {
34
172
  field: keyof T;
35
- render: (value: string, error: string, onChange: (value: string) => void) => React.ReactNode;
173
+ render: (field: {
174
+ value: any;
175
+ error: string;
176
+ onChange: (value: string) => void;
177
+ }) => React.ReactNode;
36
178
  }
37
- type HandleSubmitType<T> = (cb: (data: T) => void) => (e: React.FormEvent<HTMLFormElement>) => void;
38
179
 
39
180
  /**
40
181
  * Creates a Zod resolver for form validation.
@@ -72,9 +213,79 @@ declare const yupResolver: (schema: any) => (values: any) => Promise<{
72
213
  values?: undefined;
73
214
  }>;
74
215
 
75
- declare function createForm<T>(params: HookFormParams<T>): () => {
76
- handleSubmit: HandleSubmitType<T>;
77
- Controller: ({ field, render }: ControllerProps<T>) => react.ReactNode;
216
+ /**
217
+ * Creates a type-safe form with validation, state management, and controller components.
218
+ * Supports both built-in validation and custom resolvers (Zod, Yup).
219
+ *
220
+ * @template T - Form values type (must be an object)
221
+ * @param params - Form configuration
222
+ * @param params.defaultValues - Default field values with optional validation rules
223
+ * @param params.resolver - Optional custom validation resolver (Zod or Yup)
224
+ * @returns A hook function that returns form methods and Controller component
225
+ *
226
+ * @example
227
+ * // Using built-in validation
228
+ * const form = createForm<{ email: string; age: number }>({
229
+ * defaultValues: {
230
+ * email: {
231
+ * value: "",
232
+ * required: { value: true, message: "Email is required" },
233
+ * pattern: { value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, message: "Invalid email" }
234
+ * },
235
+ * age: {
236
+ * value: 0,
237
+ * required: true,
238
+ * min: { value: 18, message: "Must be 18+" }
239
+ * }
240
+ * }
241
+ * });
242
+ *
243
+ * const { handleSubmit, Controller } = form();
244
+ *
245
+ * // In your component:
246
+ * // <form onSubmit={handleSubmit((data) => console.log(data))}>
247
+ * // <Controller
248
+ * // field="email"
249
+ * // render={(value, error, onChange) => (
250
+ * // <div>
251
+ * // <input value={value} onChange={(e) => onChange(e.target.value)} />
252
+ * // {error && <span>{error}</span>}
253
+ * // </div>
254
+ * // )}
255
+ * // />
256
+ * // </form>
257
+ *
258
+ * @example
259
+ * // Using Zod resolver
260
+ * import { z } from 'zod';
261
+ * import { zodResolver } from './index';
262
+ *
263
+ * const schema = z.object({
264
+ * email: z.string().email("Invalid email"),
265
+ * password: z.string().min(8, "Min 8 characters")
266
+ * });
267
+ *
268
+ * const form = createForm({
269
+ * defaultValues: {
270
+ * email: "",
271
+ * password: ""
272
+ * },
273
+ * resolver: zodResolver(schema)
274
+ * });
275
+ */
276
+ declare function createForm<T extends Record<string, any>>(params: HookFormParams<T>): () => {
277
+ handleSubmit: (cb: (data: T) => void) => (e: React$1.FormEvent<HTMLFormElement>) => Promise<void>;
278
+ Controller: ({ field, render }: ControllerProps<T>) => React$1.ReactNode;
279
+ setValue: (key: keyof T, value: T[keyof T]) => void;
280
+ getvalues: (key?: keyof T | undefined) => void;
281
+ getErrors: (key?: keyof T | undefined) => string | Partial<Record<keyof T, string>>;
282
+ setError: (field: keyof T, error: string) => void;
283
+ reset: () => void;
284
+ watch: (key: keyof T) => T[keyof T];
285
+ setTouched: (field: keyof T, touched: boolean) => void;
286
+ isDirty: (field?: keyof T | undefined) => boolean;
287
+ clearAllErrors: () => void;
288
+ clearFieldError: (field: keyof T) => void;
78
289
  };
79
290
 
80
291
  export { createForm, yupResolver, zodResolver };
@@ -1 +1 @@
1
- "use strict";var T=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var C=(e,r)=>{for(var t in r)T(e,t,{get:r[t],enumerable:!0})},M=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let u of P(r))!E.call(e,u)&&u!==t&&T(e,u,{get:()=>r[u],enumerable:!(n=w(r,u))||n.enumerable});return e};var q=e=>M(T({},"__esModule",{value:!0}),e);var H={};C(H,{createForm:()=>z,yupResolver:()=>V,zodResolver:()=>x});module.exports=q(H);var h=require("react");function S(e,r=[]){let t,n=[],u=a=>{let s=typeof a=="function"?a(t):a;t={...t,...s},n.forEach(i=>i())},p=()=>t,c=N(u,p,r);t=e(c,p);let m=a=>(n.push(a),()=>{n=n.filter(s=>s!==a)});return(a=s=>s)=>(0,h.useSyncExternalStore)(m,()=>a(t))}var N=(e,r,t)=>!t||t.length===0?e:t.reduceRight((n,u)=>u(e,r)(n),e);function j(e){return e.charAt(0).toUpperCase()+e.slice(1)}function R(e,r){return e?typeof e=="boolean"?{value:e,message:e?`${j(r)} is required`:""}:e:{value:!1,message:""}}function v(e,r){return e===void 0?null:typeof e=="number"?{value:e,message:r==="min"?`Minimum is ${e}`:`Maximum is ${e}`}:e}function g(e){let r={};return Object.keys(e||{}).forEach(t=>{let n=e[t];typeof n=="object"&&"value"in n&&(r[t]=n.value)}),r}var x=e=>r=>{let t=e.safeParse(r);if(t.success)return{values:t.data};let n={};return t.error.issues.forEach(u=>{let p=u.path[0];n[p]=u.message}),{errors:n}},V=e=>async r=>{try{return{values:await e.validate(r,{abortEarly:!1})}}catch(t){let n={};return t.inner.forEach(u=>{n[u.path]=u.message}),{errors:n}}};function F(e){let r={};return Object.keys(e).forEach(t=>{let n=e[t];n&&typeof n=="object"&&"value"in n?r[t]={error:"",...n}:r[t]={value:n,error:""}}),r}function b(e,r){if(typeof r=="number"){let t=Number(e);return isNaN(t)?t:0}return typeof r=="boolean"?!!e:e}function z(e){let{defaultValues:r,resolver:t}=e,n=F(r),u=S((c,m)=>({...n,setFieldValue:(a,s)=>{var o;let i=m()[a];c({[a]:{...i,value:b(s,(o=n[a])==null?void 0:o.value)}})},defaultValidateField:a=>{var d;let s=m()[a],i="",o=b(s.value,(d=s[a])==null?void 0:d.value),y=R(s.required,a),l=v(s.min,"min"),f=v(s.max,"max");return y.value&&!o?i=y.message:typeof o=="string"&&s.pattern&&!s.pattern.value.test(o)?i=s.pattern.message:l&&(typeof o=="string"&&o.length<l.value||typeof o=="number"&&o<l.value)?i=l.message:f&&(typeof o=="string"&&o.length>f.value||typeof o=="number"&&o>f.value)&&(i=f.message),c({[a]:{...s,error:i}}),i},resolverValidate:async a=>{if(!t)return;let s=m(),i=s[a],o="",y=g(s),l=await t(y);if(l!=null&&l.errors){let f=l.errors[a];f&&(o=f)}return c({[a]:{...i,error:o}}),o},handleSubmit:a=>async s=>{s.preventDefault();let i=!1,o=m(),y=g(o),l=Object.keys(y);if(t){for(let f of l)await o.resolverValidate(f)&&(i=!0);i||a(y);return}for(let f of l)await o.defaultValidateField(f)&&(i=!0);i||a(y)}}));function p({field:c,render:m}){let a=u(),s=a[c].value,i=a[c].error,o=a.setFieldValue,y=a.defaultValidateField,l=a.resolverValidate;return m(s,i,async k=>{if(o(c,k),t){await l(c);return}y(c)})}return()=>({handleSubmit:u(m=>m.handleSubmit),Controller:p})}0&&(module.exports={createForm,yupResolver,zodResolver});
1
+ "use strict";var g=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var D=(t,s)=>{for(var a in s)g(t,a,{get:s[a],enumerable:!0})},M=(t,s,a,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let c of j(s))!C.call(t,c)&&c!==a&&g(t,c,{get:()=>s[c],enumerable:!(i=w(s,c))||i.enumerable});return t};var q=t=>M(g({},"__esModule",{value:!0}),t);var A={};D(A,{createForm:()=>z,yupResolver:()=>F,zodResolver:()=>R});module.exports=q(A);var b=require("react");function S(t,s=[]){let a,i=[],c=e=>{let o=typeof e=="function"?e(a):e;a={...a,...o},i.forEach(r=>r())},p=()=>a,f=N(c,p,s);a=t(f,p);let y=e=>(i.push(e),()=>{i=i.filter(o=>o!==e)});return(e=o=>o)=>(0,b.useSyncExternalStore)(y,()=>e(a))}var N=(t,s,a)=>!a||a.length===0?t:a.reduceRight((i,c)=>c(t,s)(i),t);function O(t){return t.charAt(0).toUpperCase()+t.slice(1)}function E(t,s){return t?typeof t=="boolean"?{value:t,message:t?`${O(s)} is required`:""}:t:{value:!1,message:""}}function h(t,s){return t===void 0?null:typeof t=="number"?{value:t,message:s==="min"?`Minimum is ${t}`:`Maximum is ${t}`}:t}function v(t){let s={};return Object.keys(t||{}).forEach(a=>{let i=t[a];typeof i=="object"&&"value"in i&&(s[a]=i.value)}),s}var R=t=>s=>{let a=t.safeParse(s);if(a.success)return{values:a.data};let i={};return a.error.issues.forEach(c=>{let p=c.path[0];i[p]=c.message}),{errors:i}},F=t=>async s=>{try{return{values:await t.validate(s,{abortEarly:!1})}}catch(a){let i={};return a.inner.forEach(c=>{i[c.path]=c.message}),{errors:i}}};function V(t){let s={};return Object.keys(t).forEach(a=>{let i=t[a];i&&typeof i=="object"&&"value"in i?s[a]={error:"",...i}:s[a]={value:i,error:""}}),s}function k(t,s){if(typeof s=="number"){let a=Number(t);return isNaN(a)?0:a}return typeof s=="boolean"?!!t:t}function z(t){let{defaultValues:s,resolver:a}=t,i=V(s),c=S((f,y)=>({...i,setFieldValue:(e,o)=>{var n;let r=y()[e];f({[e]:{...r,value:k(o,(n=i[e])==null?void 0:n.value)}})},defaultValidateField:e=>{let r=y()[e],n="",u=k(r.value,r.value),T=E(r.required,String(e)),d=h(r.min,"min"),m=h(r.max,"max");return T.value&&!u?n=T.message:typeof u=="string"&&r.pattern&&!r.pattern.value.test(u)?n=r.pattern.message:d&&(typeof u=="string"&&u.length<d.value||typeof u=="number"&&u<d.value)?n=d.message:m&&(typeof u=="string"&&u.length>m.value||typeof u=="number"&&u>m.value)&&(n=m.message),f({[e]:{...r,error:n}}),n},resolverValidate:async e=>{if(!a)return;let o=y(),r=o[e],n="",u=v(o),T=await a(u);if(T!=null&&T.errors){let d=T.errors[e];d&&(n=d)}return f({[e]:{...r,error:n}}),n},handleSubmit:e=>async o=>{o.preventDefault();let r=!1,n=y(),u=v(n),T=Object.keys(u);if(a){for(let d of T)await n.resolverValidate(d)&&(r=!0);r||e(u);return}for(let d of T)n.defaultValidateField(d)&&(r=!0);r||e(u)},getvalues:e=>e?y()[e].value:v(e),setValue:(e,o)=>{f({[e]:{...y()[e],value:o}})},setError:(e,o)=>{let r=y()[e];f({[e]:{...r,error:o}})},getErrors:e=>{let o=y();if(e){let n=o[e];return(n==null?void 0:n.error)||""}let r={};return Object.keys(o).forEach(n=>{let u=o[n];u.error&&(r[n]=u.error)}),r},clearFieldError:e=>{let o=y()[e];f({[e]:{...o,error:""}})},clearAllErrors:()=>{let e=y(),o={};Object.keys(e).forEach(r=>{let n=e[r];o[r]={...n,error:""}}),f(o)},isDirty:e=>{let o=y();if(e){let r=o[e];return(r==null?void 0:r.isDirty)||!1}return Object.keys(o).some(r=>o[r].isDirty)},isTouched:e=>{let r=y()[e];return(r==null?void 0:r.touched)||!1},setTouched:(e,o)=>{let r=y()[e];f({[e]:{...r,touched:o}})},reset:()=>{let e={};Object.keys(i).forEach(o=>{e[o]={...i[o],error:"",touched:!1,isDirty:!1}}),f(e)}}));function p({field:f,render:y}){let e=c(),o=e[f].value,r=e[f].error,n=e.setFieldValue,u=e.defaultValidateField,T=e.resolverValidate,d=e.setTouched;return y({error:r||"",onChange:async l=>{n(f,l),d(f,!0),a?await T(f):u(f)},value:o})}return()=>{let f=c(l=>l.handleSubmit),y=c(l=>l.setValue),e=c(l=>l.getvalues),o=c(l=>l.getErrors),r=c(l=>l.setError),n=c(l=>l.reset),u=c(l=>l.setTouched),T=c(l=>l.isDirty),d=c(l=>l.clearAllErrors),m=c(l=>l.clearFieldError);return{handleSubmit:f,Controller:p,setValue:y,getvalues:e,getErrors:o,setError:r,reset:n,watch:l=>c(P=>P[l].value),setTouched:u,isDirty:T,clearAllErrors:d,clearFieldError:m}}}0&&(module.exports={createForm,yupResolver,zodResolver});
@@ -1 +1 @@
1
- import{a as b}from"../chunk-RXPCG2VZ.mjs";function x(e){return e.charAt(0).toUpperCase()+e.slice(1)}function h(e,n){return e?typeof e=="boolean"?{value:e,message:e?`${x(n)} is required`:""}:e:{value:!1,message:""}}function v(e,n){return e===void 0?null:typeof e=="number"?{value:e,message:n==="min"?`Minimum is ${e}`:`Maximum is ${e}`}:e}function g(e){let n={};return Object.keys(e||{}).forEach(r=>{let s=e[r];typeof s=="object"&&"value"in s&&(n[r]=s.value)}),n}var F=e=>n=>{let r=e.safeParse(n);if(r.success)return{values:r.data};let s={};return r.error.issues.forEach(m=>{let d=m.path[0];s[d]=m.message}),{errors:s}},k=e=>async n=>{try{return{values:await e.validate(n,{abortEarly:!1})}}catch(r){let s={};return r.inner.forEach(m=>{s[m.path]=m.message}),{errors:s}}};function R(e){let n={};return Object.keys(e).forEach(r=>{let s=e[r];s&&typeof s=="object"&&"value"in s?n[r]={error:"",...s}:n[r]={value:s,error:""}}),n}function T(e,n){if(typeof n=="number"){let r=Number(e);return isNaN(r)?r:0}return typeof n=="boolean"?!!e:e}function w(e){let{defaultValues:n,resolver:r}=e,s=R(n),m=b((c,y)=>({...s,setFieldValue:(a,o)=>{var t;let u=y()[a];c({[a]:{...u,value:T(o,(t=s[a])==null?void 0:t.value)}})},defaultValidateField:a=>{var p;let o=y()[a],u="",t=T(o.value,(p=o[a])==null?void 0:p.value),f=h(o.required,a),i=v(o.min,"min"),l=v(o.max,"max");return f.value&&!t?u=f.message:typeof t=="string"&&o.pattern&&!o.pattern.value.test(t)?u=o.pattern.message:i&&(typeof t=="string"&&t.length<i.value||typeof t=="number"&&t<i.value)?u=i.message:l&&(typeof t=="string"&&t.length>l.value||typeof t=="number"&&t>l.value)&&(u=l.message),c({[a]:{...o,error:u}}),u},resolverValidate:async a=>{if(!r)return;let o=y(),u=o[a],t="",f=g(o),i=await r(f);if(i!=null&&i.errors){let l=i.errors[a];l&&(t=l)}return c({[a]:{...u,error:t}}),t},handleSubmit:a=>async o=>{o.preventDefault();let u=!1,t=y(),f=g(t),i=Object.keys(f);if(r){for(let l of i)await t.resolverValidate(l)&&(u=!0);u||a(f);return}for(let l of i)await t.defaultValidateField(l)&&(u=!0);u||a(f)}}));function d({field:c,render:y}){let a=m(),o=a[c].value,u=a[c].error,t=a.setFieldValue,f=a.defaultValidateField,i=a.resolverValidate;return y(o,u,async V=>{if(t(c,V),r){await i(c);return}f(c)})}return()=>({handleSubmit:m(y=>y.handleSubmit),Controller:d})}export{w as createForm,k as yupResolver,F as zodResolver};
1
+ import{a as h}from"../chunk-RXPCG2VZ.mjs";function V(o){return o.charAt(0).toUpperCase()+o.slice(1)}function b(o,u){return o?typeof o=="boolean"?{value:o,message:o?`${V(u)} is required`:""}:o:{value:!1,message:""}}function g(o,u){return o===void 0?null:typeof o=="number"?{value:o,message:u==="min"?`Minimum is ${o}`:`Maximum is ${o}`}:o}function v(o){let u={};return Object.keys(o||{}).forEach(i=>{let c=o[i];typeof c=="object"&&"value"in c&&(u[i]=c.value)}),u}var x=o=>u=>{let i=o.safeParse(u);if(i.success)return{values:i.data};let c={};return i.error.issues.forEach(l=>{let p=l.path[0];c[p]=l.message}),{errors:c}},S=o=>async u=>{try{return{values:await o.validate(u,{abortEarly:!1})}}catch(i){let c={};return i.inner.forEach(l=>{c[l.path]=l.message}),{errors:c}}};function E(o){let u={};return Object.keys(o).forEach(i=>{let c=o[i];c&&typeof c=="object"&&"value"in c?u[i]={error:"",...c}:u[i]={value:c,error:""}}),u}function k(o,u){if(typeof u=="number"){let i=Number(o);return isNaN(i)?0:i}return typeof u=="boolean"?!!o:o}function w(o){let{defaultValues:u,resolver:i}=o,c=E(u),l=h((f,y)=>({...c,setFieldValue:(e,t)=>{var s;let r=y()[e];f({[e]:{...r,value:k(t,(s=c[e])==null?void 0:s.value)}})},defaultValidateField:e=>{let r=y()[e],s="",a=k(r.value,r.value),m=b(r.required,String(e)),d=g(r.min,"min"),T=g(r.max,"max");return m.value&&!a?s=m.message:typeof a=="string"&&r.pattern&&!r.pattern.value.test(a)?s=r.pattern.message:d&&(typeof a=="string"&&a.length<d.value||typeof a=="number"&&a<d.value)?s=d.message:T&&(typeof a=="string"&&a.length>T.value||typeof a=="number"&&a>T.value)&&(s=T.message),f({[e]:{...r,error:s}}),s},resolverValidate:async e=>{if(!i)return;let t=y(),r=t[e],s="",a=v(t),m=await i(a);if(m!=null&&m.errors){let d=m.errors[e];d&&(s=d)}return f({[e]:{...r,error:s}}),s},handleSubmit:e=>async t=>{t.preventDefault();let r=!1,s=y(),a=v(s),m=Object.keys(a);if(i){for(let d of m)await s.resolverValidate(d)&&(r=!0);r||e(a);return}for(let d of m)s.defaultValidateField(d)&&(r=!0);r||e(a)},getvalues:e=>e?y()[e].value:v(e),setValue:(e,t)=>{f({[e]:{...y()[e],value:t}})},setError:(e,t)=>{let r=y()[e];f({[e]:{...r,error:t}})},getErrors:e=>{let t=y();if(e){let s=t[e];return(s==null?void 0:s.error)||""}let r={};return Object.keys(t).forEach(s=>{let a=t[s];a.error&&(r[s]=a.error)}),r},clearFieldError:e=>{let t=y()[e];f({[e]:{...t,error:""}})},clearAllErrors:()=>{let e=y(),t={};Object.keys(e).forEach(r=>{let s=e[r];t[r]={...s,error:""}}),f(t)},isDirty:e=>{let t=y();if(e){let r=t[e];return(r==null?void 0:r.isDirty)||!1}return Object.keys(t).some(r=>t[r].isDirty)},isTouched:e=>{let r=y()[e];return(r==null?void 0:r.touched)||!1},setTouched:(e,t)=>{let r=y()[e];f({[e]:{...r,touched:t}})},reset:()=>{let e={};Object.keys(c).forEach(t=>{e[t]={...c[t],error:"",touched:!1,isDirty:!1}}),f(e)}}));function p({field:f,render:y}){let e=l(),t=e[f].value,r=e[f].error,s=e.setFieldValue,a=e.defaultValidateField,m=e.resolverValidate,d=e.setTouched;return y({error:r||"",onChange:async n=>{s(f,n),d(f,!0),i?await m(f):a(f)},value:t})}return()=>{let f=l(n=>n.handleSubmit),y=l(n=>n.setValue),e=l(n=>n.getvalues),t=l(n=>n.getErrors),r=l(n=>n.setError),s=l(n=>n.reset),a=l(n=>n.setTouched),m=l(n=>n.isDirty),d=l(n=>n.clearAllErrors),T=l(n=>n.clearFieldError);return{handleSubmit:f,Controller:p,setValue:y,getvalues:e,getErrors:t,setError:r,reset:s,watch:n=>l(F=>F[n].value),setTouched:a,isDirty:m,clearAllErrors:d,clearFieldError:T}}}export{w as createForm,S as yupResolver,x as zodResolver};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zustic",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "A fast, minimal state management solution for React ecosystems. Works seamlessly with React, Next.js, and React Native, offering predictable state updates with a tiny footprint.",
5
5
  "scripts": {
6
6
  "build": "tsup",