@coxy/react-validator 3.0.0 → 5.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/biome.json +4 -2
- package/dist/index.js +206 -2
- package/example/example.tsx +19 -51
- package/example/index.html +2 -7
- package/example/tsconfig.json +6 -6
- package/package.json +14 -14
- package/src/context.ts +8 -4
- package/src/index.test.ts +19 -4
- package/src/index.ts +4 -3
- package/src/rules.test.ts +16 -106
- package/src/rules.ts +5 -66
- package/src/types.ts +2 -8
- package/src/use-validator.test.tsx +4 -5
- package/src/use-validator.ts +1 -1
- package/src/validator-field.test.tsx +27 -22
- package/src/validator-field.tsx +37 -54
- package/src/validator-wrapper.test.tsx +36 -18
- package/src/validator-wrapper.tsx +45 -58
- package/src/validator.test.tsx +11 -1
- package/src/validator.ts +25 -20
- package/dist/index.d.mts +0 -116
- package/dist/index.d.ts +0 -116
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +0 -1
- package/src/jest.d.ts +0 -2
- package/tsup.config.ts +0 -14
package/biome.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
|
+
"files": {
|
|
3
|
+
"includes": ["src/**/*", "example/example.tsx"]
|
|
4
|
+
},
|
|
2
5
|
"formatter": {
|
|
3
6
|
"enabled": true,
|
|
4
7
|
"indentStyle": "space",
|
|
5
8
|
"indentWidth": 2,
|
|
6
9
|
"lineWidth": 120,
|
|
7
|
-
"formatWithErrors": false
|
|
8
|
-
"ignore": ["**/*.min.js"]
|
|
10
|
+
"formatWithErrors": false
|
|
9
11
|
},
|
|
10
12
|
"javascript": {
|
|
11
13
|
"formatter": {
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,206 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
Validator: () => Validator,
|
|
23
|
+
ValidatorField: () => ValidatorField,
|
|
24
|
+
ValidatorWrapper: () => ValidatorWrapper,
|
|
25
|
+
rules: () => rules,
|
|
26
|
+
useValidator: () => useValidator
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/rules.ts
|
|
31
|
+
var import_zod = require("zod");
|
|
32
|
+
var rules = {
|
|
33
|
+
notEmpty: [import_zod.z.string().min(1, { error: "Field is required" })],
|
|
34
|
+
isTrue: [import_zod.z.boolean({ error: "Value is required" }).and(import_zod.z.literal(true))],
|
|
35
|
+
email: [import_zod.z.string().min(1, { error: "Email is required" }), import_zod.z.email({ message: "Email is invalid" })]
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// src/validator.ts
|
|
39
|
+
var Field = class {
|
|
40
|
+
rules;
|
|
41
|
+
required;
|
|
42
|
+
value;
|
|
43
|
+
id;
|
|
44
|
+
constructor({ rules: rules2, required, value, id }) {
|
|
45
|
+
this.rules = rules2;
|
|
46
|
+
this.required = required;
|
|
47
|
+
this.value = value;
|
|
48
|
+
this.id = id;
|
|
49
|
+
}
|
|
50
|
+
validate() {
|
|
51
|
+
let isValid = true;
|
|
52
|
+
let message = "";
|
|
53
|
+
const { value, required, id } = this;
|
|
54
|
+
let result = { success: true, data: value };
|
|
55
|
+
const isEmptyValue = !value && Number.parseFloat(value) !== 0;
|
|
56
|
+
const rules2 = Array.isArray(this.rules) ? this.rules : [this.rules];
|
|
57
|
+
if (!rules2.length || isEmptyValue && required === false) {
|
|
58
|
+
return {
|
|
59
|
+
isValid,
|
|
60
|
+
message,
|
|
61
|
+
id,
|
|
62
|
+
result: { success: true, data: value }
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
for (const ruleItem of rules2) {
|
|
66
|
+
if (isValid && ruleItem && "safeParse" in ruleItem) {
|
|
67
|
+
result = ruleItem.safeParse(value);
|
|
68
|
+
isValid = result.success;
|
|
69
|
+
if (!isValid && result.error) {
|
|
70
|
+
message = result.error.issues[0]?.message || "Validation error";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { isValid, message, id, result };
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
var Validator = class {
|
|
78
|
+
fields;
|
|
79
|
+
params;
|
|
80
|
+
constructor(params) {
|
|
81
|
+
this.params = params || null;
|
|
82
|
+
this.fields = [];
|
|
83
|
+
}
|
|
84
|
+
addField(params) {
|
|
85
|
+
const field = new Field(params);
|
|
86
|
+
this.fields.push(field);
|
|
87
|
+
return field;
|
|
88
|
+
}
|
|
89
|
+
removeField(field) {
|
|
90
|
+
const index = this.fields.indexOf(field);
|
|
91
|
+
if (index > -1) this.fields.splice(index, 1);
|
|
92
|
+
}
|
|
93
|
+
getField(id) {
|
|
94
|
+
return this.fields.find((field) => field.id === id) || null;
|
|
95
|
+
}
|
|
96
|
+
validate() {
|
|
97
|
+
let prevResult;
|
|
98
|
+
const statuses = this.fields.map((field) => {
|
|
99
|
+
if (this.params?.stopAtFirstError && prevResult && prevResult.isValid === false) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
prevResult = field.validate();
|
|
103
|
+
return prevResult;
|
|
104
|
+
});
|
|
105
|
+
const results = statuses.filter((inst) => inst && inst.isValid === false);
|
|
106
|
+
if (results.length) {
|
|
107
|
+
return results[0];
|
|
108
|
+
}
|
|
109
|
+
return { isValid: true, message: "", result: results[0]?.result };
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// src/use-validator.ts
|
|
114
|
+
function useValidator(value, rules2) {
|
|
115
|
+
const validator = new Validator();
|
|
116
|
+
validator.addField({ value, rules: rules2 });
|
|
117
|
+
const { isValid, ...validateObject } = validator.validate();
|
|
118
|
+
return [isValid, validateObject];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/validator-field.tsx
|
|
122
|
+
var import_react2 = require("react");
|
|
123
|
+
|
|
124
|
+
// src/context.ts
|
|
125
|
+
var import_react = require("react");
|
|
126
|
+
var Context = (0, import_react.createContext)(null);
|
|
127
|
+
|
|
128
|
+
// src/validator-field.tsx
|
|
129
|
+
var ValidatorField = (0, import_react2.forwardRef)(function ValidatorField2(props, _ref) {
|
|
130
|
+
const { children, value } = props;
|
|
131
|
+
const { registerField, unregisterField } = (0, import_react2.useContext)(Context);
|
|
132
|
+
const propsRef = (0, import_react2.useRef)(props);
|
|
133
|
+
propsRef.current = props;
|
|
134
|
+
const handleRef = (0, import_react2.useRef)(null);
|
|
135
|
+
if (!handleRef.current) {
|
|
136
|
+
handleRef.current = {
|
|
137
|
+
get props() {
|
|
138
|
+
return propsRef.current;
|
|
139
|
+
},
|
|
140
|
+
validate: () => {
|
|
141
|
+
const curr = propsRef.current;
|
|
142
|
+
const field = new Field({
|
|
143
|
+
rules: curr.rules,
|
|
144
|
+
required: curr.required,
|
|
145
|
+
value: curr.value,
|
|
146
|
+
id: curr.id
|
|
147
|
+
});
|
|
148
|
+
return field.validate();
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
(0, import_react2.useEffect)(() => {
|
|
153
|
+
registerField(handleRef.current);
|
|
154
|
+
return () => {
|
|
155
|
+
unregisterField(handleRef.current);
|
|
156
|
+
};
|
|
157
|
+
}, [registerField, unregisterField]);
|
|
158
|
+
const validity = handleRef.current.validate();
|
|
159
|
+
return typeof children === "function" ? children(validity, value) : children;
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// src/validator-wrapper.tsx
|
|
163
|
+
var import_react3 = require("react");
|
|
164
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
165
|
+
var ValidatorWrapper = (0, import_react3.forwardRef)(function ValidatorWrapper2({ children, stopAtFirstError }, ref) {
|
|
166
|
+
const fieldsRef = (0, import_react3.useRef)([]);
|
|
167
|
+
const registerField = (0, import_react3.useCallback)((field) => {
|
|
168
|
+
if (field && !fieldsRef.current.includes(field)) {
|
|
169
|
+
fieldsRef.current.push(field);
|
|
170
|
+
}
|
|
171
|
+
}, []);
|
|
172
|
+
const unregisterField = (0, import_react3.useCallback)((field) => {
|
|
173
|
+
const index = fieldsRef.current.indexOf(field);
|
|
174
|
+
if (index > -1) fieldsRef.current.splice(index, 1);
|
|
175
|
+
}, []);
|
|
176
|
+
const getField = (0, import_react3.useCallback)((id) => {
|
|
177
|
+
return fieldsRef.current.find((field) => field?.props?.id === id) || null;
|
|
178
|
+
}, []);
|
|
179
|
+
const validate = (0, import_react3.useCallback)(() => {
|
|
180
|
+
const validator = new Validator({ stopAtFirstError });
|
|
181
|
+
for (const comp of fieldsRef.current) {
|
|
182
|
+
validator.addField(comp.props);
|
|
183
|
+
}
|
|
184
|
+
return validator.validate();
|
|
185
|
+
}, [stopAtFirstError]);
|
|
186
|
+
(0, import_react3.useImperativeHandle)(
|
|
187
|
+
ref,
|
|
188
|
+
() => ({
|
|
189
|
+
validate,
|
|
190
|
+
getField,
|
|
191
|
+
registerField,
|
|
192
|
+
unregisterField
|
|
193
|
+
}),
|
|
194
|
+
[validate, getField, registerField, unregisterField]
|
|
195
|
+
);
|
|
196
|
+
const contextValue = (0, import_react3.useMemo)(() => ({ registerField, unregisterField }), [registerField, unregisterField]);
|
|
197
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value: contextValue, children });
|
|
198
|
+
});
|
|
199
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
200
|
+
0 && (module.exports = {
|
|
201
|
+
Validator,
|
|
202
|
+
ValidatorField,
|
|
203
|
+
ValidatorWrapper,
|
|
204
|
+
rules,
|
|
205
|
+
useValidator
|
|
206
|
+
});
|
package/example/example.tsx
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import { createRef, useState } from 'react'
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import { Validator, ValidatorField, ValidatorWrapper, rules } from '../dist/index'
|
|
1
|
+
// biome-ignore lint/correctness/noUnusedImports: <need>
|
|
2
|
+
import React, { createRef, useState } from 'react'
|
|
3
|
+
import ReactDOM from 'react-dom/client'
|
|
4
|
+
import { rules, ValidatorField, ValidatorWrapper } from '../dist/index'
|
|
6
5
|
|
|
7
6
|
function App() {
|
|
8
7
|
const [email, setEmail] = useState('')
|
|
@@ -17,55 +16,24 @@ function App() {
|
|
|
17
16
|
console.log('success')
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
function handleValidateCustomFields() {
|
|
21
|
-
const validator = new Validator({ stopAtFirstError: true })
|
|
22
|
-
const fieldPassword = validator.addField({
|
|
23
|
-
rules: rules.password,
|
|
24
|
-
value: '',
|
|
25
|
-
id: 'for-remove',
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
const fieldSearchPassword = validator.getField('for-remove')
|
|
29
|
-
console.log(fieldPassword === fieldSearchPassword)
|
|
30
|
-
|
|
31
|
-
validator.removeField(fieldPassword)
|
|
32
|
-
|
|
33
|
-
validator.addField({
|
|
34
|
-
rules: rules.password,
|
|
35
|
-
value: 'testpassword',
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
const { isValid, message, errors } = validator.validate()
|
|
39
|
-
if (!isValid) {
|
|
40
|
-
console.log(isValid, message, errors)
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
console.log('success')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
19
|
return (
|
|
47
|
-
|
|
48
|
-
<
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
<button onClick={handleValidateCustomFields} type="button">
|
|
62
|
-
Validate custom fields
|
|
63
|
-
</button>
|
|
64
|
-
</ValidatorWrapper>
|
|
65
|
-
</>
|
|
20
|
+
<ValidatorWrapper ref={jsxValidator}>
|
|
21
|
+
<ValidatorField rules={rules.email} value={email}>
|
|
22
|
+
{({ isValid, message }) => (
|
|
23
|
+
<>
|
|
24
|
+
<input onChange={({ target: { value } }) => setEmail(value)} />
|
|
25
|
+
<div>{isValid ? 'valid' : 'invalid'}</div>
|
|
26
|
+
<div>{message || ''}</div>
|
|
27
|
+
<button onClick={handleValidateEmail} type="button">
|
|
28
|
+
Validate email
|
|
29
|
+
</button>
|
|
30
|
+
</>
|
|
31
|
+
)}
|
|
32
|
+
</ValidatorField>
|
|
33
|
+
</ValidatorWrapper>
|
|
66
34
|
)
|
|
67
35
|
}
|
|
68
36
|
|
|
69
|
-
const root = createRoot(document.getElementById('root'))
|
|
37
|
+
const root = ReactDOM.createRoot(document.getElementById('root'))
|
|
70
38
|
|
|
71
39
|
root.render(<App />)
|
package/example/index.html
CHANGED
|
@@ -3,14 +3,9 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<title>Test template</title>
|
|
6
|
-
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
|
|
7
|
-
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
|
|
8
6
|
</head>
|
|
9
7
|
<body>
|
|
10
|
-
|
|
11
|
-
<
|
|
12
|
-
|
|
13
|
-
<script src="./dist/example.js"></script>
|
|
14
|
-
|
|
8
|
+
<div id="root"></div>
|
|
9
|
+
<script type="module" src="./dist/example.js"></script>
|
|
15
10
|
</body>
|
|
16
11
|
</html>
|
package/example/tsconfig.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"extends": "../tsconfig.json",
|
|
3
2
|
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ES2022",
|
|
5
|
+
"jsx": "react",
|
|
4
6
|
"outDir": "./dist",
|
|
5
|
-
"
|
|
6
|
-
"baseUrl": "./"
|
|
7
|
+
"allowSyntheticDefaultImports": true
|
|
7
8
|
},
|
|
8
|
-
"include": ["example.tsx"]
|
|
9
|
-
|
|
10
|
-
}
|
|
9
|
+
"include": ["example.tsx"]
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coxy/react-validator",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "🚀 Simple validation form for React or NodeJS apps. useValidator are included ;)",
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"description": "🚀 Simple validation form for React or NodeJS apps with zod. useValidator are included ;)",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -22,35 +22,35 @@
|
|
|
22
22
|
"author": "dsshard",
|
|
23
23
|
"scripts": {
|
|
24
24
|
"start": "tsup ./src/index.ts --watch",
|
|
25
|
-
"build": "tsup",
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
25
|
+
"build": "tsup ./src/index.ts",
|
|
26
|
+
"lint": "npx biome lint --fix",
|
|
27
|
+
"example": "NODE_ENV=production npx esbuild example/example.tsx --bundle --outfile=example/dist/example.js --minify --format=esm --target=es2020",
|
|
28
|
+
"coverage": "npx codecov",
|
|
29
|
+
"coveralls": "npx coveralls .",
|
|
29
30
|
"test": "jest"
|
|
30
31
|
},
|
|
31
32
|
"license": "MIT",
|
|
32
33
|
"peerDependencies": {
|
|
33
|
-
"react": ">=
|
|
34
|
+
"react": ">=18.x.x"
|
|
34
35
|
},
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"
|
|
37
|
+
"zod": "^3.25.0 || ^4.0.0"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
|
-
"@biomejs/biome": "2.1.2",
|
|
40
40
|
"@testing-library/dom": "10.4.1",
|
|
41
41
|
"@testing-library/react": "16.3.0",
|
|
42
42
|
"@types/jest": "30.0.0",
|
|
43
43
|
"@types/node": "24.1.0",
|
|
44
|
-
"@types/react": "19.1.
|
|
45
|
-
"
|
|
46
|
-
"coveralls": "3.1.1",
|
|
44
|
+
"@types/react": "19.1.11",
|
|
45
|
+
"@types/react-dom": "19.1.7",
|
|
47
46
|
"jest": "30.0.5",
|
|
48
47
|
"jest-environment-jsdom": "30.0.5",
|
|
49
|
-
"react
|
|
50
|
-
"
|
|
48
|
+
"react": ">=18.x.x",
|
|
49
|
+
"react-dom": ">=18.x.x",
|
|
51
50
|
"react-test-renderer": "19.1.1",
|
|
52
51
|
"ts-jest": "29.4.0",
|
|
53
52
|
"ts-node": "10.9.2",
|
|
53
|
+
"tsup": "8.5.0",
|
|
54
54
|
"typescript": "5.8.3"
|
|
55
55
|
}
|
|
56
56
|
}
|
package/src/context.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { createContext } from 'react'
|
|
2
2
|
|
|
3
|
-
import type { Validity } from './types'
|
|
3
|
+
import type { FieldParams, Validity } from './types'
|
|
4
|
+
|
|
5
|
+
export interface RegisteredFieldHandle {
|
|
6
|
+
props: FieldParams
|
|
7
|
+
validate: () => Validity
|
|
8
|
+
}
|
|
4
9
|
|
|
5
10
|
export const Context = createContext<{
|
|
6
|
-
registerField: (field:
|
|
7
|
-
unregisterField: (field:
|
|
8
|
-
customErrors: Array<Validity>
|
|
11
|
+
registerField: (field: RegisteredFieldHandle) => void
|
|
12
|
+
unregisterField: (field: RegisteredFieldHandle) => void
|
|
9
13
|
}>(null)
|
package/src/index.test.ts
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rules, useValidator, Validator, ValidatorField, ValidatorWrapper } from './index'
|
|
2
2
|
|
|
3
|
-
it('
|
|
4
|
-
expect(typeof ValidatorWrapper).toBe('
|
|
5
|
-
expect(typeof ValidatorField).toBe('
|
|
3
|
+
it('exports surface is available', () => {
|
|
4
|
+
expect(typeof ValidatorWrapper).toBe('object')
|
|
5
|
+
expect(typeof ValidatorField).toBe('object')
|
|
6
6
|
expect(typeof rules).toBe('object')
|
|
7
|
+
expect(typeof Validator).toBe('function')
|
|
8
|
+
expect(typeof useValidator).toBe('function')
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('use exports to execute a basic validation flow', () => {
|
|
12
|
+
// use Validator (class)
|
|
13
|
+
const validator = new Validator()
|
|
14
|
+
validator.addField({ value: 'test@example.com', rules: rules.email })
|
|
15
|
+
const res = validator.validate()
|
|
16
|
+
expect(res.isValid).toBe(true)
|
|
17
|
+
|
|
18
|
+
// use useValidator (hook-like util function)
|
|
19
|
+
const [isValid, { message }] = useValidator('bad-email', rules.email)
|
|
20
|
+
expect(isValid).toBe(false)
|
|
21
|
+
expect(message).toBe('Email is invalid')
|
|
7
22
|
})
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { rules } from './rules'
|
|
2
|
+
|
|
3
|
+
export type { ErrorMessage, FieldParams, Validity } from './types'
|
|
4
|
+
export { useValidator } from './use-validator'
|
|
5
|
+
export { Validator } from './validator'
|
|
2
6
|
export { ValidatorField } from './validator-field'
|
|
3
7
|
export { ValidatorWrapper } from './validator-wrapper'
|
|
4
|
-
export { Validator } from './validator'
|
|
5
|
-
export { useValidator } from './use-validator'
|
|
6
|
-
export * from './types'
|
package/src/rules.test.ts
CHANGED
|
@@ -1,127 +1,37 @@
|
|
|
1
|
+
import type { ZodSafeParseResult } from 'zod'
|
|
1
2
|
import { rules } from './rules'
|
|
2
3
|
|
|
3
4
|
it('check rule email', () => {
|
|
4
5
|
expect(rules.email.length).toBe(2)
|
|
5
6
|
|
|
6
|
-
let result:
|
|
7
|
+
let result: ZodSafeParseResult<string>
|
|
7
8
|
// email.length > 0
|
|
8
|
-
result = rules.email[0].
|
|
9
|
-
expect(result).toBe(true)
|
|
9
|
+
result = rules.email[0].safeParse('test')
|
|
10
|
+
expect(result.success).toBe(true)
|
|
10
11
|
|
|
11
12
|
// email check regexp
|
|
12
|
-
result = rules.email[1].
|
|
13
|
-
expect(result).toBe(false)
|
|
13
|
+
result = rules.email[1].safeParse('test')
|
|
14
|
+
expect(result.success).toBe(false)
|
|
14
15
|
|
|
15
16
|
// email check regexp
|
|
16
|
-
result = rules.email[1].
|
|
17
|
-
expect(result).toBe(true)
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
it('check rule password', () => {
|
|
21
|
-
expect(rules.password.length).toBe(2)
|
|
22
|
-
|
|
23
|
-
let result: boolean
|
|
24
|
-
// password.length > 0
|
|
25
|
-
result = rules.password[0].rule('test')
|
|
26
|
-
expect(result).toBe(true)
|
|
27
|
-
|
|
28
|
-
// password.length > 5
|
|
29
|
-
result = rules.password[1].rule('test')
|
|
30
|
-
expect(result).toBe(false)
|
|
17
|
+
result = rules.email[1].safeParse('test@gmail.com')
|
|
18
|
+
expect(result.success).toBe(true)
|
|
31
19
|
})
|
|
32
20
|
|
|
33
21
|
it('check rule bool', () => {
|
|
34
|
-
expect(rules.
|
|
22
|
+
expect(rules.isTrue.length).toBe(1)
|
|
35
23
|
|
|
36
|
-
const result = rules.
|
|
37
|
-
expect(result).toBe(true)
|
|
24
|
+
const result = rules.isTrue[0].safeParse(true)
|
|
25
|
+
expect(result.success).toBe(true)
|
|
38
26
|
})
|
|
39
27
|
|
|
40
28
|
it('check rule notEmpty', () => {
|
|
41
29
|
expect(rules.notEmpty.length).toBe(1)
|
|
42
30
|
|
|
43
|
-
let result:
|
|
44
|
-
result = rules.notEmpty[0].
|
|
45
|
-
expect(result).toBe(false)
|
|
46
|
-
|
|
47
|
-
result = rules.notEmpty[0].rule('test')
|
|
48
|
-
expect(result).toBe(true)
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
it('check rule min', () => {
|
|
52
|
-
expect(rules.min(1).length).toBe(1)
|
|
53
|
-
expect(typeof rules.min).toBe('function')
|
|
54
|
-
|
|
55
|
-
let result: boolean
|
|
56
|
-
result = rules.min(10)[0].rule('')
|
|
57
|
-
expect(result).toBe(false)
|
|
58
|
-
|
|
59
|
-
result = rules.min(9)[0].rule('testtesttest')
|
|
60
|
-
expect(result).toBe(false)
|
|
61
|
-
|
|
62
|
-
result = rules.min(9)[0].rule('11')
|
|
63
|
-
expect(result).toBe(true)
|
|
64
|
-
|
|
65
|
-
result = rules.min(9)[0].rule(10)
|
|
66
|
-
expect(result).toBe(true)
|
|
67
|
-
|
|
68
|
-
result = rules.min(9)[0].rule('8')
|
|
69
|
-
expect(result).toBe(false)
|
|
70
|
-
|
|
71
|
-
result = rules.min(9)[0].rule(7)
|
|
72
|
-
expect(result).toBe(false)
|
|
73
|
-
|
|
74
|
-
result = rules.min(-1)[0].rule('')
|
|
75
|
-
expect(result).toBe(false)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('check rule max', () => {
|
|
79
|
-
let result: boolean
|
|
80
|
-
result = rules.max(10)[0].rule('')
|
|
81
|
-
expect(result).toBe(false)
|
|
82
|
-
|
|
83
|
-
result = rules.max(9)[0].rule('testtesttest')
|
|
84
|
-
expect(result).toBe(false)
|
|
85
|
-
|
|
86
|
-
result = rules.max(9)[0].rule('11')
|
|
87
|
-
expect(result).toBe(false)
|
|
88
|
-
|
|
89
|
-
result = rules.max(9)[0].rule(10)
|
|
90
|
-
expect(result).toBe(false)
|
|
91
|
-
|
|
92
|
-
result = rules.max(9)[0].rule('5')
|
|
93
|
-
expect(result).toBe(true)
|
|
94
|
-
|
|
95
|
-
result = rules.max(9)[0].rule(5)
|
|
96
|
-
expect(result).toBe(true)
|
|
97
|
-
|
|
98
|
-
result = rules.max(-1)[0].rule('')
|
|
99
|
-
expect(result).toBe(false)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it('check rule length', () => {
|
|
103
|
-
let result: boolean
|
|
104
|
-
result = rules.length(1)[0].rule('')
|
|
105
|
-
expect(result).toBe(false)
|
|
106
|
-
|
|
107
|
-
result = rules.length(1)[0].rule('1')
|
|
108
|
-
expect(result).toBe(true)
|
|
109
|
-
|
|
110
|
-
result = rules.length(1, 10)[0].rule('testtesttest')
|
|
111
|
-
expect(result).toBe(true)
|
|
112
|
-
|
|
113
|
-
result = rules.length(1, 10)[1].rule('testtesttest')
|
|
114
|
-
expect(result).toBe(false)
|
|
115
|
-
|
|
116
|
-
result = rules.length(1, 10)[0].rule('lol')
|
|
117
|
-
expect(result).toBe(true)
|
|
118
|
-
|
|
119
|
-
result = rules.length(1, 10)[1].rule('lol')
|
|
120
|
-
expect(result).toBe(true)
|
|
121
|
-
|
|
122
|
-
result = rules.length(1)[1].rule('test undefined 2 param')
|
|
123
|
-
expect(result).toBe(true)
|
|
31
|
+
let result: ZodSafeParseResult<string>
|
|
32
|
+
result = rules.notEmpty[0].safeParse('')
|
|
33
|
+
expect(result.success).toBe(false)
|
|
124
34
|
|
|
125
|
-
result = rules.
|
|
126
|
-
expect(result).toBe(
|
|
35
|
+
result = rules.notEmpty[0].safeParse('test')
|
|
36
|
+
expect(result.success).toBe(true)
|
|
127
37
|
})
|