Skip to content

Commit 7457a55

Browse files
authored
Merge pull request #24 from nullgr/release/1.3.4
Release/1.3.4
2 parents e304dfa + 34227ad commit 7457a55

6 files changed

Lines changed: 93 additions & 7 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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
22
"name": "react-validation-utils",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"description": "Validate react form components, based on their state",
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",
11-
"prepublish": "npm run build",
12-
"release": "np --no-yarn --no-publish"
11+
"prepublish": "npm run build"
1312
},
1413
"repository": {
1514
"type": "git",
@@ -34,7 +33,6 @@
3433
"eslint-plugin-prettier": "2.3.1",
3534
"husky": "^0.14.3",
3635
"jest": "^22.4.0",
37-
"np": "^2.20.1",
3836
"prettier": "^1.10.2",
3937
"watch": "^1.0.1"
4038
}

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)