Skip to content

Commit ccb6728

Browse files
committed
Add TypeScript definitions
1 parent 04f6317 commit ccb6728

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

index.d.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Type definitions for React Joi Validation
2+
// Project: react-joi-validation
3+
4+
import { Component } from 'react';
5+
import * as Joi from 'joi';
6+
7+
export function guessCorrectValue(event: Event, value: any): any;
8+
export function useEventTargetValue(event: Event): any;
9+
export function useFirstArgument<T>(value: T): T;
10+
export function useSecondArgument<T>(arg1: any, value: T): T;
11+
export function useThirdArgument<T>(arg1: any, arg2: any, value: T): T;
12+
13+
type Path = String;
14+
type ListOrSingle<T> = T|Array<T>;
15+
16+
interface ValidatorComponentProps {
17+
18+
}
19+
20+
interface ValidatedComponentProps extends ValidatorComponentProps {
21+
errors: object;
22+
23+
changeHandler: (path: Path, options?: { value?: any, strategy?: Function, validate?: boolean, callback?: Function }) => () => void;
24+
changesHandler: (changes: Array<[Path,any]>, options?: { validate?: boolean, callback?: Function }) => () => void;
25+
changeValue: (path: Path, value: any, options?: { validate?: boolean, callback?: Function }) => void;
26+
changeValues: (changes: Array<[Path,any]>, options?: { validate?: boolean, callback?: Function }) => void;
27+
28+
pushHandler: (path: Path, options?: { allowDuplicates?: boolean, value?: any, strategy?: Function, validate?: boolean, callback?: Function }) => () => void;
29+
pushValue: (path: Path, value: any, options?: { allowDuplicates?: boolean, validate?: boolean, callback?: Function }) => void;
30+
togglePushHandler: (path: Path, options?: { value?: any, strategy?: Function, validate?: boolean, callback?: Function }) => () => void;
31+
togglePushValue: (path: Path, value: any, options?: { validate?: boolean, callback?: Function }) => void;
32+
33+
unshiftHandler: (path: Path, options?: { allowDuplicates?: boolean, value?: any, strategy?: Function, validate?: boolean, callback?: Function }) => () => void;
34+
unshiftValue: (path: Path, value: any, options?: { allowDuplicates?: boolean, validate?: boolean, callback?: Function }) => void;
35+
toggleUnshiftHandler: (path: Path, options?: { value?: any, strategy?: Function, validate?: boolean, callback?: Function }) => () => void;
36+
toggleUnshiftValue: (path: Path, value: any, options?: { validate?: boolean, callback?: Function }) => void;
37+
38+
pullHandler: (path: Path, options?: { index?: number, removeAllInstances?: boolean, value?: any, strategy?: Function, validate?: boolean, callback?: Function }) => () => void;
39+
pullValue: (path: Path, value: any, options?: { index?: number, removeAllInstances?: boolean, validate?: boolean, callback?: Function }) => void;
40+
41+
validateAllHandler: (callback?: Function) => () => void;
42+
validateAll: (callback?: Function) => void;
43+
validateHandler: (paths: ListOrSingle<Path>, callback?: Function) => () => void;
44+
validate: (paths: ListOrSingle<Path>, callback?: Function) => void;
45+
46+
clearValidationAndResetValues: (paths?: ListOrSingle<Path>) => void;
47+
clearValidation: (paths?: ListOrSingle<Path>) => void;
48+
clearTouchedValues: () => void;
49+
}
50+
51+
interface ValidatorComponentPropsWithChildren extends ValidatorComponentProps {
52+
children: Component<ValidatedComponentProps, object>;
53+
}
54+
55+
interface ValidatorComponentState {
56+
errors: object;
57+
values: object;
58+
touchedValues: object;
59+
validatedValues: object;
60+
changingValues: Array<Path>;
61+
validatedAllValues: boolean;
62+
}
63+
64+
interface ValidatorOptions extends ValidatorComponentState {
65+
valuesWithDefaults: object;
66+
touchedValues: Array<Path>;
67+
validatedValues: Array<Path>;
68+
props: ValidatorComponentProps;
69+
errors: object;
70+
}
71+
72+
type ValidatorFunction = (options: ValidatorOptions) => { errors: object, values: object };
73+
74+
export default function(ValidatedComponent: Component, options: {
75+
joiSchema?: Joi.Schema,
76+
joiOptions?: Joi.ValidationOptions,
77+
validator?: ListOrSingle<ValidatorFunction>,
78+
only?: ListOrSingle<Path>,
79+
pseudoValues?: ListOrSingle<Path>,
80+
externalErrorsPath?: Path,
81+
changeHandlerStrategy?: Function,
82+
} = { pseudoValues: [], changeHandlerStrategy: guessCorrectValue }): Component<ValidatorComponentPropsWithChildren, ValidatorComponentState>;

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
"files": [
2424
"lib",
2525
"package.json",
26-
"README.md"
26+
"README.md",
27+
"index.d.ts"
2728
],
29+
"typings": "./index.d.ts",
2830
"author": "Aleck Greenham",
2931
"license": "ISC",
3032
"bugs": {
@@ -51,6 +53,8 @@
5153
"lodash.unset": "^4.5.2"
5254
},
5355
"devDependencies": {
56+
"@types/joi": "^13.0.5",
57+
"@types/react": "^16.0.36",
5458
"babel-cli": "^6.24.1",
5559
"babel-plugin-remove-comments": "^2.0.0",
5660
"babel-preset-es2015": "^6.24.1",

src/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,6 @@ const ReactJoiValidation = (ValidatedComponent, { joiSchema, joiOptions, validat
822822
* Updates the specified attribute with a new value
823823
*
824824
* @param {String} valuePath A path pointing to the attribute to update
825-
* @param {Object<String,*>?} options A hash of options that configure how the
826-
* attribute is updated
827825
* @param {*} value The new value to update the attribute to.
828826
* @param {Object<String,*>?} options A hash of configuration options
829827
* @param {Boolean|String|Array.<String>?} options.validate Whether to validate
@@ -1040,7 +1038,7 @@ const ReactJoiValidation = (ValidatedComponent, { joiSchema, joiOptions, validat
10401038
* in the validation component's state (including values set by defaultProps
10411039
* and passed in as props).
10421040
*
1043-
* @param {Array.<String>} validatePaths List of paths to values that will
1041+
* @param {String|Array.<String>} validatePaths List of paths to values that will
10441042
* be validated when the returned function is called
10451043
* @param {Function?} callback Function to call once the validation has been
10461044
* completed and any error messages set.
@@ -1076,7 +1074,7 @@ const ReactJoiValidation = (ValidatedComponent, { joiSchema, joiOptions, validat
10761074
* Validates some of the values currently in the validation component's state
10771075
* (including values set by defaultProps and passed in as props).
10781076
*
1079-
* @param {Array.<String>} validatePaths List of paths to values that will
1077+
* @param {String|Array.<String>} validatePaths List of paths to values that will
10801078
* be validated when the returned function is called
10811079
* @param {Function?} afterValidationCallback Function to call once the
10821080
* validation has been completed and any error messages set.
@@ -1112,7 +1110,7 @@ const ReactJoiValidation = (ValidatedComponent, { joiSchema, joiOptions, validat
11121110
* Clears the validation state and resets values for some or all of the values
11131111
* being handled by the validator component.
11141112
*
1115-
* @param {String|Array.<String>} paths A path, or a list of paths for which any
1113+
* @param {String|Array.<String>?} paths A path, or a list of paths for which any
11161114
* errors or values, should be reset to the default. If not provided, all values
11171115
* and errors are reset to their default.
11181116
*
@@ -1131,7 +1129,7 @@ const ReactJoiValidation = (ValidatedComponent, { joiSchema, joiOptions, validat
11311129
* Clears the validation state and resets values for some or all of the values
11321130
* being handled by the validator component.
11331131
*
1134-
* @param {String|Array.<String>} paths A path, or a list of paths for which any
1132+
* @param {String|Array.<String>?} paths A path, or a list of paths for which any
11351133
* errors or values, should be reset to the default. If not provided, all values
11361134
* and errors are reset to their default.
11371135
*
@@ -1184,7 +1182,7 @@ const ReactJoiValidation = (ValidatedComponent, { joiSchema, joiOptions, validat
11841182
/**
11851183
* Clears the validation state for some or all of the values being handled by
11861184
* the validator component. The actual values are NOT reset.
1187-
* @param {String|Array.<String>} paths A path, or a list of paths for which any
1185+
* @param {String|Array.<String>?} paths A path, or a list of paths for which any
11881186
* values should be reset to the default. If not provided, all values
11891187
* and errors are reset to their default.
11901188
*

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
# yarn lockfile v1
33

44

5+
"@types/joi@^13.0.5":
6+
version "13.0.5"
7+
resolved "https://registry.yarnpkg.com/@types/joi/-/joi-13.0.5.tgz#50829402204e8fec8ee287eeb37d79fbff34913c"
8+
9+
"@types/react@^16.0.36":
10+
version "16.0.36"
11+
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.0.36.tgz#ceb5639013bdb92a94147883052e69bb2c22c69b"
12+
513
abbrev@1:
614
version "1.1.0"
715
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f"

0 commit comments

Comments
 (0)