@coxy/react-validator 2.0.7 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -120
- package/biome.json +32 -0
- package/dist/index.d.mts +116 -0
- package/dist/index.d.ts +116 -6
- package/dist/index.js +2 -28
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/example/example.tsx +13 -9
- package/example/tsconfig.json +3 -8
- package/package.json +21 -30
- package/src/context.ts +1 -1
- package/src/jest.d.ts +1 -0
- package/src/rules.test.ts +6 -6
- package/src/rules.ts +3 -3
- package/src/types.ts +3 -2
- package/src/use-validator.test.tsx +4 -6
- package/src/use-validator.ts +3 -3
- package/src/validator-field.test.tsx +11 -15
- package/src/validator-field.tsx +10 -9
- package/src/validator-wrapper.test.tsx +16 -20
- package/src/validator-wrapper.tsx +9 -9
- package/src/validator.ts +7 -7
- package/tsconfig.json +6 -16
- package/tsup.config.ts +14 -0
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -5
- package/.prettierrc.js +0 -10
- package/CHANGELOG.md +0 -49
- package/dist/context.d.ts +0 -7
- package/dist/context.js +0 -5
- package/dist/rules.d.ts +0 -32
- package/dist/rules.js +0 -60
- package/dist/types.d.ts +0 -24
- package/dist/types.js +0 -2
- package/dist/use-validator.d.ts +0 -4
- package/dist/use-validator.js +0 -22
- package/dist/validator-field.d.ts +0 -17
- package/dist/validator-field.js +0 -38
- package/dist/validator-wrapper.d.ts +0 -24
- package/dist/validator-wrapper.js +0 -57
- package/dist/validator.d.ts +0 -21
- package/dist/validator.js +0 -72
- package/tsconfig.build.json +0 -12
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { Component, ReactNode, RefObject } from 'react'
|
|
1
|
+
import { Component, type ReactNode, type RefObject } from 'react'
|
|
2
2
|
|
|
3
|
-
import { Validity } from './types'
|
|
4
3
|
import { Context } from './context'
|
|
5
|
-
import {
|
|
4
|
+
import type { Validity } from './types'
|
|
5
|
+
import { type Field, Validator } from './validator'
|
|
6
6
|
|
|
7
7
|
interface ComponentProps {
|
|
8
8
|
children?: ReactNode
|
|
9
9
|
stopAtFirstError?: boolean
|
|
10
|
-
ref?: RefObject<
|
|
10
|
+
ref?: RefObject<ValidatorWrapper>
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export class ValidatorWrapper extends Component<ComponentProps> {
|
|
@@ -16,8 +16,8 @@ export class ValidatorWrapper extends Component<ComponentProps> {
|
|
|
16
16
|
customErrors: [],
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
constructor(props
|
|
20
|
-
super(props
|
|
19
|
+
constructor(props) {
|
|
20
|
+
super(props)
|
|
21
21
|
this.registerField = this.registerField.bind(this)
|
|
22
22
|
this.unregisterField = this.unregisterField.bind(this)
|
|
23
23
|
}
|
|
@@ -53,13 +53,13 @@ export class ValidatorWrapper extends Component<ComponentProps> {
|
|
|
53
53
|
|
|
54
54
|
validate(): Validity {
|
|
55
55
|
const validator = new Validator({ stopAtFirstError: this.props.stopAtFirstError })
|
|
56
|
-
this.fields
|
|
56
|
+
for (const comp of this.fields) {
|
|
57
57
|
validator.addField(comp.props)
|
|
58
|
-
}
|
|
58
|
+
}
|
|
59
59
|
return validator.validate()
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
render() {
|
|
62
|
+
render(): ReactNode {
|
|
63
63
|
return (
|
|
64
64
|
<Context.Provider
|
|
65
65
|
value={{
|
package/src/validator.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import type { ValidatorRules } from './rules'
|
|
2
|
+
import type { FieldParams, Validity } from './types'
|
|
3
|
+
import type { Value } from './validator-field'
|
|
4
4
|
|
|
5
5
|
export class Field {
|
|
6
6
|
private rules: ValidatorRules
|
|
@@ -20,12 +20,12 @@ export class Field {
|
|
|
20
20
|
let message = ''
|
|
21
21
|
const { rules, value, required, id } = this
|
|
22
22
|
|
|
23
|
-
const isEmptyValue = !value && parseFloat(value) !== 0
|
|
23
|
+
const isEmptyValue = !value && Number.parseFloat(value) !== 0
|
|
24
24
|
|
|
25
25
|
if (!rules.length || (isEmptyValue && required === false)) {
|
|
26
26
|
return { isValid, message, id }
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
for (const instance of rules) {
|
|
29
29
|
if (isValid) {
|
|
30
30
|
isValid = instance.rule(value)
|
|
31
31
|
if (!isValid) {
|
|
@@ -36,7 +36,7 @@ export class Field {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
}
|
|
39
|
+
}
|
|
40
40
|
return { isValid, message, id }
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -70,7 +70,7 @@ export class Validator {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
validate(): Validity {
|
|
73
|
-
let prevResult
|
|
73
|
+
let prevResult: Validity | null
|
|
74
74
|
const statuses = this.fields.map((field) => {
|
|
75
75
|
if (this.params?.stopAtFirstError && prevResult && prevResult.isValid === false) {
|
|
76
76
|
return null
|
package/tsconfig.json
CHANGED
|
@@ -6,21 +6,17 @@
|
|
|
6
6
|
"removeComments": true,
|
|
7
7
|
"emitDecoratorMetadata": true,
|
|
8
8
|
"experimentalDecorators": true,
|
|
9
|
-
"target": "
|
|
9
|
+
"target": "ESNext",
|
|
10
10
|
"sourceMap": false,
|
|
11
|
-
"composite":
|
|
12
|
-
"incremental":
|
|
11
|
+
"composite": false,
|
|
12
|
+
"incremental": false,
|
|
13
13
|
"noUnusedLocals": true,
|
|
14
14
|
"noUnusedParameters": false,
|
|
15
15
|
"resolveJsonModule": true,
|
|
16
16
|
"outDir": "./dist",
|
|
17
17
|
"rootDir": "src",
|
|
18
18
|
"baseUrl": "./src",
|
|
19
|
-
"lib": [
|
|
20
|
-
"dom",
|
|
21
|
-
"dom.iterable",
|
|
22
|
-
"esnext"
|
|
23
|
-
],
|
|
19
|
+
"lib": ["dom", "dom.iterable", "esnext"],
|
|
24
20
|
"allowJs": true,
|
|
25
21
|
"skipLibCheck": true,
|
|
26
22
|
"strict": false,
|
|
@@ -28,12 +24,6 @@
|
|
|
28
24
|
"moduleResolution": "node",
|
|
29
25
|
"jsx": "react-jsx"
|
|
30
26
|
},
|
|
31
|
-
"include": [
|
|
32
|
-
|
|
33
|
-
"src/**/*"
|
|
34
|
-
],
|
|
35
|
-
"exclude": [
|
|
36
|
-
"node_modules",
|
|
37
|
-
"dist"
|
|
38
|
-
]
|
|
27
|
+
"include": ["src", "src/**/*"],
|
|
28
|
+
"exclude": ["node_modules", "dist"]
|
|
39
29
|
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup'
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ['src/index.ts'],
|
|
5
|
+
format: ['cjs', 'esm'],
|
|
6
|
+
target: 'esnext',
|
|
7
|
+
clean: true,
|
|
8
|
+
dts: true,
|
|
9
|
+
treeshake: true,
|
|
10
|
+
splitting: false,
|
|
11
|
+
minify: true,
|
|
12
|
+
sourcemap: true,
|
|
13
|
+
external: ['react'],
|
|
14
|
+
})
|
package/.eslintignore
DELETED
package/.eslintrc.js
DELETED
package/.prettierrc.js
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
2.0.1 / 2022-06-12
|
|
2
|
-
==================
|
|
3
|
-
* Typescript version
|
|
4
|
-
* Update dependencies
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
1.3.0 / 2020-11-10
|
|
8
|
-
==================
|
|
9
|
-
* Update dependencies
|
|
10
|
-
* "message" can be a function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
1.2.6 / 2020-08-23
|
|
14
|
-
==================
|
|
15
|
-
* Update dependencies
|
|
16
|
-
|
|
17
|
-
1.2.5 / 2020-08-23
|
|
18
|
-
==================
|
|
19
|
-
* Fix email validation
|
|
20
|
-
* Add test for sort rules
|
|
21
|
-
|
|
22
|
-
1.2.4 / 2020-08-13
|
|
23
|
-
==================
|
|
24
|
-
* Fix rules order validation
|
|
25
|
-
|
|
26
|
-
1.2.3 / 2020-08-13
|
|
27
|
-
==================
|
|
28
|
-
* Update dependencies
|
|
29
|
-
* Fix rules order validation
|
|
30
|
-
|
|
31
|
-
1.2.1 / 2020-08-3
|
|
32
|
-
==================
|
|
33
|
-
* Fix Readme
|
|
34
|
-
* Update eslint
|
|
35
|
-
* Small fixes
|
|
36
|
-
|
|
37
|
-
1.2.0 / 2020-07-28
|
|
38
|
-
==================
|
|
39
|
-
* Add new inline Validator API
|
|
40
|
-
* Add more tests
|
|
41
|
-
* Add examples
|
|
42
|
-
|
|
43
|
-
1.1.7 / 2020-07-28
|
|
44
|
-
==================
|
|
45
|
-
* Update tests
|
|
46
|
-
|
|
47
|
-
1.1.6 / 2020-07-24
|
|
48
|
-
==================
|
|
49
|
-
* Update readme. Add more examples
|
package/dist/context.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
import { Validity } from './types';
|
|
3
|
-
export declare const Context: import("react").Context<{
|
|
4
|
-
registerField: (field: string | number) => void;
|
|
5
|
-
unregisterField: (field: string | number) => void;
|
|
6
|
-
customErrors: Array<Validity>;
|
|
7
|
-
}>;
|
package/dist/context.js
DELETED
package/dist/rules.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { ValidatorRule } from './types';
|
|
2
|
-
export declare type ValidatorRules = ValidatorRule[];
|
|
3
|
-
export declare const rules: {
|
|
4
|
-
notEmpty: {
|
|
5
|
-
rule: (value: any) => boolean;
|
|
6
|
-
message: string;
|
|
7
|
-
}[];
|
|
8
|
-
bool: {
|
|
9
|
-
rule: (value: any) => boolean;
|
|
10
|
-
message: string;
|
|
11
|
-
}[];
|
|
12
|
-
password: {
|
|
13
|
-
rule: (value: any) => boolean;
|
|
14
|
-
message: string;
|
|
15
|
-
}[];
|
|
16
|
-
email: {
|
|
17
|
-
rule: (value: any) => boolean;
|
|
18
|
-
message: string;
|
|
19
|
-
}[];
|
|
20
|
-
min: (min: any) => {
|
|
21
|
-
rule: (value: any) => boolean;
|
|
22
|
-
message: string;
|
|
23
|
-
}[];
|
|
24
|
-
max: (max: any) => {
|
|
25
|
-
rule: (value: any) => boolean;
|
|
26
|
-
message: string;
|
|
27
|
-
}[];
|
|
28
|
-
length: (min: any, max?: any) => {
|
|
29
|
-
rule: (value: any) => boolean;
|
|
30
|
-
message: string;
|
|
31
|
-
}[];
|
|
32
|
-
};
|
package/dist/rules.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rules = void 0;
|
|
4
|
-
const emailReg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
5
|
-
exports.rules = {
|
|
6
|
-
notEmpty: [
|
|
7
|
-
{
|
|
8
|
-
rule: (value) => value !== '' && value.length > 0,
|
|
9
|
-
message: 'Value is required',
|
|
10
|
-
},
|
|
11
|
-
],
|
|
12
|
-
bool: [
|
|
13
|
-
{
|
|
14
|
-
rule: (value) => !!value,
|
|
15
|
-
message: 'Value is required',
|
|
16
|
-
},
|
|
17
|
-
],
|
|
18
|
-
password: [
|
|
19
|
-
{
|
|
20
|
-
rule: (value) => value.length > 0,
|
|
21
|
-
message: 'Password field cannot be empty',
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
rule: (value) => value.length > 5,
|
|
25
|
-
message: 'Password field can not be less than 6 characters',
|
|
26
|
-
},
|
|
27
|
-
],
|
|
28
|
-
email: [
|
|
29
|
-
{
|
|
30
|
-
rule: (value) => !!value && value !== '' && value.length !== 0,
|
|
31
|
-
message: 'Email is required',
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
rule: (value) => emailReg.test(String(value).toLowerCase()),
|
|
35
|
-
message: 'Email is invalid',
|
|
36
|
-
},
|
|
37
|
-
],
|
|
38
|
-
min: (min) => [
|
|
39
|
-
{
|
|
40
|
-
rule: (value) => parseFloat(value) > min,
|
|
41
|
-
message: `The value must be greater than ${min}`,
|
|
42
|
-
},
|
|
43
|
-
],
|
|
44
|
-
max: (max) => [
|
|
45
|
-
{
|
|
46
|
-
rule: (value) => parseFloat(value) < max,
|
|
47
|
-
message: `The value must be smaller ${max}`,
|
|
48
|
-
},
|
|
49
|
-
],
|
|
50
|
-
length: (min, max) => [
|
|
51
|
-
{
|
|
52
|
-
rule: (value) => String(value).length >= min,
|
|
53
|
-
message: `No less than ${min} symbols`,
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
rule: (value) => (max !== undefined ? String(value).length <= max : true),
|
|
57
|
-
message: `No more than ${max} symbols`,
|
|
58
|
-
},
|
|
59
|
-
],
|
|
60
|
-
};
|
package/dist/types.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { ValidatorRules } from './rules';
|
|
2
|
-
import { Value } from './validator-field';
|
|
3
|
-
declare type Fn = (value: Value) => string;
|
|
4
|
-
export interface ValidatorRule {
|
|
5
|
-
rule: (value: Value) => boolean;
|
|
6
|
-
message: string | Fn;
|
|
7
|
-
}
|
|
8
|
-
export interface ErrorMessage {
|
|
9
|
-
message: string;
|
|
10
|
-
isValid: boolean;
|
|
11
|
-
}
|
|
12
|
-
export interface Validity {
|
|
13
|
-
message: string;
|
|
14
|
-
isValid: boolean;
|
|
15
|
-
errors?: ErrorMessage[];
|
|
16
|
-
id?: string | number;
|
|
17
|
-
}
|
|
18
|
-
export interface FieldParams {
|
|
19
|
-
value: Value;
|
|
20
|
-
rules: ValidatorRules;
|
|
21
|
-
required?: boolean;
|
|
22
|
-
id?: string | number;
|
|
23
|
-
}
|
|
24
|
-
export {};
|
package/dist/types.js
DELETED
package/dist/use-validator.d.ts
DELETED
package/dist/use-validator.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
-
t[p[i]] = s[p[i]];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.useValidator = void 0;
|
|
15
|
-
const validator_1 = require("./validator");
|
|
16
|
-
function useValidator(value, rules) {
|
|
17
|
-
const validator = new validator_1.Validator();
|
|
18
|
-
validator.addField({ value, rules });
|
|
19
|
-
const _a = validator.validate(), { isValid } = _a, validateObject = __rest(_a, ["isValid"]);
|
|
20
|
-
return [isValid, validateObject];
|
|
21
|
-
}
|
|
22
|
-
exports.useValidator = useValidator;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { ReactNode } from 'react';
|
|
2
|
-
import { Validity } from 'types';
|
|
3
|
-
import { ValidatorRules } from './rules';
|
|
4
|
-
export declare type Value = any;
|
|
5
|
-
declare type Fn = (validity: Validity, value: Value) => ReactNode;
|
|
6
|
-
interface Props {
|
|
7
|
-
rules?: ValidatorRules;
|
|
8
|
-
required?: boolean;
|
|
9
|
-
value?: Value;
|
|
10
|
-
id?: string | number;
|
|
11
|
-
children?: ReactNode | Fn;
|
|
12
|
-
unregisterField: (val: any) => void;
|
|
13
|
-
registerField: (val: any) => void;
|
|
14
|
-
customErrors: Array<Validity>;
|
|
15
|
-
}
|
|
16
|
-
export declare function ValidatorField(props: Omit<Props, 'registerField' | 'unregisterField' | 'customErrors'>): JSX.Element;
|
|
17
|
-
export {};
|
package/dist/validator-field.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ValidatorField = void 0;
|
|
4
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const context_1 = require("./context");
|
|
7
|
-
const validator_1 = require("./validator");
|
|
8
|
-
class ValidationFieldWrapper extends react_1.Component {
|
|
9
|
-
componentWillUnmount() {
|
|
10
|
-
this.props.unregisterField(this);
|
|
11
|
-
}
|
|
12
|
-
componentDidMount() {
|
|
13
|
-
this.props.registerField(this);
|
|
14
|
-
}
|
|
15
|
-
validate() {
|
|
16
|
-
const props = this.props;
|
|
17
|
-
const customError = (props.customErrors || []).find((item) => item.id === props.id);
|
|
18
|
-
if (customError) {
|
|
19
|
-
return customError;
|
|
20
|
-
}
|
|
21
|
-
const field = new validator_1.Field({
|
|
22
|
-
rules: props.rules,
|
|
23
|
-
required: props.required,
|
|
24
|
-
value: props.value,
|
|
25
|
-
id: props.id,
|
|
26
|
-
});
|
|
27
|
-
return field.validate();
|
|
28
|
-
}
|
|
29
|
-
render() {
|
|
30
|
-
const { children, value } = this.props;
|
|
31
|
-
const validity = this.validate();
|
|
32
|
-
return typeof children === 'function' ? children(validity, value) : children;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function ValidatorField(props) {
|
|
36
|
-
return ((0, jsx_runtime_1.jsx)(context_1.Context.Consumer, { children: (data) => ((0, jsx_runtime_1.jsx)(ValidationFieldWrapper, Object.assign({}, props, { customErrors: data === null || data === void 0 ? void 0 : data.customErrors, registerField: data === null || data === void 0 ? void 0 : data.registerField, unregisterField: data === null || data === void 0 ? void 0 : data.unregisterField }))) }));
|
|
37
|
-
}
|
|
38
|
-
exports.ValidatorField = ValidatorField;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Component, ReactNode, RefObject } from 'react';
|
|
2
|
-
import { Validity } from './types';
|
|
3
|
-
import { Field } from './validator';
|
|
4
|
-
interface ComponentProps {
|
|
5
|
-
children?: ReactNode;
|
|
6
|
-
stopAtFirstError?: boolean;
|
|
7
|
-
ref?: RefObject<any>;
|
|
8
|
-
}
|
|
9
|
-
export declare class ValidatorWrapper extends Component<ComponentProps> {
|
|
10
|
-
fields: any[];
|
|
11
|
-
state: {
|
|
12
|
-
customErrors: any[];
|
|
13
|
-
};
|
|
14
|
-
constructor(props: any, ctx: any);
|
|
15
|
-
componentWillUnmount(): void;
|
|
16
|
-
registerField(field: any): void;
|
|
17
|
-
unregisterField(field: any): void;
|
|
18
|
-
getField(id: any): Field | null;
|
|
19
|
-
setCustomError(customError: Validity): void;
|
|
20
|
-
clearCustomErrors(): void;
|
|
21
|
-
validate(): Validity;
|
|
22
|
-
render(): JSX.Element;
|
|
23
|
-
}
|
|
24
|
-
export {};
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ValidatorWrapper = void 0;
|
|
4
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const context_1 = require("./context");
|
|
7
|
-
const validator_1 = require("./validator");
|
|
8
|
-
class ValidatorWrapper extends react_1.Component {
|
|
9
|
-
constructor(props, ctx) {
|
|
10
|
-
super(props, ctx);
|
|
11
|
-
this.fields = [];
|
|
12
|
-
this.state = {
|
|
13
|
-
customErrors: [],
|
|
14
|
-
};
|
|
15
|
-
this.registerField = this.registerField.bind(this);
|
|
16
|
-
this.unregisterField = this.unregisterField.bind(this);
|
|
17
|
-
}
|
|
18
|
-
componentWillUnmount() {
|
|
19
|
-
this.fields = [];
|
|
20
|
-
}
|
|
21
|
-
registerField(field) {
|
|
22
|
-
if (field && !this.fields.includes(field)) {
|
|
23
|
-
this.fields.push(field);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
unregisterField(field) {
|
|
27
|
-
const index = this.fields.indexOf(field);
|
|
28
|
-
if (index > -1)
|
|
29
|
-
this.fields.splice(index, 1);
|
|
30
|
-
}
|
|
31
|
-
getField(id) {
|
|
32
|
-
return this.fields.find((field) => field.props.id === id) || null;
|
|
33
|
-
}
|
|
34
|
-
setCustomError(customError) {
|
|
35
|
-
this.setState({
|
|
36
|
-
customErrors: [...this.state.customErrors, customError],
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
clearCustomErrors() {
|
|
40
|
-
this.setState({ customErrors: [] });
|
|
41
|
-
}
|
|
42
|
-
validate() {
|
|
43
|
-
const validator = new validator_1.Validator({ stopAtFirstError: this.props.stopAtFirstError });
|
|
44
|
-
this.fields.forEach((comp) => {
|
|
45
|
-
validator.addField(comp.props);
|
|
46
|
-
});
|
|
47
|
-
return validator.validate();
|
|
48
|
-
}
|
|
49
|
-
render() {
|
|
50
|
-
return ((0, jsx_runtime_1.jsx)(context_1.Context.Provider, Object.assign({ value: {
|
|
51
|
-
customErrors: this.state.customErrors,
|
|
52
|
-
registerField: this.registerField,
|
|
53
|
-
unregisterField: this.unregisterField,
|
|
54
|
-
} }, { children: this.props.children })));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.ValidatorWrapper = ValidatorWrapper;
|
package/dist/validator.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { FieldParams, Validity } from './types';
|
|
2
|
-
export declare class Field {
|
|
3
|
-
private rules;
|
|
4
|
-
private required;
|
|
5
|
-
private value;
|
|
6
|
-
id: string | number;
|
|
7
|
-
constructor({ rules, required, value, id }: FieldParams);
|
|
8
|
-
validate(): Validity;
|
|
9
|
-
}
|
|
10
|
-
export interface ValidatorParams {
|
|
11
|
-
stopAtFirstError: boolean;
|
|
12
|
-
}
|
|
13
|
-
export declare class Validator {
|
|
14
|
-
private fields;
|
|
15
|
-
private params;
|
|
16
|
-
constructor(params?: ValidatorParams);
|
|
17
|
-
addField(params: FieldParams): Field;
|
|
18
|
-
removeField(field: Field): void;
|
|
19
|
-
getField(id: Field['id']): Field;
|
|
20
|
-
validate(): Validity;
|
|
21
|
-
}
|
package/dist/validator.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Validator = exports.Field = void 0;
|
|
4
|
-
class Field {
|
|
5
|
-
constructor({ rules, required, value, id }) {
|
|
6
|
-
this.rules = rules;
|
|
7
|
-
this.required = required;
|
|
8
|
-
this.value = value;
|
|
9
|
-
this.id = id;
|
|
10
|
-
}
|
|
11
|
-
validate() {
|
|
12
|
-
let isValid = true;
|
|
13
|
-
let message = '';
|
|
14
|
-
const { rules, value, required, id } = this;
|
|
15
|
-
const isEmptyValue = !value && parseFloat(value) !== 0;
|
|
16
|
-
if (!rules.length || (isEmptyValue && required === false)) {
|
|
17
|
-
return { isValid, message, id };
|
|
18
|
-
}
|
|
19
|
-
rules.forEach((instance) => {
|
|
20
|
-
if (isValid) {
|
|
21
|
-
isValid = instance.rule(value);
|
|
22
|
-
if (!isValid) {
|
|
23
|
-
if (typeof instance.message === 'function') {
|
|
24
|
-
message = instance.message(value);
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
message = instance.message;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
return { isValid, message, id };
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.Field = Field;
|
|
36
|
-
class Validator {
|
|
37
|
-
constructor(params) {
|
|
38
|
-
this.params = params || null;
|
|
39
|
-
this.fields = [];
|
|
40
|
-
}
|
|
41
|
-
addField(params) {
|
|
42
|
-
const field = new Field(params);
|
|
43
|
-
this.fields.push(field);
|
|
44
|
-
return field;
|
|
45
|
-
}
|
|
46
|
-
removeField(field) {
|
|
47
|
-
const index = this.fields.indexOf(field);
|
|
48
|
-
if (index > -1)
|
|
49
|
-
this.fields.splice(index, 1);
|
|
50
|
-
}
|
|
51
|
-
getField(id) {
|
|
52
|
-
return this.fields.find((field) => field.id === id) || null;
|
|
53
|
-
}
|
|
54
|
-
validate() {
|
|
55
|
-
let prevResult;
|
|
56
|
-
const statuses = this.fields.map((field) => {
|
|
57
|
-
var _a;
|
|
58
|
-
if (((_a = this.params) === null || _a === void 0 ? void 0 : _a.stopAtFirstError) && prevResult && prevResult.isValid === false) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
prevResult = field.validate();
|
|
62
|
-
return prevResult;
|
|
63
|
-
});
|
|
64
|
-
const errors = statuses.filter((inst) => inst && inst.isValid === false);
|
|
65
|
-
if (errors.length) {
|
|
66
|
-
const { isValid, message } = errors[0];
|
|
67
|
-
return { isValid, message, errors };
|
|
68
|
-
}
|
|
69
|
-
return { isValid: true, message: '' };
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
exports.Validator = Validator;
|