@radicalbit/formbit 2.1.0 → 4.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.
Files changed (33) hide show
  1. package/README.md +150 -400
  2. package/dist/formbit-context.d.ts +4 -4
  3. package/dist/index.d.ts +1 -1
  4. package/dist/index.js +142 -269
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.modern.js +142 -269
  7. package/dist/index.modern.js.map +1 -1
  8. package/dist/use-execute-callbacks.d.ts +2 -2
  9. package/dist/use-formbit.d.ts +5 -5
  10. package/dist/validate-sync-all.d.ts +2 -2
  11. package/package.json +32 -49
  12. package/dist/__tests__/check.test.d.ts +0 -1
  13. package/dist/__tests__/error.test.d.ts +0 -1
  14. package/dist/__tests__/initialize.test.d.ts +0 -1
  15. package/dist/__tests__/is-form-invalid.test.d.ts +0 -1
  16. package/dist/__tests__/is-form-valid.test.d.ts +0 -1
  17. package/dist/__tests__/live-validation.test.d.ts +0 -1
  18. package/dist/__tests__/miscellaneous.d.ts +0 -1
  19. package/dist/__tests__/remove-all.test.d.ts +0 -1
  20. package/dist/__tests__/remove.test.d.ts +0 -1
  21. package/dist/__tests__/reset-form.test.d.ts +0 -1
  22. package/dist/__tests__/set-error.test.d.ts +0 -1
  23. package/dist/__tests__/set-schema.test.d.ts +0 -1
  24. package/dist/__tests__/submit-form.test.d.ts +0 -1
  25. package/dist/__tests__/use-execute-callbacks.test.d.ts +0 -1
  26. package/dist/__tests__/use-formbit-context.test.d.ts +0 -1
  27. package/dist/__tests__/validate-all.test.d.ts +0 -1
  28. package/dist/__tests__/validate-form.test.d.ts +0 -1
  29. package/dist/__tests__/validate-sync-all.test.d.ts +0 -1
  30. package/dist/__tests__/validate.test.d.ts +0 -1
  31. package/dist/__tests__/write-all.test.d.ts +0 -1
  32. package/dist/__tests__/write.test.d.ts +0 -1
  33. package/dist/types/index.d.ts +0 -333
@@ -1,333 +0,0 @@
1
- import { ObjectSchema, ValidationError as YupValidationError, ValidateOptions as YupValidateOptions } from 'yup';
2
- import { ACTIONS } from '../helpers/constants';
3
- /**
4
- * @internal
5
- * @private
6
- */
7
- export type Action = keyof typeof ACTIONS;
8
- /**
9
- * @internal
10
- * Generic object with string keys.
11
- */
12
- export type FormbitRecord = Record<string, unknown>;
13
- /** @deprecated Use {@link FormbitRecord} instead. Renamed to avoid shadowing the global `Object`. */
14
- export type Object = FormbitRecord;
15
- /**
16
- * @internal
17
- */
18
- export type GenericCallback<Values extends InitialValues> = SuccessCallback<Values> | ErrorCallback<Values>;
19
- /**
20
- * @internal
21
- */
22
- export type ValidationFormbitError = Pick<ValidationError, 'message' | 'path'>;
23
- /**
24
- * @internal
25
- * @private
26
- */
27
- export type WriteOrRemove<Values extends Form> = (path: keyof Values | string, value: unknown, options?: WriteFnOptions<Values>, action?: Action) => void;
28
- /**
29
- * @internal
30
- * @private
31
- */
32
- export type PrivateValidateForm<Values extends InitialValues> = (successCallback?: SuccessCallback<Values>, errorCallback?: ErrorCallback<Partial<Values>>, options?: {
33
- options?: ValidateOptions;
34
- isDirty?: boolean;
35
- }) => void;
36
- /**
37
- * Base type for form values: a record of string keys with an optional `__metadata` field.
38
- */
39
- export type FormbitValues = {
40
- __metadata?: FormbitRecord;
41
- } & FormbitRecord;
42
- /** Object containing the updated form. */
43
- export type Form = FormbitValues;
44
- /** InitialValues used to set up formbit; also used to reset the form to its original version. */
45
- export type InitialValues = FormbitValues;
46
- /**
47
- * Object including all the registered error messages since the last validation.
48
- * Errors are stored using the same path of the corresponding form values.
49
- *
50
- * @example
51
- * If the form object has this structure:
52
- * ```json
53
- * {
54
- * "age": 1
55
- * }
56
- * ```
57
- * and age is a non valid field, errors object will look like this
58
- * ```json
59
- * {
60
- * "age": "Age must be greater then 18"
61
- * }
62
- * ```
63
- */
64
- export type Errors = Record<string, string>;
65
- /**
66
- * Object including all the values that are being live validated.
67
- * Usually fields that fail validation (using one of the methods that triggers validation)
68
- * will automatically be set to live-validated.
69
- *
70
- * A value/path is live-validated when validated at every change of the form.
71
- *
72
- * By default no field is live-validated.
73
- *
74
- * @example
75
- * If the form object has this structure:
76
- * ```json
77
- * {
78
- * "age": 1
79
- * }
80
- * ```
81
- * and age is a field that is being live-validated, liveValidation object will look like this
82
- * ```json
83
- * {
84
- * "age": true
85
- * }
86
- * ```
87
- */
88
- export type LiveValidation = Record<string, true>;
89
- /**
90
- * Internal form state storing all the data of the form (except the validation schema).
91
- */
92
- export type FormState<Values extends InitialValues> = {
93
- form: Values;
94
- initialValues: Values;
95
- errors: Errors;
96
- liveValidation: LiveValidation;
97
- isDirty: boolean;
98
- };
99
- /** @deprecated Use {@link FormState} instead. */
100
- export type Writer<Values extends InitialValues> = FormState<Values>;
101
- /**
102
- * Type imported from the yup library.
103
- * It represents any validation schema created with the yup.object() method.
104
- *
105
- * Link to the Yup documentation {@link https://github.com/jquense/yup}
106
- */
107
- export type ValidationSchema<Values extends InitialValues> = ObjectSchema<Values>;
108
- /**
109
- * Type imported from the yup library.
110
- * It represents the object with all the options that can be passed to the internal yup validation method.
111
- *
112
- * Link to the Yup documentation {@link https://github.com/jquense/yup}
113
- */
114
- export type ValidateOptions = YupValidateOptions;
115
- /**
116
- * Type imported from the yup library.
117
- * It represents the error object returned when a validation fails.
118
- *
119
- * Link to the Yup documentation {@link https://github.com/jquense/yup}
120
- */
121
- export type ValidationError = YupValidationError;
122
- /**
123
- * Success callback invoked by some formbit methods when the operation is successful.
124
- */
125
- export type SuccessCallback<Values extends InitialValues> = (writer: FormState<Values>, setError: SetError) => void;
126
- /**
127
- * Invoked in case of errors raised by validation.
128
- */
129
- export type ErrorCallback<Values extends InitialValues> = (writer: FormState<Values>, setError: SetError) => void;
130
- /**
131
- * Success callback invoked by the check method when the operation is successful.
132
- */
133
- export type CheckSuccessCallback<Values extends InitialValues> = (json: Form, writer: FormState<Values>, setError: SetError) => void;
134
- /** @deprecated Use {@link CheckSuccessCallback} instead. */
135
- export type SuccessCheckCallback<Values extends InitialValues> = CheckSuccessCallback<Values>;
136
- /**
137
- * Invoked in case of errors raised by validation of check method.
138
- */
139
- export type CheckErrorCallback<Values extends InitialValues> = (json: Form, inner: ValidationError[], writer: FormState<Values>, setError: SetError) => void;
140
- /** @deprecated Use {@link CheckErrorCallback} instead. */
141
- export type ErrorCheckCallback<Values extends InitialValues> = CheckErrorCallback<Values>;
142
- /**
143
- * Success callback invoked by the submit method when the validation is successful.
144
- * Is the right place to send your data to the backend.
145
- */
146
- export type SubmitSuccessCallback<Values extends InitialValues> = (writer: FormState<Values | Omit<Values, '__metadata'>>, setError: SetError, clearIsDirty: () => void) => void;
147
- /** @deprecated Inlined into {@link FormbitObject}. */
148
- export type ErrorFn = (path: string) => string | undefined;
149
- /** @deprecated Inlined into {@link FormbitObject}. */
150
- export type IsFormValid = () => boolean;
151
- /** @deprecated Inlined into {@link FormbitObject}. */
152
- export type IsFormInvalid = () => boolean;
153
- /** @deprecated Inlined into {@link FormbitObject}. */
154
- export type ClearIsDirty = () => void;
155
- /** @deprecated Inlined into {@link FormbitObject}. */
156
- export type ResetForm = () => void;
157
- /** @deprecated Inlined into {@link FormbitObject}. */
158
- export type LiveValidationFn = (path: string) => true | undefined;
159
- /** @deprecated Inlined into {@link FormbitObject}. */
160
- export type IsDirty = boolean;
161
- /** See {@link FormbitObject.check}. */
162
- export type Check<Values extends InitialValues> = (json: Form, options?: CheckFnOptions<Values>) => ValidationError[] | undefined;
163
- /** See {@link FormbitObject.initialize}. */
164
- export type Initialize<Values extends InitialValues> = (values: Partial<Values>) => void;
165
- /** See {@link FormbitObject.remove}. */
166
- export type Remove<Values extends InitialValues> = (path: string, options?: WriteFnOptions<Values>) => void;
167
- /** See {@link FormbitObject.setError}. */
168
- export type SetError = (path: string, value: string) => void;
169
- /** See {@link FormbitObject.setSchema}. */
170
- export type SetSchema<Values extends InitialValues> = (newSchema: ValidationSchema<Values>) => void;
171
- /** See {@link FormbitObject.submitForm}. */
172
- export type SubmitForm<Values extends InitialValues> = (successCallback: SubmitSuccessCallback<Values>, errorCallback?: ErrorCallback<Partial<Values>>, options?: ValidateOptions) => void;
173
- /** See {@link FormbitObject.validate}. */
174
- export type Validate<Values extends InitialValues> = (path: string, options?: ValidateFnOptions<Values>) => void;
175
- /** See {@link FormbitObject.validateAll}. */
176
- export type ValidateAll<Values extends InitialValues> = (paths: string[], options?: ValidateFnOptions<Values>) => void;
177
- /** See {@link FormbitObject.validateForm}. */
178
- export type ValidateForm<Values extends InitialValues> = (successCallback?: SuccessCallback<Values>, errorCallback?: ErrorCallback<Values>, options?: ValidateOptions) => void;
179
- /** See {@link FormbitObject.write}. */
180
- export type Write<Values extends InitialValues> = (path: keyof Values | string, value: unknown, options?: WriteFnOptions<Values>) => void;
181
- /** See {@link FormbitObject.writeAll}. */
182
- export type WriteAll<Values extends InitialValues> = (arr: WriteAllValue<Values>[], options?: WriteFnOptions<Values>) => void;
183
- /** See {@link FormbitObject.removeAll}. */
184
- export type RemoveAll<Values extends InitialValues> = (arr: string[], options?: WriteFnOptions<Values>) => void;
185
- /**
186
- * Tuple of [key, value] pair.
187
- */
188
- export type WriteAllValue<Values extends InitialValues> = [keyof Values | string, unknown];
189
- /**
190
- * Options object to change the behavior of the check method.
191
- */
192
- export type CheckFnOptions<Values extends InitialValues> = {
193
- successCallback?: CheckSuccessCallback<Values>;
194
- errorCallback?: CheckErrorCallback<Values>;
195
- options?: ValidateOptions;
196
- };
197
- /**
198
- * Options object to change the behavior of the validate methods.
199
- */
200
- export type ValidateFnOptions<Values extends InitialValues> = {
201
- successCallback?: SuccessCallback<Partial<Values>>;
202
- errorCallback?: ErrorCallback<Partial<Values>>;
203
- options?: ValidateOptions;
204
- };
205
- /**
206
- * Options object to change the behavior of the write methods.
207
- */
208
- export type WriteFnOptions<Values extends InitialValues> = {
209
- noLiveValidation?: boolean;
210
- pathsToValidate?: string[];
211
- } & ValidateFnOptions<Values>;
212
- /**
213
- * Object returned by useFormbit() and useFormbitContextHook().
214
- * It contains all the data and methods needed to handle the form.
215
- */
216
- export type FormbitObject<Values extends InitialValues> = {
217
- /**
218
- * Object containing the updated form.
219
- */
220
- form: Partial<Values>;
221
- /**
222
- * Object including all the registered error messages since the last validation.
223
- * Errors are stored using the same path of the corresponding form values.
224
- *
225
- * @example
226
- * If the form object has this structure:
227
- * ```json
228
- * {
229
- * "age": 1
230
- * }
231
- * ```
232
- * and age is a non valid field, errors object will look like this
233
- * ```json
234
- * {
235
- * "age": "Age must be greater then 18"
236
- * }
237
- * ```
238
- */
239
- errors: Errors;
240
- /**
241
- * Returns true if the form is Dirty (user already interacted with the form), false otherwise.
242
- */
243
- isDirty: boolean;
244
- /**
245
- * Returns the error message for the given path if any.
246
- * It doesn't trigger any validation.
247
- */
248
- error: (path: string) => string | undefined;
249
- /**
250
- * Returns true if the form is valid.
251
- * It doesn't perform any validation, it checks if any errors are present.
252
- */
253
- isFormValid: () => boolean;
254
- /**
255
- * Returns true if the form is NOT valid.
256
- * It doesn't perform any validation, it checks if any errors are present.
257
- */
258
- isFormInvalid: () => boolean;
259
- /**
260
- * Returns true if live validation is active for the given path.
261
- */
262
- liveValidation: (path: string) => true | undefined;
263
- /**
264
- * Checks the given json against the form schema and returns an array of errors.
265
- * It returns undefined if the json is valid.
266
- */
267
- check: Check<Partial<Values>>;
268
- /**
269
- * This method updates the form state writing $value into the $path, setting isDirty to true.
270
- *
271
- * After writing, it validates all the paths contained into $pathsToValidate (if any)
272
- * and all the fields that have the live validation active.
273
- */
274
- write: Write<Values>;
275
- /**
276
- * This method takes an array of [path, value] and updates the form state writing
277
- * all those values into the specified paths.
278
- *
279
- * It sets isDirty to true.
280
- *
281
- * After writing, it validates all the paths contained into $pathToValidate and all
282
- * the fields that have the live validation active.
283
- */
284
- writeAll: WriteAll<Values>;
285
- /**
286
- * This method updates the form state deleting value, setting isDirty to true.
287
- *
288
- * After writing, it validates all the paths contained into pathsToValidate (if any)
289
- * and all the fields that have the live validation active.
290
- */
291
- remove: Remove<Values>;
292
- /**
293
- * This method updates the form state deleting multiple values, setting isDirty to true.
294
- */
295
- removeAll: RemoveAll<Values>;
296
- /**
297
- * Initialize the form with new initial values.
298
- */
299
- initialize: Initialize<Values>;
300
- /**
301
- * Reset form to the initial state.
302
- * Errors and liveValidation are set back to empty objects.
303
- * isDirty is set back to false.
304
- */
305
- resetForm: () => void;
306
- /**
307
- * Set a message (value) to the given error path.
308
- */
309
- setError: SetError;
310
- /**
311
- * Override the current schema with the given one.
312
- */
313
- setSchema: SetSchema<Values>;
314
- /**
315
- * This method only validates the specified path. Does not check for fields that have the
316
- * live validation active.
317
- */
318
- validate: Validate<Values>;
319
- /**
320
- * This method only validates the specified paths. Does not check for fields that have the
321
- * live validation active.
322
- */
323
- validateAll: ValidateAll<Values>;
324
- /**
325
- * This method validates the entire form and sets the corresponding errors if any.
326
- */
327
- validateForm: ValidateForm<Partial<Values>>;
328
- /**
329
- * Perform a validation against the current form object, and execute the successCallback if the validation passes,
330
- * otherwise it executes the errorCallback.
331
- */
332
- submitForm: SubmitForm<Values>;
333
- };