Skip to content

Commit 8468e65

Browse files
committed
Comment TypeScript definitions
1 parent dd986fa commit 8468e65

File tree

2 files changed

+249
-2
lines changed

2 files changed

+249
-2
lines changed

index.d.ts

Lines changed: 247 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,46 @@
44
import { Component } from 'react';
55
import * as Joi from 'joi';
66

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+
*/
713
export function guessCorrectValue(event: Event, value: any): any;
14+
15+
/**
16+
* Returns the target value of an event that is passed to it
17+
*/
818
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+
*/
924
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+
*/
1030
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+
*/
1136
export function useThirdArgument<T>(arg1: any, arg2: any, value: T): T;
1237

38+
/**
39+
* A string that describes a path to a particular value, using periods or square
40+
* parenthesis to indicate nesting
41+
*/
1342
type Path = String;
43+
44+
/**
45+
* A single item of a given type, or an array of that type
46+
*/
1447
type ListOrSingle<T> = T|Array<T>;
1548

1649
interface ValidatorComponentProps {
@@ -20,31 +53,139 @@ interface ValidatorComponentProps {
2053
interface ValidatedComponentProps extends ValidatorComponentProps {
2154
errors: object;
2255

56+
/**
57+
* Returns a function that, when called, updates the specified attribute with
58+
* a new value
59+
*/
2360
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+
*/
2465
changesHandler: (changes: Array<[Path,any]>, options?: { validate?: boolean, callback?: Function }) => () => void;
66+
67+
/**
68+
* Updates an attribute with a new value
69+
*/
2570
changeValue: (path: Path, value: any, options?: { validate?: boolean, callback?: Function }) => void;
71+
72+
/**
73+
* Updates multiple attributes with new values
74+
*/
2675
changeValues: (changes: Array<[Path,any]>, options?: { validate?: boolean, callback?: Function }) => void;
2776

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+
*/
2881
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+
*/
2986
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+
*/
3093
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+
*/
31100
togglePushValue: (path: Path, value: any, options?: { validate?: boolean, callback?: Function }) => void;
32101

102+
/**
103+
* Similar to pushValueHandler, but the returned function, when called,
104+
* adds a value to the beginning of an array, instead
105+
*/
33106
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+
*/
34112
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+
*/
35119
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+
*/
36126
toggleUnshiftValue: (path: Path, value: any, options?: { validate?: boolean, callback?: Function }) => void;
37127

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+
*/
38135
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+
*/
39144
pullValue: (path: Path, value: any, options?: { index?: number, removeAllInstances?: boolean, validate?: boolean, callback?: Function }) => void;
40145

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+
*/
41151
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+
*/
42157
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+
*/
43164
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+
*/
44170
validate: (paths: ListOrSingle<Path>, callback?: Function) => void;
45171

172+
/**
173+
* Clears the validation state and resets values for some or all of the values
174+
* being handled by the validator component.
175+
*/
46176
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+
*/
47182
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+
*/
48189
clearTouchedValues: () => void;
49190
}
50191

@@ -53,30 +194,135 @@ interface ValidatorComponentPropsWithChildren extends ValidatorComponentProps {
53194
}
54195

55196
interface ValidatorComponentState {
197+
/**
198+
* An object storing all of the validation errors for the values stored in
199+
* the validator component's state
200+
*/
56201
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+
*/
57207
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+
*/
58213
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+
*/
59219
validatedValues: object;
220+
221+
/**
222+
* A list of values that are changing in the current update cycle
223+
*/
60224
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+
*/
61230
validatedAllValues: boolean;
62231
}
63232

64233
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+
*/
65239
valuesWithDefaults: object;
240+
241+
/**
242+
* An array of value paths of all the values that have been changed
243+
*/
66244
touchedValues: Array<Path>;
245+
246+
/**
247+
* An array of value paths of all the values that have been validated
248+
*/
67249
validatedValues: Array<Path>;
250+
251+
/**
252+
* The props passed to the validator component
253+
*/
68254
props: ValidatorComponentProps;
255+
256+
/**
257+
* An object of errors resulting from applying the validations to the list
258+
* of validated values
259+
*/
69260
errors: object;
70261
}
71262

72263
type ValidatorFunction = (options: ValidatorOptions) => { errors: object, values: object };
73264

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+
*/
74276
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+
*/
75281
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+
*/
76287
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+
*/
77296
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+
*/
78303
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+
*/
79310
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+
*/
80317
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+
*/
81327
changeHandlerStrategy?: Function,
82-
} = { pseudoValues: [], changeHandlerStrategy: guessCorrectValue }): Component<ValidatorComponentPropsWithChildren, ValidatorComponentState>;
328+
} = { pseudoValues: [], changeHandlerStrategy: guessCorrectValue }): ValidatorComponent;

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ const ReactJoiValidation = (ValidatedComponent, { joiSchema, joiOptions, validat
819819
}
820820

821821
/**
822-
* Updates the specified attribute with a new value
822+
* Updates an attribute with a new value
823823
*
824824
* @param {String} valuePath A path pointing to the attribute to update
825825
* @param {*} value The new value to update the attribute to.
@@ -1182,6 +1182,7 @@ const ReactJoiValidation = (ValidatedComponent, { joiSchema, joiOptions, validat
11821182
/**
11831183
* Clears the validation state for some or all of the values being handled by
11841184
* the validator component. The actual values are NOT reset.
1185+
*
11851186
* @param {String|Array.<String>?} paths A path, or a list of paths for which any
11861187
* values should be reset to the default. If not provided, all values
11871188
* and errors are reset to their default.

0 commit comments

Comments
 (0)