@radicalbit/formbit 2.0.0 → 3.0.0
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/README.md +517 -649
- package/dist/__tests__/is-validation-error.test.d.ts +1 -0
- package/dist/formbit-context.d.ts +4 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +13 -13
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +13 -13
- package/dist/index.modern.js.map +1 -1
- package/dist/types/index.d.ts +135 -387
- package/dist/use-execute-callbacks.d.ts +2 -2
- package/dist/use-formbit.d.ts +5 -5
- package/dist/validate-sync-all.d.ts +2 -2
- package/package.json +4 -3
package/dist/types/index.d.ts
CHANGED
|
@@ -1,430 +1,178 @@
|
|
|
1
1
|
import { ObjectSchema, ValidationError as YupValidationError, ValidateOptions as YupValidateOptions } from 'yup';
|
|
2
2
|
import { ACTIONS } from '../helpers/constants';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Checks the given json against the form schema and returns and array of errors.
|
|
9
|
-
* It returns undefined if the json is valid.
|
|
10
|
-
*
|
|
11
|
-
*/
|
|
12
|
-
export type Check<Values extends InitialValues> = (json: Form, options?: CheckFnOptions<Values>) => ValidationError[] | undefined;
|
|
13
|
-
/**
|
|
14
|
-
* Reset isDirty value to false
|
|
15
|
-
*/
|
|
16
|
-
export type ClearIsDirty = () => void;
|
|
17
|
-
/**
|
|
18
|
-
* Invoked in case of errors raised by validation
|
|
19
|
-
*/
|
|
20
|
-
export type ErrorCallback<Values extends InitialValues> = (writer: Writer<Values>, setError: SetError) => void;
|
|
21
|
-
/**
|
|
22
|
-
* Invoked in case of errors raised by validation of check method
|
|
4
|
+
* Base shape of every form handled by formbit: an open record of values, plus an
|
|
5
|
+
* optional `__metadata` field formbit uses to carry data that must survive a
|
|
6
|
+
* reset/initialize but must NOT be submitted.
|
|
23
7
|
*
|
|
8
|
+
* The generic `T` you pass to `useFormbit<T>()` must extend this type.
|
|
24
9
|
*/
|
|
25
|
-
export type
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* It doesn't trigger any validation
|
|
29
|
-
*
|
|
30
|
-
*/
|
|
31
|
-
export type ErrorFn = (path: string) => string | undefined;
|
|
10
|
+
export type FormbitValues = Record<string, unknown> & {
|
|
11
|
+
__metadata?: Record<string, unknown>;
|
|
12
|
+
};
|
|
32
13
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
14
|
+
* Error messages registered since the last validation, stored under the same
|
|
15
|
+
* dot-path as the corresponding form value.
|
|
35
16
|
*
|
|
36
17
|
* @example
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* {
|
|
40
|
-
* "age": 1
|
|
41
|
-
* }
|
|
42
|
-
* ```
|
|
43
|
-
* and age is a non valid field, errors object will look like this
|
|
44
|
-
* ```json
|
|
45
|
-
* {
|
|
46
|
-
* "age": "Age must be greater then 18"
|
|
47
|
-
* }
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
18
|
+
* form: { age: 1 }
|
|
19
|
+
* errors: { age: "Age must be greater than 18" }
|
|
50
20
|
*/
|
|
51
21
|
export type Errors = Record<string, string>;
|
|
52
22
|
/**
|
|
53
|
-
*
|
|
54
|
-
|
|
55
|
-
export type Form = {
|
|
56
|
-
__metadata?: Object;
|
|
57
|
-
} & Object;
|
|
58
|
-
export type GenericCallback<Values extends InitialValues> = SuccessCallback<Values> | ErrorCallback<Values>;
|
|
59
|
-
/**
|
|
60
|
-
* Initialize the form with new initial values
|
|
61
|
-
*/
|
|
62
|
-
export type Initialize<Values extends InitialValues> = (values: Partial<Values>) => void;
|
|
63
|
-
/**
|
|
64
|
-
* InitialValues used to setup formbit, used also to reset the form to the original version.
|
|
65
|
-
*
|
|
66
|
-
*/
|
|
67
|
-
export type InitialValues = {
|
|
68
|
-
__metadata?: Object;
|
|
69
|
-
} & Object;
|
|
70
|
-
/**
|
|
71
|
-
* Returns true if the form is Dirty (user already interacted with the form), false otherwise.
|
|
72
|
-
*
|
|
73
|
-
*/
|
|
74
|
-
export type IsDirty = boolean;
|
|
75
|
-
/**
|
|
76
|
-
* Returns true if the form is NOT valid
|
|
77
|
-
* It doesn't perform any validation, it checks if any errors are present
|
|
78
|
-
*/
|
|
79
|
-
export type IsFormInvalid = () => boolean;
|
|
80
|
-
/**
|
|
81
|
-
* Returns true id the form is valid
|
|
82
|
-
* It doesn't perform any validation, it checks if any errors are present
|
|
83
|
-
*/
|
|
84
|
-
export type IsFormValid = () => boolean;
|
|
85
|
-
/**
|
|
86
|
-
* Object including all the values that are being live validated.
|
|
87
|
-
* Usually fields that fail validation (using one of the method that triggers validation)
|
|
88
|
-
* will automatically set to be live-validated.
|
|
89
|
-
*
|
|
90
|
-
* A value/path is live-validated when validated at every change of the form.
|
|
91
|
-
*
|
|
92
|
-
* By default no field is live-validated
|
|
23
|
+
* Fields currently under live-validation (re-validated on every form change).
|
|
24
|
+
* A field is added here automatically when it fails a validation. Empty by default.
|
|
93
25
|
*
|
|
94
26
|
* @example
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* {
|
|
98
|
-
* "age": 1
|
|
99
|
-
* }
|
|
100
|
-
* ```
|
|
101
|
-
* and age is a field that is being live-validated, liveValidation object will look like this
|
|
102
|
-
* ```json
|
|
103
|
-
* {
|
|
104
|
-
* "age": "Age must be greater then 18"
|
|
105
|
-
* }
|
|
106
|
-
* ```
|
|
107
|
-
*
|
|
27
|
+
* form: { age: 1 }
|
|
28
|
+
* liveValidation: { age: true }
|
|
108
29
|
*/
|
|
109
30
|
export type LiveValidation = Record<string, true>;
|
|
110
31
|
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*/
|
|
114
|
-
export type LiveValidationFn = (path: string) => true | undefined;
|
|
115
|
-
/**
|
|
116
|
-
* Generic object with string as keys
|
|
117
|
-
*
|
|
118
|
-
*/
|
|
119
|
-
export type Object = Record<string, unknown>;
|
|
120
|
-
/**
|
|
121
|
-
* @private
|
|
122
|
-
*/
|
|
123
|
-
export type PrivateValidateForm<Values extends InitialValues> = (successCallback?: SuccessCallback<Values>, errorCallback?: ErrorCallback<Partial<Values>>, options?: {
|
|
124
|
-
options?: ValidateOptions;
|
|
125
|
-
isDirty?: boolean;
|
|
126
|
-
}) => void;
|
|
127
|
-
/**
|
|
128
|
-
*
|
|
129
|
-
* This method updates the form state deleting value and set isDirty to true.
|
|
130
|
-
*
|
|
131
|
-
* After writing, it validates all the paths contained into pathsToValidate (if any)
|
|
132
|
-
* and all the fields that have the live validation active.
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*/
|
|
136
|
-
export type Remove<Values extends InitialValues> = (path: string, options?: WriteFnOptions<Values>) => void;
|
|
137
|
-
/**
|
|
138
|
-
* Reset form to the initial state.
|
|
139
|
-
* Errors and liveValidation are set back to empty objects.
|
|
140
|
-
* isDirty is set back to false
|
|
141
|
-
*
|
|
142
|
-
*/
|
|
143
|
-
export type ResetForm = () => void;
|
|
144
|
-
/**
|
|
145
|
-
* Type imported from the yup library.
|
|
146
|
-
* It represents any validation schema created with the yup.object() method
|
|
147
|
-
*
|
|
148
|
-
* Link to the Yup documentation {@link https://github.com/jquense/yup}
|
|
149
|
-
*
|
|
150
|
-
*/
|
|
151
|
-
export type ValidationSchema<Values extends InitialValues> = ObjectSchema<Values>;
|
|
152
|
-
/**
|
|
153
|
-
* Set a message(value) to the given error path.
|
|
154
|
-
*
|
|
155
|
-
*/
|
|
156
|
-
export type SetError = (path: string, value: string) => void;
|
|
157
|
-
/**
|
|
158
|
-
* Override the current schema with the given one.
|
|
159
|
-
*
|
|
160
|
-
*/
|
|
161
|
-
export type SetSchema<Values extends InitialValues> = (newSchema: ValidationSchema<Values>) => void;
|
|
162
|
-
/**
|
|
163
|
-
* Perform a validation against the current form object, and execute the successCallback if the validation pass
|
|
164
|
-
* otherwise it executes the errorCallback
|
|
165
|
-
*
|
|
166
|
-
*/
|
|
167
|
-
export type SubmitForm<Values extends InitialValues> = (successCallback: SuccessSubmitCallback<Values>, errorCallback?: ErrorCallback<Partial<Values>>, options?: ValidateOptions) => void;
|
|
168
|
-
/**
|
|
169
|
-
* Success callback invoked by some formbit methods when the operation is successful.
|
|
170
|
-
*
|
|
171
|
-
*/
|
|
172
|
-
export type SuccessCallback<Values extends InitialValues> = (writer: Writer<Values>, setError: SetError) => void;
|
|
173
|
-
/**
|
|
174
|
-
* Success callback invoked by the check method when the operation is successful.
|
|
175
|
-
*
|
|
176
|
-
*/
|
|
177
|
-
export type SuccessCheckCallback<Values extends InitialValues> = (json: Form, writer: Writer<Values>, setError: SetError) => void;
|
|
178
|
-
/**
|
|
179
|
-
* Success callback invoked by the submit method when the validation is successful.
|
|
180
|
-
* Is the right place to send your data to the backend.
|
|
181
|
-
*
|
|
182
|
-
*/
|
|
183
|
-
export type SuccessSubmitCallback<Values extends InitialValues> = (writer: Writer<Values | Omit<Values, '__metadata'>>, setError: SetError, clearIsDirty: ClearIsDirty) => void;
|
|
184
|
-
/**
|
|
185
|
-
*
|
|
186
|
-
* This method only validate the specified path. Do not check for fields that have the
|
|
187
|
-
* live validation active.
|
|
188
|
-
*/
|
|
189
|
-
export type Validate<Values extends InitialValues> = (path: string, options?: ValidateFnOptions<Values>) => void;
|
|
190
|
-
/**
|
|
191
|
-
*
|
|
192
|
-
* This method only validate the specified paths. Do not check for fields that have the
|
|
193
|
-
* live validation active.
|
|
194
|
-
*/
|
|
195
|
-
export type ValidateAll<Values extends InitialValues> = (paths: string[], options?: ValidateFnOptions<Values>) => void;
|
|
196
|
-
/**
|
|
197
|
-
* This method validates the entire form and set the corresponding errors if any.
|
|
198
|
-
*
|
|
199
|
-
*/
|
|
200
|
-
export type ValidateForm<Values extends InitialValues> = (successCallback?: SuccessCallback<Values>, errorCallback?: ErrorCallback<Values>, options?: ValidateOptions) => void;
|
|
201
|
-
export type ValidationFormbitError = Pick<ValidationError, 'message' | 'path'>;
|
|
202
|
-
/**
|
|
203
|
-
* Type imported from the yup library.
|
|
204
|
-
* It represents the object with all the options that can be passed to the internal yup validation method,
|
|
205
|
-
*
|
|
206
|
-
* Link to the Yup documentation {@link https://github.com/jquense/yup}
|
|
207
|
-
*
|
|
208
|
-
*/
|
|
209
|
-
export type ValidateOptions = YupValidateOptions;
|
|
210
|
-
/**
|
|
211
|
-
*
|
|
212
|
-
* This method update the form state writing $value into the $path, setting isDirty to true.
|
|
213
|
-
*
|
|
214
|
-
* After writing, it validates all the paths contained into $pathsToValidate (if any)
|
|
215
|
-
* and all the fields that have the live validation active.
|
|
32
|
+
* The whole internal state of the form (everything except the validation schema).
|
|
216
33
|
*/
|
|
217
|
-
export type
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
* all those values into the specified paths.
|
|
221
|
-
*
|
|
222
|
-
* It set isDirty to true.
|
|
223
|
-
*
|
|
224
|
-
* After writing, it validate all the paths contained into $pathToValidate and all
|
|
225
|
-
* the fields that have the live validation active.
|
|
226
|
-
*
|
|
227
|
-
*/
|
|
228
|
-
export type WriteAll<Values extends InitialValues> = (arr: WriteAllValue<Values>[], options?: WriteFnOptions<Values>) => void;
|
|
229
|
-
/**
|
|
230
|
-
*
|
|
231
|
-
* This method updates the form state deleting multiple values, setting isDirty to true.
|
|
232
|
-
*
|
|
233
|
-
*/
|
|
234
|
-
export type RemoveAll<Values extends InitialValues> = (arr: string[], options?: WriteFnOptions<Values>) => void;
|
|
235
|
-
/**
|
|
236
|
-
* Tuple of [key, value] pair.
|
|
237
|
-
*
|
|
238
|
-
*/
|
|
239
|
-
export type WriteAllValue<Values extends InitialValues> = [keyof Values | string, unknown];
|
|
240
|
-
/**
|
|
241
|
-
* @private
|
|
242
|
-
*/
|
|
243
|
-
export type WriteOrRemove<Values extends Form> = (path: keyof Values | string, value: unknown, options?: WriteFnOptions<Values>, action?: Action) => void;
|
|
244
|
-
/**
|
|
245
|
-
* Internal form state storing all the data of the form (except the validation schema)
|
|
246
|
-
*
|
|
247
|
-
*/
|
|
248
|
-
export type Writer<Values extends InitialValues> = {
|
|
249
|
-
form: Values;
|
|
250
|
-
initialValues: Values;
|
|
34
|
+
export type FormState<T extends FormbitValues> = {
|
|
35
|
+
form: T;
|
|
36
|
+
initialValues: T;
|
|
251
37
|
errors: Errors;
|
|
252
38
|
liveValidation: LiveValidation;
|
|
253
|
-
isDirty:
|
|
39
|
+
isDirty: boolean;
|
|
254
40
|
};
|
|
255
|
-
/**
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
41
|
+
/** A validation schema built with `yup.object()`. See {@link https://github.com/jquense/yup}. */
|
|
42
|
+
export type ValidationSchema<T extends FormbitValues> = ObjectSchema<T>;
|
|
43
|
+
/** Options forwarded to yup's validation methods. See {@link https://github.com/jquense/yup}. */
|
|
44
|
+
export type ValidateOptions = YupValidateOptions;
|
|
45
|
+
/** The error object yup throws when a validation fails. See {@link https://github.com/jquense/yup}. */
|
|
46
|
+
export type ValidationError = YupValidationError;
|
|
47
|
+
/** Invoked by validation methods when the form (or the validated paths) are valid. */
|
|
48
|
+
export type SuccessCallback<T extends FormbitValues> = (writer: FormState<T>, setError: SetError) => void;
|
|
49
|
+
/** Invoked by validation methods when validation fails. */
|
|
50
|
+
export type ErrorCallback<T extends FormbitValues> = (writer: FormState<T>, setError: SetError) => void;
|
|
51
|
+
/** Invoked by `check()` when the given json is valid. */
|
|
52
|
+
export type CheckSuccessCallback<T extends FormbitValues> = (json: FormbitValues, writer: FormState<T>, setError: SetError) => void;
|
|
53
|
+
/** Invoked by `check()` when the given json is invalid. */
|
|
54
|
+
export type CheckErrorCallback<T extends FormbitValues> = (json: FormbitValues, inner: ValidationError[], writer: FormState<T>, setError: SetError) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Invoked by `submitForm()` once the whole form is valid — the place to send data
|
|
57
|
+
* to the backend. `__metadata` is stripped from `writer.form` before this runs.
|
|
58
|
+
*/
|
|
59
|
+
export type SubmitSuccessCallback<T extends FormbitValues> = (writer: FormState<Omit<T, '__metadata'>>, setError: SetError, clearIsDirty: () => void) => void;
|
|
60
|
+
/** See {@link FormbitObject.check}. */
|
|
61
|
+
export type Check<T extends FormbitValues> = (json: FormbitValues, options?: CheckFnOptions<T>) => ValidationError[] | undefined;
|
|
62
|
+
/** See {@link FormbitObject.initialize}. */
|
|
63
|
+
export type Initialize<T extends FormbitValues> = (values: Partial<T>) => void;
|
|
64
|
+
/** See {@link FormbitObject.remove}. */
|
|
65
|
+
export type Remove<T extends FormbitValues> = (path: string, options?: WriteFnOptions<T>) => void;
|
|
66
|
+
/** See {@link FormbitObject.setError}. */
|
|
67
|
+
export type SetError = (path: string, value: string) => void;
|
|
68
|
+
/** See {@link FormbitObject.setSchema}. */
|
|
69
|
+
export type SetSchema<T extends FormbitValues> = (newSchema: ValidationSchema<T>) => void;
|
|
70
|
+
/** A single `[path, value]` pair accepted by `writeAll`. */
|
|
71
|
+
export type WriteAllValue<T extends FormbitValues> = [keyof T | string, unknown];
|
|
72
|
+
/** See {@link FormbitObject.write}. */
|
|
73
|
+
export type Write<T extends FormbitValues> = (path: keyof T | string, value: unknown, options?: WriteFnOptions<T>) => void;
|
|
74
|
+
/** See {@link FormbitObject.writeAll}. */
|
|
75
|
+
export type WriteAll<T extends FormbitValues> = (arr: WriteAllValue<T>[], options?: WriteFnOptions<T>) => void;
|
|
76
|
+
/** See {@link FormbitObject.removeAll}. */
|
|
77
|
+
export type RemoveAll<T extends FormbitValues> = (arr: string[], options?: WriteFnOptions<T>) => void;
|
|
78
|
+
/** See {@link FormbitObject.validate}. */
|
|
79
|
+
export type Validate<T extends FormbitValues> = (path: string, options?: ValidateFnOptions<T>) => void;
|
|
80
|
+
/** See {@link FormbitObject.validateAll}. */
|
|
81
|
+
export type ValidateAll<T extends FormbitValues> = (paths: string[], options?: ValidateFnOptions<T>) => void;
|
|
82
|
+
/** See {@link FormbitObject.validateForm}. */
|
|
83
|
+
export type ValidateForm<T extends FormbitValues> = (successCallback?: SuccessCallback<T>, errorCallback?: ErrorCallback<T>, options?: ValidateOptions) => void;
|
|
84
|
+
/** See {@link FormbitObject.submitForm}. */
|
|
85
|
+
export type SubmitForm<T extends FormbitValues> = (successCallback: SubmitSuccessCallback<T>, errorCallback?: ErrorCallback<Partial<T>>, options?: ValidateOptions) => void;
|
|
86
|
+
/** Options accepted by `check()`. */
|
|
87
|
+
export type CheckFnOptions<T extends FormbitValues> = {
|
|
88
|
+
successCallback?: CheckSuccessCallback<T>;
|
|
89
|
+
errorCallback?: CheckErrorCallback<T>;
|
|
262
90
|
options?: ValidateOptions;
|
|
263
91
|
};
|
|
264
|
-
/**
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
export type ValidateFnOptions<Values extends InitialValues> = {
|
|
269
|
-
successCallback?: SuccessCallback<Partial<Values>>;
|
|
270
|
-
errorCallback?: ErrorCallback<Partial<Values>>;
|
|
92
|
+
/** Options accepted by the `validate` methods. */
|
|
93
|
+
export type ValidateFnOptions<T extends FormbitValues> = {
|
|
94
|
+
successCallback?: SuccessCallback<Partial<T>>;
|
|
95
|
+
errorCallback?: ErrorCallback<Partial<T>>;
|
|
271
96
|
options?: ValidateOptions;
|
|
272
97
|
};
|
|
273
|
-
/**
|
|
274
|
-
|
|
275
|
-
* It represents the error object returned when a validation fails
|
|
276
|
-
*
|
|
277
|
-
* Link to the Yup documentation {@link https://github.com/jquense/yup}
|
|
278
|
-
*
|
|
279
|
-
*/
|
|
280
|
-
export type ValidationError = YupValidationError;
|
|
281
|
-
/**
|
|
282
|
-
* Options object to change the behavior of the write methods
|
|
283
|
-
*
|
|
284
|
-
*/
|
|
285
|
-
export type WriteFnOptions<Values extends InitialValues> = {
|
|
98
|
+
/** Options accepted by the `write`/`remove` methods (validate options plus path control). */
|
|
99
|
+
export type WriteFnOptions<T extends FormbitValues> = {
|
|
286
100
|
noLiveValidation?: boolean;
|
|
287
101
|
pathsToValidate?: string[];
|
|
288
|
-
} & ValidateFnOptions<
|
|
102
|
+
} & ValidateFnOptions<T>;
|
|
103
|
+
/** @internal */
|
|
104
|
+
export type Action = keyof typeof ACTIONS;
|
|
105
|
+
/** @internal */
|
|
106
|
+
export type GenericCallback<T extends FormbitValues> = SuccessCallback<T> | ErrorCallback<T>;
|
|
107
|
+
/** @internal Subset of a yup ValidationError kept by formbit's sync validation. */
|
|
108
|
+
export type ValidationFormbitError = Pick<ValidationError, 'message' | 'path'>;
|
|
109
|
+
/** @internal */
|
|
110
|
+
export type WriteOrRemove<T extends FormbitValues> = (path: keyof T | string, value: unknown, options?: WriteFnOptions<T>, action?: Action) => void;
|
|
111
|
+
/** @internal */
|
|
112
|
+
export type PrivateValidateForm<T extends FormbitValues> = (successCallback?: SuccessCallback<T>, errorCallback?: ErrorCallback<Partial<T>>, options?: {
|
|
113
|
+
options?: ValidateOptions;
|
|
114
|
+
}) => void;
|
|
289
115
|
/**
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
116
|
+
* The object returned by `useFormbit()` and `useFormbitContext()`. Holds the form
|
|
117
|
+
* state and every method needed to read, mutate and validate the form.
|
|
293
118
|
*/
|
|
294
|
-
export type FormbitObject<
|
|
295
|
-
/**
|
|
296
|
-
|
|
297
|
-
* It returns undefined if the json is valid.
|
|
298
|
-
*
|
|
299
|
-
*/
|
|
300
|
-
check: Check<Partial<Values>>;
|
|
119
|
+
export type FormbitObject<T extends FormbitValues> = {
|
|
120
|
+
/** The current form values. Partial: fields may be missing until validated. */
|
|
121
|
+
form: Partial<T>;
|
|
301
122
|
/**
|
|
302
|
-
*
|
|
303
|
-
* It doesn't trigger any validation
|
|
304
|
-
*
|
|
305
|
-
*/
|
|
306
|
-
error: ErrorFn;
|
|
307
|
-
/**
|
|
308
|
-
* Object including all the registered errors messages since the last validation.
|
|
309
|
-
* Errors are stored using the same path of the corresponding form values.
|
|
123
|
+
* Error messages registered since the last validation, keyed by the value's dot-path.
|
|
310
124
|
*
|
|
311
125
|
* @example
|
|
312
|
-
*
|
|
313
|
-
|
|
314
|
-
* {
|
|
315
|
-
* "age": 1
|
|
316
|
-
* }
|
|
317
|
-
* ```
|
|
318
|
-
* and age is a non valid field, errors object will look like this
|
|
319
|
-
* ```json
|
|
320
|
-
* {
|
|
321
|
-
* "age": "Age must be greater then 18"
|
|
322
|
-
* }
|
|
323
|
-
* ```
|
|
324
|
-
*
|
|
325
|
-
*/
|
|
326
|
-
errors: Errors;
|
|
327
|
-
/**
|
|
328
|
-
* Object containing the updated form
|
|
329
|
-
*/
|
|
330
|
-
form: Partial<Values>;
|
|
331
|
-
/**
|
|
332
|
-
* Initialize the form with new initial values
|
|
333
|
-
*/
|
|
334
|
-
initialize: Initialize<Values>;
|
|
335
|
-
/**
|
|
336
|
-
* Returns true if the form is Dirty (user already interacted with the form), false otherwise.
|
|
337
|
-
*
|
|
126
|
+
* form: { age: 1 }
|
|
127
|
+
* errors: { age: "Age must be greater than 18" }
|
|
338
128
|
*/
|
|
129
|
+
errors: Errors;
|
|
130
|
+
/** True once the user has interacted with the form. */
|
|
339
131
|
isDirty: boolean;
|
|
132
|
+
/** Returns the error message registered for `path`, if any. */
|
|
133
|
+
error: (path: string) => string | undefined;
|
|
134
|
+
/** True if no errors are currently registered. Does not run validation. */
|
|
135
|
+
isFormValid: () => boolean;
|
|
136
|
+
/** True if any error is currently registered. Does not run validation. */
|
|
137
|
+
isFormInvalid: () => boolean;
|
|
138
|
+
/** True if live-validation is active for `path`. */
|
|
139
|
+
liveValidation: (path: string) => true | undefined;
|
|
140
|
+
/** Validates `json` against the current schema; returns the errors, or undefined if valid. */
|
|
141
|
+
check: Check<Partial<T>>;
|
|
340
142
|
/**
|
|
341
|
-
*
|
|
342
|
-
*
|
|
143
|
+
* Writes `value` at `path`, sets `isDirty`, then validates `pathsToValidate`
|
|
144
|
+
* plus every live-validated field.
|
|
343
145
|
*/
|
|
344
|
-
|
|
146
|
+
write: Write<T>;
|
|
345
147
|
/**
|
|
346
|
-
*
|
|
347
|
-
*
|
|
148
|
+
* Writes every `[path, value]` pair, sets `isDirty`, then validates
|
|
149
|
+
* `pathsToValidate` plus every live-validated field.
|
|
348
150
|
*/
|
|
349
|
-
|
|
151
|
+
writeAll: WriteAll<T>;
|
|
350
152
|
/**
|
|
351
|
-
*
|
|
153
|
+
* Removes the value at `path`, sets `isDirty`, then validates `pathsToValidate`
|
|
154
|
+
* plus every live-validated field.
|
|
352
155
|
*/
|
|
353
|
-
|
|
156
|
+
remove: Remove<T>;
|
|
354
157
|
/**
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
* After writing, it validates all the paths contained into pathsToValidate (if any)
|
|
359
|
-
* and all the fields that have the live validation active.
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
*/
|
|
363
|
-
remove: Remove<Values>;
|
|
364
|
-
/**
|
|
365
|
-
* Reset form to the initial state.
|
|
366
|
-
* Errors and liveValidation are set back to empty objects.
|
|
367
|
-
* isDirty is set back to false
|
|
368
|
-
*
|
|
369
|
-
*/
|
|
370
|
-
resetForm: ResetForm;
|
|
371
|
-
/**
|
|
372
|
-
* Set a message(value) to the given error path.
|
|
373
|
-
*
|
|
158
|
+
* Removes every given path, sets `isDirty`, then validates `pathsToValidate`
|
|
159
|
+
* plus every live-validated field.
|
|
374
160
|
*/
|
|
161
|
+
removeAll: RemoveAll<T>;
|
|
162
|
+
/** Re-initializes the form with new initial values. */
|
|
163
|
+
initialize: Initialize<T>;
|
|
164
|
+
/** Resets form, errors, liveValidation and isDirty back to their initial state. */
|
|
165
|
+
resetForm: () => void;
|
|
166
|
+
/** Sets the error message at `path`. */
|
|
375
167
|
setError: SetError;
|
|
376
|
-
/**
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
submitForm: SubmitForm<Values>;
|
|
387
|
-
/**
|
|
388
|
-
*
|
|
389
|
-
* This method only validate the specified path. Do not check for fields that have the
|
|
390
|
-
* live validation active.
|
|
391
|
-
*/
|
|
392
|
-
validate: Validate<Values>;
|
|
393
|
-
/**
|
|
394
|
-
*
|
|
395
|
-
* This method only validate the specified paths. Do not check for fields that have the
|
|
396
|
-
* live validation active.
|
|
397
|
-
*/
|
|
398
|
-
validateAll: ValidateAll<Values>;
|
|
399
|
-
/**
|
|
400
|
-
* This method validates the entire form and set the corresponding errors if any.
|
|
401
|
-
*
|
|
402
|
-
*/
|
|
403
|
-
validateForm: ValidateForm<Partial<Values>>;
|
|
404
|
-
/**
|
|
405
|
-
*
|
|
406
|
-
* This method update the form state writing $value into the $path, setting isDirty to true.
|
|
407
|
-
*
|
|
408
|
-
* After writing, it validates all the paths contained into $pathsToValidate (if any)
|
|
409
|
-
* and all the fields that have the live validation active.
|
|
410
|
-
*/
|
|
411
|
-
write: Write<Values>;
|
|
412
|
-
/**
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
* This method takes an array of [path, value] and update the form state writing
|
|
416
|
-
* all those values into the specified paths.
|
|
417
|
-
*
|
|
418
|
-
* It set isDirty to true.
|
|
419
|
-
*
|
|
420
|
-
* After writing, it validate all the paths contained into $pathToValidate and all
|
|
421
|
-
* the fields that have the live validation active.
|
|
422
|
-
*/
|
|
423
|
-
writeAll: WriteAll<Values>;
|
|
424
|
-
/**
|
|
425
|
-
*
|
|
426
|
-
* This method updates the form state deleting multiple values, setting isDirty to true.
|
|
427
|
-
*
|
|
428
|
-
*/
|
|
429
|
-
removeAll: RemoveAll<Values>;
|
|
168
|
+
/** Replaces the current validation schema. */
|
|
169
|
+
setSchema: SetSchema<T>;
|
|
170
|
+
/** Validates only `path` (ignores live-validated fields). */
|
|
171
|
+
validate: Validate<T>;
|
|
172
|
+
/** Validates only the given `paths` (ignores live-validated fields). */
|
|
173
|
+
validateAll: ValidateAll<T>;
|
|
174
|
+
/** Validates the whole form and registers any error. */
|
|
175
|
+
validateForm: ValidateForm<Partial<T>>;
|
|
176
|
+
/** Validates the whole form and, if valid, runs the success callback to submit. */
|
|
177
|
+
submitForm: SubmitForm<T>;
|
|
430
178
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GenericCallback,
|
|
1
|
+
import { FormState, GenericCallback, FormbitValues, SetError } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* useExecuteCallbacks
|
|
4
4
|
*
|
|
@@ -7,5 +7,5 @@ import { GenericCallback, InitialValues, SetError, Writer } from './types';
|
|
|
7
7
|
*
|
|
8
8
|
*
|
|
9
9
|
*/
|
|
10
|
-
declare const _default: <
|
|
10
|
+
declare const _default: <T extends FormbitValues>(writer: FormState<T>, setError: SetError) => (uuid: string, cb?: GenericCallback<Partial<T>>) => void;
|
|
11
11
|
export default _default;
|
package/dist/use-formbit.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { FormbitObject,
|
|
2
|
-
type UseFormbitParams<
|
|
3
|
-
initialValues?: Partial<
|
|
4
|
-
yup: ValidationSchema<
|
|
1
|
+
import { FormbitObject, FormbitValues, ValidationSchema } from './types';
|
|
2
|
+
type UseFormbitParams<T extends FormbitValues> = {
|
|
3
|
+
initialValues?: Partial<T>;
|
|
4
|
+
yup: ValidationSchema<T>;
|
|
5
5
|
};
|
|
6
|
-
declare const _default: <
|
|
6
|
+
declare const _default: <T extends FormbitValues>({ initialValues, yup: schema }: UseFormbitParams<T>) => FormbitObject<T>;
|
|
7
7
|
export default _default;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const validateSyncAll: <
|
|
1
|
+
import { FormbitValues, ValidateOptions, ValidationFormbitError, ValidationSchema } from './types';
|
|
2
|
+
export declare const validateSyncAll: <T extends FormbitValues>(paths: string[], schema: ValidationSchema<T>, form: FormbitValues, options?: ValidateOptions) => ValidationFormbitError[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radicalbit/formbit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Lightweight state form library",
|
|
5
5
|
"author": "https://github.com/radicalbit",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"predeploy": "cd example && yarn install && yarn run build",
|
|
26
26
|
"deploy": "gh-pages -d example/build",
|
|
27
27
|
"yalc:publish": "yalc publish --push --sig",
|
|
28
|
-
"docs:gen": "typedoc --excludePrivate --plugin typedoc-plugin-markdown --hideInPageTOC true --hideBreadcrumbs true --hidePageTitle true --out docs src/types/index.ts",
|
|
28
|
+
"docs:gen": "typedoc --excludePrivate --excludeInternal --plugin typedoc-plugin-markdown --hideInPageTOC true --hideBreadcrumbs true --hidePageTitle true --out docs src/types/index.ts",
|
|
29
29
|
"docs:merge": "node scripts/readme-merge.js",
|
|
30
30
|
"docs:toc": "doctoc --maxlevel 3 README.md --github",
|
|
31
31
|
"docs:clean": "rimraf docs",
|
|
@@ -51,11 +51,12 @@
|
|
|
51
51
|
"@typescript-eslint/parser": "^7.4.0",
|
|
52
52
|
"cross-env": "^7.0.2",
|
|
53
53
|
"doctoc": "^2.2.1",
|
|
54
|
-
"eslint": "
|
|
54
|
+
"eslint": "8.57.0",
|
|
55
55
|
"eslint-config-prettier": "^9.1.0",
|
|
56
56
|
"eslint-config-react-app": "^7.0.1",
|
|
57
57
|
"eslint-config-standard": "^17.1.0",
|
|
58
58
|
"eslint-config-standard-react": "^13.0.0",
|
|
59
|
+
"eslint-plugin-cypress": "^2.15.0",
|
|
59
60
|
"eslint-plugin-import": "^2.29.1",
|
|
60
61
|
"eslint-plugin-jest": "^27.9.0",
|
|
61
62
|
"eslint-plugin-n": "^16.6.2",
|