4
4
import { Component } from 'react' ;
5
5
import * as Joi from 'joi' ;
6
6
7
+ /**
8
+ * Takes arguments that resemble those commonly passed to event handlers and attempts
9
+ * to guess what the desired event value should be. This function is the default used
10
+ * by all *Handler methods to extract values from events so that they can be set and
11
+ * validated.
12
+ */
7
13
export function guessCorrectValue ( event : Event , value : any ) : any ;
14
+
15
+ /**
16
+ * Returns the target value of an event that is passed to it
17
+ */
8
18
export function useEventTargetValue ( event : Event ) : any ;
19
+
20
+ /**
21
+ * Returns the first argument. Used for adapting event handlers that return the event
22
+ * value as the first argument.
23
+ */
9
24
export function useFirstArgument < T > ( value : T ) : T ;
25
+
26
+ /**
27
+ * Returns the second argument. Used for adapting event handlers that return the event
28
+ * value as the second argument.
29
+ */
10
30
export function useSecondArgument < T > ( arg1 : any , value : T ) : T ;
31
+
32
+ /**
33
+ * Returns the third argument. Used for adapting event handlers that return the event
34
+ * value as the third argument.
35
+ */
11
36
export function useThirdArgument < T > ( arg1 : any , arg2 : any , value : T ) : T ;
12
37
38
+ /**
39
+ * A string that describes a path to a particular value, using periods or square
40
+ * parenthesis to indicate nesting
41
+ */
13
42
type Path = String ;
43
+
44
+ /**
45
+ * A single item of a given type, or an array of that type
46
+ */
14
47
type ListOrSingle < T > = T | Array < T > ;
15
48
16
49
interface ValidatorComponentProps {
@@ -20,31 +53,139 @@ interface ValidatorComponentProps {
20
53
interface ValidatedComponentProps extends ValidatorComponentProps {
21
54
errors : object ;
22
55
56
+ /**
57
+ * Returns a function that, when called, updates the specified attribute with
58
+ * a new value
59
+ */
23
60
changeHandler : ( path : Path , options ?: { value ?: any , strategy ?: Function , validate ?: boolean , callback ?: Function } ) => ( ) => void ;
61
+
62
+ /**
63
+ * Returns a function that, when called, updates multiple attributes with new values
64
+ */
24
65
changesHandler : ( changes : Array < [ Path , any ] > , options ?: { validate ?: boolean , callback ?: Function } ) => ( ) => void ;
66
+
67
+ /**
68
+ * Updates an attribute with a new value
69
+ */
25
70
changeValue : ( path : Path , value : any , options ?: { validate ?: boolean , callback ?: Function } ) => void ;
71
+
72
+ /**
73
+ * Updates multiple attributes with new values
74
+ */
26
75
changeValues : ( changes : Array < [ Path , any ] > , options ?: { validate ?: boolean , callback ?: Function } ) => void ;
27
76
77
+ /**
78
+ * Returns a function that, when called, pushes a value onto the end of an array
79
+ * stored in the validator component's state
80
+ */
28
81
pushHandler : ( path : Path , options ?: { allowDuplicates ?: boolean , value ?: any , strategy ?: Function , validate ?: boolean , callback ?: Function } ) => ( ) => void ;
82
+
83
+ /**
84
+ * Immediately pushes a value onto the end of an array
85
+ */
29
86
pushValue : ( path : Path , value : any , options ?: { allowDuplicates ?: boolean , validate ?: boolean , callback ?: Function } ) => void ;
87
+
88
+ /**
89
+ * Returns a function that, when called, pushes a value onto the end of an array
90
+ * if that value is not already in the array, otherwise it removes it. i.e. it
91
+ * toggles that value's inclusion in the array.
92
+ */
30
93
togglePushHandler : ( path : Path , options ?: { value ?: any , strategy ?: Function , validate ?: boolean , callback ?: Function } ) => ( ) => void ;
94
+
95
+ /**
96
+ * Immediately pushes a value onto the end of an array if that value is not
97
+ * already in the array, otherwise it removes it. i.e. it toggles that value's
98
+ * inclusion in the array.
99
+ */
31
100
togglePushValue : ( path : Path , value : any , options ?: { validate ?: boolean , callback ?: Function } ) => void ;
32
101
102
+ /**
103
+ * Similar to pushValueHandler, but the returned function, when called,
104
+ * adds a value to the beginning of an array, instead
105
+ */
33
106
unshiftHandler : ( path : Path , options ?: { allowDuplicates ?: boolean , value ?: any , strategy ?: Function , validate ?: boolean , callback ?: Function } ) => ( ) => void ;
107
+
108
+ /**
109
+ * Similar to pushValue, but immediately adds a value to the beginning of an array
110
+ * instead
111
+ */
34
112
unshiftValue : ( path : Path , value : any , options ?: { allowDuplicates ?: boolean , validate ?: boolean , callback ?: Function } ) => void ;
113
+
114
+ /**
115
+ * Returns a function that, when called, adds a value to the beginning of an array
116
+ * if that value is not already in the array, otherwise it removes it. i.e. it
117
+ * toggles that value's inclusion in the array.
118
+ */
35
119
toggleUnshiftHandler : ( path : Path , options ?: { value ?: any , strategy ?: Function , validate ?: boolean , callback ?: Function } ) => ( ) => void ;
120
+
121
+ /**
122
+ * Immediately pushes a value to the beginning of an array if that value is not
123
+ * already in the array, otherwise it removes it. i.e. it toggles that value's
124
+ * inclusion in the array.
125
+ */
36
126
toggleUnshiftValue : ( path : Path , value : any , options ?: { validate ?: boolean , callback ?: Function } ) => void ;
37
127
128
+ /**
129
+ * The opposite of pushHandler and unshiftHandler; returns a function that,
130
+ * when called, will remove one or more instances of a value from an array.
131
+ *
132
+ * The default behaviour is to remove only the first instance of the specified
133
+ * value from the array. i.e. the instance of the value with the lowest index.
134
+ */
38
135
pullHandler : ( path : Path , options ?: { index ?: number , removeAllInstances ?: boolean , value ?: any , strategy ?: Function , validate ?: boolean , callback ?: Function } ) => ( ) => void ;
136
+
137
+ /**
138
+ * The opposite of pushValue and unshiftValue; immediately removes a value from
139
+ * an array.
140
+ *
141
+ * The default behaviour is to remove only the first instance of the specified
142
+ * value from the array. i.e. the instance of the value with the lowest index.
143
+ */
39
144
pullValue : ( path : Path , value : any , options ?: { index ?: number , removeAllInstances ?: boolean , validate ?: boolean , callback ?: Function } ) => void ;
40
145
146
+ /**
147
+ * Returns a function that, when called, validates all values currently in the
148
+ * validation component's state (including values set by defaultProps and passed
149
+ * in as props).
150
+ */
41
151
validateAllHandler : ( callback ?: Function ) => ( ) => void ;
152
+
153
+ /**
154
+ * Validates all values currently in the validation component's state
155
+ * (including values set by defaultProps and passed in as props).
156
+ */
42
157
validateAll : ( callback ?: Function ) => void ;
158
+
159
+ /**
160
+ * Returns a function that, when called, validates some of the values currently
161
+ * in the validation component's state (including values set by defaultProps
162
+ * and passed in as props).
163
+ */
43
164
validateHandler : ( paths : ListOrSingle < Path > , callback ?: Function ) => ( ) => void ;
165
+
166
+ /**
167
+ * Validates some of the values currently in the validation component's state
168
+ * (including values set by defaultProps and passed in as props).
169
+ */
44
170
validate : ( paths : ListOrSingle < Path > , callback ?: Function ) => void ;
45
171
172
+ /**
173
+ * Clears the validation state and resets values for some or all of the values
174
+ * being handled by the validator component.
175
+ */
46
176
clearValidationAndResetValues : ( paths ?: ListOrSingle < Path > ) => void ;
177
+
178
+ /**
179
+ * Clears the validation state for some or all of the values being handled by
180
+ * the validator component. The actual values are NOT reset.
181
+ */
47
182
clearValidation : ( paths ?: ListOrSingle < Path > ) => void ;
183
+
184
+ /**
185
+ * Clears the record of which values have been touched, i.e. the values that
186
+ * the validated component has updated or validated using any of the functions the
187
+ * validator component provides
188
+ */
48
189
clearTouchedValues : ( ) => void ;
49
190
}
50
191
@@ -53,30 +194,135 @@ interface ValidatorComponentPropsWithChildren extends ValidatorComponentProps {
53
194
}
54
195
55
196
interface ValidatorComponentState {
197
+ /**
198
+ * An object storing all of the validation errors for the values stored in
199
+ * the validator component's state
200
+ */
56
201
errors : object ;
202
+
203
+ /**
204
+ * An object containing all the values that have been set by the validated
205
+ * component using the change helper methods
206
+ */
57
207
values : object ;
208
+
209
+ /**
210
+ * An object containing references to all the values that have been changed
211
+ * by the validated component using the change helper methods
212
+ */
58
213
touchedValues : object ;
214
+
215
+ /**
216
+ * An object containing references to all the values that have been validated
217
+ * by the validated component using the change or validate helper methods
218
+ */
59
219
validatedValues : object ;
220
+
221
+ /**
222
+ * A list of values that are changing in the current update cycle
223
+ */
60
224
changingValues : Array < Path > ;
225
+
226
+ /**
227
+ * Whether to validate all the values stored in the validator component's state
228
+ * and not just those mentioned in validatedValues
229
+ */
61
230
validatedAllValues : boolean ;
62
231
}
63
232
64
233
interface ValidatorOptions extends ValidatorComponentState {
234
+ /**
235
+ * An object of values set by starting with the props passed to the validated
236
+ * component, and then deeply merging in the values that have been changed
237
+ * by the validated component calling one of the change methods
238
+ */
65
239
valuesWithDefaults : object ;
240
+
241
+ /**
242
+ * An array of value paths of all the values that have been changed
243
+ */
66
244
touchedValues : Array < Path > ;
245
+
246
+ /**
247
+ * An array of value paths of all the values that have been validated
248
+ */
67
249
validatedValues : Array < Path > ;
250
+
251
+ /**
252
+ * The props passed to the validator component
253
+ */
68
254
props : ValidatorComponentProps ;
255
+
256
+ /**
257
+ * An object of errors resulting from applying the validations to the list
258
+ * of validated values
259
+ */
69
260
errors : object ;
70
261
}
71
262
72
263
type ValidatorFunction = ( options : ValidatorOptions ) => { errors : object , values : object } ;
73
264
265
+ /**
266
+ * Component class that renders its children with props that contain functions for
267
+ * updating and validating the values stored in the validator component's state.
268
+ */
269
+ type ValidatorComponent = Component < ValidatorComponentPropsWithChildren , ValidatorComponentState > ;
270
+
271
+ /**
272
+ * Returns a validator component that wraps a validated component and provides
273
+ * methods for updating and validating the validator component's state via the
274
+ * validated component's props
275
+ */
74
276
export default function ( ValidatedComponent : Component , options : {
277
+ /**
278
+ * A Joi schema that, if provided, is used to validate the values stored in
279
+ * the validator component's state every time
280
+ */
75
281
joiSchema ?: Joi . Schema ,
282
+
283
+ /**
284
+ * Options that are passed to Joi on every validation attempt. See the
285
+ * documentation for the version of Joi that you are using.
286
+ */
76
287
joiOptions ?: Joi . ValidationOptions ,
288
+
289
+ /**
290
+ * A function or array of functions to use to validate the values stored in
291
+ * the validator component's state every time one of the validate methods are
292
+ * called. Can be used in conjunction with the joiSchema option, or without it.
293
+ * If both are specified, the joiSchema validation is performed first and then
294
+ * the validators are called in the order that they appear in the array.
295
+ */
77
296
validator ?: ListOrSingle < ValidatorFunction > ,
297
+
298
+ /**
299
+ * Path or paths to the part of the props object passed to the validator (and
300
+ * passed through to the validated object) that should be validated against the
301
+ * joi schema. Useful if you want to validate only part of the props.
302
+ */
78
303
only ?: ListOrSingle < Path > ,
304
+
305
+ /**
306
+ * List of paths to values that aren't actually in the props passed to the
307
+ * validator component and are never set by the validated component, but are
308
+ * accessible to attach validation errors to, in validator functions.
309
+ */
79
310
pseudoValues ?: ListOrSingle < Path > ,
311
+
312
+ /**
313
+ * Path to the attribute on props that stores the errors object of any
314
+ * validation performed outside of the validator component. These errors are
315
+ * merged into those set by the validator component.
316
+ */
80
317
externalErrorsPath ?: Path ,
318
+
319
+ /**
320
+ * The default change handler strategy to use in the validator component. i.e.
321
+ * The default function that should be used to map the arguments of event handlers
322
+ * to the validation handlers', provided by the validator component. If
323
+ * unspecified, the default set at the package level using
324
+ * setChangeHandlerStrategy() is used, or the guessCorrectValue function if none
325
+ * has been otherwise specified.
326
+ */
81
327
changeHandlerStrategy ?: Function ,
82
- } = { pseudoValues : [ ] , changeHandlerStrategy : guessCorrectValue } ) : Component < ValidatorComponentPropsWithChildren , ValidatorComponentState > ;
328
+ } = { pseudoValues : [ ] , changeHandlerStrategy : guessCorrectValue } ) : ValidatorComponent ;
0 commit comments