Skip to content

Commit b46e7a7

Browse files
authored
Merge pull request #22 from nullgr/feature/type-definitons
Feature/type definitons
2 parents e304dfa + 61c2988 commit b46e7a7

6 files changed

Lines changed: 91 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ When you are creating the component state, you can use this method to prevalidat
165165
If you want, you can set `showErrorsOnStart` to true, so fields will be validated and you will get all the errors in the first component render.
166166
When you are adding a Validator with `Validator.addValidation` method, all the fields will be prevalidated.
167167
Prevalidation means, that you will get no error message, if the field has not passed a validation rule (`'prevalidation-failed'`).
168-
Vэlidation means, that you will get an error message, if the field has not passed a validation rule (`'validation-failed'`).
168+
Validation means, that you will get an error message, if the field has not passed a validation rule (`'validation-failed'`).
169169

170170
### validate({stateChange} | updater | null, showErrors = true)
171171
You should use it inside the this.setState method like it was already described [here](#validation).
@@ -235,4 +235,4 @@ Use this method to check, if particular field is valid. Returns true if it is va
235235

236236

237237
## Compatibility
238-
This package id fully compatible with the React v.16, because it uses state updater functions inside.
238+
This package is fully compatible with the React v.16, because it uses state updater functions inside.

build/index.d.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
type Rule = (val: string | boolean, state: object) => boolean;
2+
3+
type RuleData = {
4+
rule: Rule;
5+
message: string;
6+
id?: string;
7+
};
8+
9+
type FieldsDescription = {
10+
[key: string]: RuleData | RuleData[];
11+
};
12+
13+
export default class Validator<State> {
14+
constructor(fields: FieldsDescription, validationStorageName?: string);
15+
16+
// 'State' type in methods below is right only in argument of addValidation method
17+
//
18+
// In all another methods 'State' type is incorrect,
19+
// in real scenario it contains one more field [validationStorageName]
20+
//
21+
// In most cases user of library shouldn't care about [validationStorageName] field
22+
// in component state, so this inaccuracy isn't very serious, but at the same time
23+
// it simplifies the use of library
24+
25+
addValidation(state: State, showErrorsOnStart?: boolean): Readonly<State>;
26+
27+
validate(
28+
stateUpdates: (prevState: Readonly<State>) => State,
29+
showErrors?: boolean
30+
): ((prevState: Readonly<State>) => State);
31+
32+
validate(
33+
stateUpdates?: { [key in keyof State]: string } | null,
34+
showErrors?: boolean
35+
): Readonly<State>;
36+
37+
getErrors(state: State): { [key in keyof State]: string };
38+
39+
isFormValid(state: State): boolean;
40+
41+
isFieldValid(state: State, fieldName: string): boolean;
42+
}

build/rules/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export function requiredRule(value: string | boolean): boolean;
2+
export function lengthRule(l: number): (v: string) => boolean;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "build/index.js",
66
"scripts": {
77
"dev": "watch 'npm run build' src",
8-
"build": "babel src -d build",
8+
"build": "babel src -d build && babel types -d build --copy-files",
99
"test": "jest",
1010
"prepush": "npm test",
1111
"prepublish": "npm run build",

types/index.d.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
type Rule = (val: string | boolean, state: object) => boolean;
2+
3+
type RuleData = {
4+
rule: Rule;
5+
message: string;
6+
id?: string;
7+
};
8+
9+
type FieldsDescription = {
10+
[key: string]: RuleData | RuleData[];
11+
};
12+
13+
export default class Validator<State> {
14+
constructor(fields: FieldsDescription, validationStorageName?: string);
15+
16+
// 'State' type in methods below is right only in argument of addValidation method
17+
//
18+
// In all another methods 'State' type is incorrect,
19+
// in real scenario it contains one more field [validationStorageName]
20+
//
21+
// In most cases user of library shouldn't care about [validationStorageName] field
22+
// in component state, so this inaccuracy isn't very serious, but at the same time
23+
// it simplifies the use of library
24+
25+
addValidation(state: State, showErrorsOnStart?: boolean): Readonly<State>;
26+
27+
validate(
28+
stateUpdates: (prevState: Readonly<State>) => State,
29+
showErrors?: boolean
30+
): ((prevState: Readonly<State>) => State);
31+
32+
validate(
33+
stateUpdates?: { [key in keyof State]: string } | null,
34+
showErrors?: boolean
35+
): Readonly<State>;
36+
37+
getErrors(state: State): { [key in keyof State]: string };
38+
39+
isFormValid(state: State): boolean;
40+
41+
isFieldValid(state: State, fieldName: string): boolean;
42+
}

types/rules/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export function requiredRule(value: string | boolean): boolean;
2+
export function lengthRule(l: number): (v: string) => boolean;

0 commit comments

Comments
 (0)