@formvk/validator 0.1.0 → 1.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/CHANGELOG.md +12 -0
- package/README.md +64 -0
- package/package.json +6 -2
- package/__tests__/parser.spec.ts +0 -17
- package/__tests__/registry.spec.ts +0 -23
- package/__tests__/validator.spec.ts +0 -535
- package/src/formats.ts +0 -66
- package/src/index.ts +0 -4
- package/src/locale.ts +0 -219
- package/src/parser.ts +0 -136
- package/src/registry.ts +0 -108
- package/src/rules.ts +0 -147
- package/src/template.ts +0 -18
- package/src/types.ts +0 -105
- package/src/validator.ts +0 -36
- package/tsconfig.build.json +0 -8
- package/tsconfig.json +0 -4
- package/vite.config.ts +0 -18
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @formvk/validator
|
|
2
|
+
|
|
3
|
+
Standalone validation engine used by Formvk, with a rule registry, format validators, localized messages and validator description parsing.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @formvk/validator
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { validate } from '@formvk/validator'
|
|
15
|
+
|
|
16
|
+
const results = await validate('alice', [
|
|
17
|
+
{ required: true, message: 'Username is required' },
|
|
18
|
+
{ minLength: 3, maxLength: 20, message: 'Use 3–20 characters' },
|
|
19
|
+
])
|
|
20
|
+
|
|
21
|
+
results.error.length === 0 // true
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Register custom rules and formats:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { registerValidateFormats, registerValidateRules } from '@formvk/validator'
|
|
28
|
+
|
|
29
|
+
registerValidateRules({
|
|
30
|
+
isEven: value => (typeof value === 'number' && value % 2 === 0 ? '' : 'Must be an even number'),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
registerValidateFormats({
|
|
34
|
+
hex: /^#[0-9a-f]{6}$/i,
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Switch the built-in language (`en`, `zh`, `ja`):
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { setValidateLanguage } from '@formvk/validator'
|
|
42
|
+
|
|
43
|
+
setValidateLanguage('zh')
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## API
|
|
47
|
+
|
|
48
|
+
- **Validate** — `validate(value, validator, options?)` → `{ error, success, warning }`.
|
|
49
|
+
- **Parse** — `parseValidator`, `parseValidatorRules`, `parseValidatorDescription`, `parseValidatorDescriptions`.
|
|
50
|
+
- **Registry** — `registerValidateRules`, `registerValidateFormats`, `registerValidateLocale`, `registerValidateMessageTemplateEngine`, `getValidateRules`, `getValidateFormats`, `getValidateLocale`.
|
|
51
|
+
- **Language** — `setValidateLanguage`, `getValidateLanguage`, `getLocaleByPath`.
|
|
52
|
+
- **Built-in rules** — `required`, `max`, `min`, `exclusiveMaximum`, `exclusiveMinimum`, `len`, `pattern`, `whitespace`, `enum`, `const`, `multipleOf`, `uniqueItems`, `maxProperties`, `minProperties`, plus aliases such as `maxLength`, `minLength`, `maxItems`, `minItems`, `maximum`, `minimum`.
|
|
53
|
+
- **Built-in formats** — `url`, `email`, `number`, `integer`, `qq`, `phone`, `idcard`, `money`, `zh`, `date`, `zip`.
|
|
54
|
+
|
|
55
|
+
`@formvk/schema` accepts these rules directly on schema fields through `validator`.
|
|
56
|
+
|
|
57
|
+
## Documentation
|
|
58
|
+
|
|
59
|
+
- Validator API: https://formvk.github.io/core/api/validator
|
|
60
|
+
- Validation guide: https://formvk.github.io/core/guide/validation
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formvk/validator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"CHANGELOG.md"
|
|
8
|
+
],
|
|
5
9
|
"type": "module",
|
|
6
10
|
"main": "dist/index.js",
|
|
7
11
|
"module": "dist/index.js",
|
|
@@ -10,7 +14,7 @@
|
|
|
10
14
|
"access": "public"
|
|
11
15
|
},
|
|
12
16
|
"dependencies": {
|
|
13
|
-
"@formvk/shared": "
|
|
17
|
+
"@formvk/shared": "~1.0.0"
|
|
14
18
|
},
|
|
15
19
|
"scripts": {
|
|
16
20
|
"build": "tsc --noEmit && vite build",
|
package/__tests__/parser.spec.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { parseValidatorDescriptions } from '../src/parser'
|
|
4
|
-
|
|
5
|
-
describe('parseValidatorDescriptions', () => {
|
|
6
|
-
test('basic', () => {
|
|
7
|
-
expect(parseValidatorDescriptions('date')).toEqual([{ format: 'date' }])
|
|
8
|
-
function validator() {
|
|
9
|
-
return ''
|
|
10
|
-
}
|
|
11
|
-
expect(parseValidatorDescriptions(validator)).toEqual([{ validator }])
|
|
12
|
-
expect(parseValidatorDescriptions(['date'])).toEqual([{ format: 'date' }])
|
|
13
|
-
expect(parseValidatorDescriptions([validator])).toEqual([{ validator }])
|
|
14
|
-
expect(parseValidatorDescriptions({ format: 'date' })).toEqual([{ format: 'date' }])
|
|
15
|
-
expect(parseValidatorDescriptions({ validator })).toEqual([{ validator }])
|
|
16
|
-
})
|
|
17
|
-
})
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { expect, test } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { getLocaleByPath, getValidateLocale, getValidateLocaleIOSCode, setValidateLanguage } from '../src'
|
|
4
|
-
import locale from '../src/locale'
|
|
5
|
-
|
|
6
|
-
test('getValidateLocaleIOSCode', () => {
|
|
7
|
-
expect(getValidateLocaleIOSCode('zh-CN')).toEqual('zh-CN')
|
|
8
|
-
expect(getValidateLocaleIOSCode('zh')).toEqual('zh')
|
|
9
|
-
expect(getValidateLocaleIOSCode('ZH')).toEqual('zh')
|
|
10
|
-
expect(getValidateLocaleIOSCode('cn')).toEqual('zh-CN')
|
|
11
|
-
expect(getValidateLocaleIOSCode('en')).toEqual('en')
|
|
12
|
-
expect(getValidateLocaleIOSCode('TW')).toEqual('zh-TW')
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
test('getLocaleByPath', () => {
|
|
16
|
-
expect(getLocaleByPath('pattern', 'vi')).toEqual(locale.en.pattern)
|
|
17
|
-
expect(getLocaleByPath('pattern')).toEqual(locale.en.pattern)
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test('getValidateLocale', () => {
|
|
21
|
-
setValidateLanguage('vi')
|
|
22
|
-
expect(getValidateLocale('pattern')).toEqual(locale.en.pattern)
|
|
23
|
-
})
|
|
@@ -1,535 +0,0 @@
|
|
|
1
|
-
import { expect, test } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
registerValidateFormats,
|
|
5
|
-
registerValidateMessageTemplateEngine,
|
|
6
|
-
registerValidateRules,
|
|
7
|
-
setValidateLanguage,
|
|
8
|
-
validate,
|
|
9
|
-
} from '../src'
|
|
10
|
-
|
|
11
|
-
registerValidateRules({
|
|
12
|
-
custom: value => (value === '123' ? 'custom error' : ''),
|
|
13
|
-
customBool: () => false,
|
|
14
|
-
customBool2: () => true,
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
registerValidateFormats({
|
|
18
|
-
custom: /^[\u4e00-\u9fa5]+$/,
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
function hasError(results: any, message?: string) {
|
|
22
|
-
if (!message) {
|
|
23
|
-
return expect(results?.error?.[0]).not.toBeUndefined()
|
|
24
|
-
}
|
|
25
|
-
return expect(results?.error?.[0]).toEqual(message)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function noError(results: any) {
|
|
29
|
-
return expect(results?.error?.[0]).toBeUndefined()
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
test('empty string validate', async () => {
|
|
33
|
-
const results = await validate('', { required: true })
|
|
34
|
-
expect(results).toEqual({
|
|
35
|
-
error: ['The field value is required'],
|
|
36
|
-
success: [],
|
|
37
|
-
warning: [],
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('empty array validate', async () => {
|
|
42
|
-
const results = await validate([], { required: true })
|
|
43
|
-
expect(results).toEqual({
|
|
44
|
-
error: ['The field value is required'],
|
|
45
|
-
success: [],
|
|
46
|
-
warning: [],
|
|
47
|
-
})
|
|
48
|
-
noError(await validate([''], { required: true }))
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
test('empty object validate', async () => {
|
|
52
|
-
const results = await validate({}, { required: true })
|
|
53
|
-
expect(results).toEqual({
|
|
54
|
-
error: ['The field value is required'],
|
|
55
|
-
success: [],
|
|
56
|
-
warning: [],
|
|
57
|
-
})
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('empty number validate', async () => {
|
|
61
|
-
const results = await validate(0, { required: true })
|
|
62
|
-
expect(results).toEqual({
|
|
63
|
-
error: [],
|
|
64
|
-
success: [],
|
|
65
|
-
warning: [],
|
|
66
|
-
})
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('multi validate', async () => {
|
|
70
|
-
const results = await validate('', {
|
|
71
|
-
required: true,
|
|
72
|
-
validator() {
|
|
73
|
-
return 'validate error'
|
|
74
|
-
},
|
|
75
|
-
})
|
|
76
|
-
expect(results).toEqual({
|
|
77
|
-
error: ['The field value is required', 'validate error'],
|
|
78
|
-
success: [],
|
|
79
|
-
warning: [],
|
|
80
|
-
})
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
test('message scope', async () => {
|
|
84
|
-
const results = await validate(
|
|
85
|
-
'',
|
|
86
|
-
{
|
|
87
|
-
required: true,
|
|
88
|
-
validator() {
|
|
89
|
-
return 'validate error {{name}}'
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
context: {
|
|
94
|
-
name: 'scopeName',
|
|
95
|
-
},
|
|
96
|
-
}
|
|
97
|
-
)
|
|
98
|
-
expect(results).toEqual({
|
|
99
|
-
error: ['The field value is required', 'validate error scopeName'],
|
|
100
|
-
success: [],
|
|
101
|
-
warning: [],
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test('first validate', async () => {
|
|
106
|
-
const results = await validate(
|
|
107
|
-
'',
|
|
108
|
-
{
|
|
109
|
-
required: true,
|
|
110
|
-
validator() {
|
|
111
|
-
return 'validate error'
|
|
112
|
-
},
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
validateFirst: true,
|
|
116
|
-
}
|
|
117
|
-
)
|
|
118
|
-
expect(results).toEqual({
|
|
119
|
-
error: ['The field value is required'],
|
|
120
|
-
success: [],
|
|
121
|
-
warning: [],
|
|
122
|
-
})
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
test('custom validate results', async () => {
|
|
126
|
-
const results = await validate('', {
|
|
127
|
-
validator() {
|
|
128
|
-
return { type: 'error', message: 'validate error' }
|
|
129
|
-
},
|
|
130
|
-
})
|
|
131
|
-
expect(results).toEqual({
|
|
132
|
-
error: ['validate error'],
|
|
133
|
-
success: [],
|
|
134
|
-
warning: [],
|
|
135
|
-
})
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
test('exception validate', async () => {
|
|
139
|
-
const results1 = await validate('', {
|
|
140
|
-
validator() {
|
|
141
|
-
throw new Error('validate error')
|
|
142
|
-
},
|
|
143
|
-
})
|
|
144
|
-
expect(results1).toEqual({
|
|
145
|
-
error: ['validate error'],
|
|
146
|
-
success: [],
|
|
147
|
-
warning: [],
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
const results2 = await validate('', {
|
|
151
|
-
validator() {
|
|
152
|
-
throw 'custom string'
|
|
153
|
-
},
|
|
154
|
-
})
|
|
155
|
-
expect(results2).toEqual({
|
|
156
|
-
error: ['custom string'],
|
|
157
|
-
success: [],
|
|
158
|
-
warning: [],
|
|
159
|
-
})
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
test('max/maxItems/maxLength/minItems/minLength/min/maximum/exclusiveMaximum/minimum/exclusiveMinimum/len', async () => {
|
|
163
|
-
hasError(await validate(6, { max: 5 }))
|
|
164
|
-
hasError(await validate(6, { maxLength: 5 }))
|
|
165
|
-
hasError(await validate(6, { maxItems: 5 }))
|
|
166
|
-
noError(await validate(5, { max: 5 }))
|
|
167
|
-
noError(await validate(5, { maxLength: 5 }))
|
|
168
|
-
noError(await validate(5, { maxItems: 5 }))
|
|
169
|
-
hasError(await validate([1, 2, 3, 4, 5, 6], { max: 5 }))
|
|
170
|
-
hasError(await validate([1, 2, 3, 4, 5, 6], { maxLength: 5 }))
|
|
171
|
-
hasError(await validate([1, 2, 3, 4, 5, 6], { maxItems: 5 }))
|
|
172
|
-
noError(await validate([1, 2, 3, 4, 5], { max: 5 }))
|
|
173
|
-
noError(await validate([1, 2, 3, 4, 5], { maxLength: 5 }))
|
|
174
|
-
noError(await validate([1, 2, 3, 4, 5], { maxItems: 5 }))
|
|
175
|
-
hasError(await validate('123456', { max: 5 }))
|
|
176
|
-
hasError(await validate('123456', { maxLength: 5 }))
|
|
177
|
-
hasError(await validate('123456', { maxItems: 5 }))
|
|
178
|
-
noError(await validate('12345', { max: 5 }))
|
|
179
|
-
noError(await validate('12345', { maxLength: 5 }))
|
|
180
|
-
noError(await validate('12345', { maxItems: 5 }))
|
|
181
|
-
hasError(await validate(2, { min: 3 }))
|
|
182
|
-
hasError(await validate(2, { minLength: 3 }))
|
|
183
|
-
hasError(await validate(2, { minItems: 3 }))
|
|
184
|
-
noError(await validate(3, { min: 3 }))
|
|
185
|
-
noError(await validate(3, { minLength: 3 }))
|
|
186
|
-
noError(await validate(3, { minItems: 3 }))
|
|
187
|
-
hasError(await validate([1, 2], { min: 3 }))
|
|
188
|
-
hasError(await validate([1, 2], { minLength: 3 }))
|
|
189
|
-
hasError(await validate([1, 2], { minItems: 3 }))
|
|
190
|
-
noError(await validate([1, 2, 3], { min: 3 }))
|
|
191
|
-
noError(await validate([1, 2, 3], { minLength: 3 }))
|
|
192
|
-
noError(await validate([1, 2, 3], { minItems: 3 }))
|
|
193
|
-
hasError(await validate('12', { min: 3 }))
|
|
194
|
-
hasError(await validate('12', { minLength: 3 }))
|
|
195
|
-
hasError(await validate('12', { minItems: 3 }))
|
|
196
|
-
noError(await validate('123', { min: 3 }))
|
|
197
|
-
noError(await validate('123', { minLength: 3 }))
|
|
198
|
-
noError(await validate('123', { minItems: 3 }))
|
|
199
|
-
|
|
200
|
-
hasError(await validate(6, { maximum: 5 }))
|
|
201
|
-
noError(await validate(5, { maximum: 5 }))
|
|
202
|
-
hasError(await validate([1, 2, 3, 4, 5, 6], { maximum: 5 }))
|
|
203
|
-
noError(await validate([1, 2, 3, 4, 5], { maximum: 5 }))
|
|
204
|
-
hasError(await validate('123456', { maximum: 5 }))
|
|
205
|
-
noError(await validate('12345', { maximum: 5 }))
|
|
206
|
-
hasError(await validate(2, { minimum: 3 }))
|
|
207
|
-
noError(await validate(3, { minimum: 3 }))
|
|
208
|
-
hasError(await validate([1, 2], { minimum: 3 }))
|
|
209
|
-
noError(await validate([1, 2, 3], { minimum: 3 }))
|
|
210
|
-
hasError(await validate('12', { minimum: 3 }))
|
|
211
|
-
noError(await validate('123', { minimum: 3 }))
|
|
212
|
-
|
|
213
|
-
hasError(await validate(6, { exclusiveMaximum: 5 }))
|
|
214
|
-
hasError(await validate(5, { exclusiveMaximum: 5 }))
|
|
215
|
-
hasError(await validate([1, 2, 3, 4, 5, 6], { exclusiveMaximum: 5 }))
|
|
216
|
-
hasError(await validate([1, 2, 3, 4, 5], { exclusiveMaximum: 5 }))
|
|
217
|
-
hasError(await validate('123456', { exclusiveMaximum: 5 }))
|
|
218
|
-
hasError(await validate('12345', { exclusiveMaximum: 5 }))
|
|
219
|
-
hasError(await validate(2, { exclusiveMinimum: 3 }))
|
|
220
|
-
hasError(await validate(3, { exclusiveMinimum: 3 }))
|
|
221
|
-
hasError(await validate([1, 2], { exclusiveMinimum: 3 }))
|
|
222
|
-
hasError(await validate([1, 2, 3], { exclusiveMinimum: 3 }))
|
|
223
|
-
hasError(await validate('12', { exclusiveMinimum: 3 }))
|
|
224
|
-
hasError(await validate('123', { exclusiveMinimum: 3 }))
|
|
225
|
-
|
|
226
|
-
hasError(await validate('1234', { len: 3 }))
|
|
227
|
-
hasError(await validate({ aa: 1, bb: 2, cc: 3 }, { maxProperties: 2 }))
|
|
228
|
-
noError(await validate({ aa: 1, cc: 3 }, { maxProperties: 2 }))
|
|
229
|
-
hasError(await validate({ aa: 1 }, { minProperties: 2 }))
|
|
230
|
-
noError(await validate({ aa: 1, bb: 2, cc: 3 }, { minProperties: 2 }))
|
|
231
|
-
noError(await validate({ aa: 1, cc: 3 }, { maxProperties: 2 }))
|
|
232
|
-
})
|
|
233
|
-
|
|
234
|
-
test('const', async () => {
|
|
235
|
-
noError(await validate('', { const: '123' }))
|
|
236
|
-
noError(await validate('123', { const: '123' }))
|
|
237
|
-
hasError(await validate('xxx', { const: '123' }))
|
|
238
|
-
})
|
|
239
|
-
|
|
240
|
-
test('multipleOf', async () => {
|
|
241
|
-
noError(await validate('', { multipleOf: 2 }))
|
|
242
|
-
noError(await validate(4, { multipleOf: 2 }))
|
|
243
|
-
hasError(await validate(3, { multipleOf: 2 }))
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
test('uniqueItems', async () => {
|
|
247
|
-
noError(await validate('', { uniqueItems: true }))
|
|
248
|
-
noError(await validate(4, { uniqueItems: true }))
|
|
249
|
-
hasError(await validate([1, 2], { uniqueItems: true }))
|
|
250
|
-
hasError(
|
|
251
|
-
await validate([{ label: '11', value: '11' }, { label: '11' }], {
|
|
252
|
-
uniqueItems: true,
|
|
253
|
-
})
|
|
254
|
-
)
|
|
255
|
-
noError(await validate([1, 1], { uniqueItems: true }))
|
|
256
|
-
noError(
|
|
257
|
-
await validate(
|
|
258
|
-
[
|
|
259
|
-
{ label: '11', value: '11' },
|
|
260
|
-
{ label: '11', value: '11' },
|
|
261
|
-
],
|
|
262
|
-
{ uniqueItems: true }
|
|
263
|
-
)
|
|
264
|
-
)
|
|
265
|
-
})
|
|
266
|
-
|
|
267
|
-
test('pattern', async () => {
|
|
268
|
-
hasError(await validate('aaa', { pattern: /^\d+$/ }))
|
|
269
|
-
})
|
|
270
|
-
|
|
271
|
-
test('validator', async () => {
|
|
272
|
-
hasError(
|
|
273
|
-
await validate('aaa', {
|
|
274
|
-
validator() {
|
|
275
|
-
return false
|
|
276
|
-
},
|
|
277
|
-
message: 'error',
|
|
278
|
-
}),
|
|
279
|
-
'error'
|
|
280
|
-
)
|
|
281
|
-
})
|
|
282
|
-
|
|
283
|
-
test('whitespace', async () => {
|
|
284
|
-
hasError(
|
|
285
|
-
await validate(' ', {
|
|
286
|
-
whitespace: true,
|
|
287
|
-
})
|
|
288
|
-
)
|
|
289
|
-
})
|
|
290
|
-
|
|
291
|
-
test('enum', async () => {
|
|
292
|
-
hasError(
|
|
293
|
-
await validate('11', {
|
|
294
|
-
enum: ['22', '33'],
|
|
295
|
-
})
|
|
296
|
-
)
|
|
297
|
-
noError(
|
|
298
|
-
await validate('11', {
|
|
299
|
-
enum: ['22', '33', '11'],
|
|
300
|
-
})
|
|
301
|
-
)
|
|
302
|
-
})
|
|
303
|
-
|
|
304
|
-
test('filter trigger type(unmatch)', async () => {
|
|
305
|
-
expect(
|
|
306
|
-
await validate(
|
|
307
|
-
'',
|
|
308
|
-
{
|
|
309
|
-
triggerType: 'onBlur',
|
|
310
|
-
required: true,
|
|
311
|
-
validator() {
|
|
312
|
-
return 'validate error'
|
|
313
|
-
},
|
|
314
|
-
},
|
|
315
|
-
{
|
|
316
|
-
validateFirst: true,
|
|
317
|
-
triggerType: 'onInput',
|
|
318
|
-
}
|
|
319
|
-
)
|
|
320
|
-
).toEqual({
|
|
321
|
-
error: [],
|
|
322
|
-
success: [],
|
|
323
|
-
warning: [],
|
|
324
|
-
})
|
|
325
|
-
})
|
|
326
|
-
|
|
327
|
-
test('filter trigger type(match first validate)', async () => {
|
|
328
|
-
expect(
|
|
329
|
-
await validate(
|
|
330
|
-
'',
|
|
331
|
-
{
|
|
332
|
-
triggerType: 'onBlur',
|
|
333
|
-
required: true,
|
|
334
|
-
validator() {
|
|
335
|
-
return 'validate error'
|
|
336
|
-
},
|
|
337
|
-
},
|
|
338
|
-
{
|
|
339
|
-
validateFirst: true,
|
|
340
|
-
triggerType: 'onBlur',
|
|
341
|
-
}
|
|
342
|
-
)
|
|
343
|
-
).toEqual({
|
|
344
|
-
error: ['The field value is required'],
|
|
345
|
-
success: [],
|
|
346
|
-
warning: [],
|
|
347
|
-
})
|
|
348
|
-
})
|
|
349
|
-
|
|
350
|
-
test('filter trigger type(match multi validate)', async () => {
|
|
351
|
-
expect(
|
|
352
|
-
await validate(
|
|
353
|
-
'',
|
|
354
|
-
{
|
|
355
|
-
triggerType: 'onBlur',
|
|
356
|
-
required: true,
|
|
357
|
-
validator() {
|
|
358
|
-
return 'validate error'
|
|
359
|
-
},
|
|
360
|
-
},
|
|
361
|
-
{
|
|
362
|
-
triggerType: 'onBlur',
|
|
363
|
-
}
|
|
364
|
-
)
|
|
365
|
-
).toEqual({
|
|
366
|
-
error: ['The field value is required', 'validate error'],
|
|
367
|
-
success: [],
|
|
368
|
-
warning: [],
|
|
369
|
-
})
|
|
370
|
-
})
|
|
371
|
-
|
|
372
|
-
test('validate formats(date)', async () => {
|
|
373
|
-
noError(await validate('', 'date'))
|
|
374
|
-
hasError(await validate('2020-1', 'date'))
|
|
375
|
-
hasError(await validate('2020-01- 11:23:33', 'date'))
|
|
376
|
-
hasError(await validate('12/01/', 'date'))
|
|
377
|
-
noError(await validate('2020-1-12', 'date'))
|
|
378
|
-
noError(await validate('2020/1/12', 'date'))
|
|
379
|
-
noError(await validate('2020-01-12', 'date'))
|
|
380
|
-
noError(await validate('2020/01/12', 'date'))
|
|
381
|
-
noError(await validate('12/01/2020', 'date'))
|
|
382
|
-
noError(await validate('2020-01-12 11:23:33', 'date'))
|
|
383
|
-
noError(await validate('2020/01/12 11:23:33', 'date'))
|
|
384
|
-
noError(await validate('12/01/2020 11:23:33', 'date'))
|
|
385
|
-
noError(await validate('12/1/2020 11:23:33', 'date'))
|
|
386
|
-
})
|
|
387
|
-
|
|
388
|
-
test('validate formats(number)', async () => {
|
|
389
|
-
noError(await validate('', 'number'))
|
|
390
|
-
hasError(await validate('12323d', 'number'))
|
|
391
|
-
noError(await validate('12323', 'number'))
|
|
392
|
-
noError(await validate('12323.12', 'number'))
|
|
393
|
-
noError(await validate('-12323.12', 'number'))
|
|
394
|
-
noError(await validate('+12323.12', 'number'))
|
|
395
|
-
})
|
|
396
|
-
|
|
397
|
-
test('validate formats(integer)', async () => {
|
|
398
|
-
noError(await validate('', 'integer'))
|
|
399
|
-
hasError(await validate('222.333', 'integer'))
|
|
400
|
-
noError(await validate('12323', 'integer'))
|
|
401
|
-
})
|
|
402
|
-
|
|
403
|
-
test('validate formats(phone)', async () => {
|
|
404
|
-
noError(await validate('', 'phone'))
|
|
405
|
-
hasError(await validate('222333', 'phone'))
|
|
406
|
-
noError(await validate('15934567899', 'phone'))
|
|
407
|
-
})
|
|
408
|
-
|
|
409
|
-
test('validate formats(money)', async () => {
|
|
410
|
-
noError(await validate('$12', 'money'))
|
|
411
|
-
hasError(await validate('$12.', 'money'))
|
|
412
|
-
noError(await validate('$12.3', 'money'))
|
|
413
|
-
})
|
|
414
|
-
|
|
415
|
-
test('validate custom validator', async () => {
|
|
416
|
-
hasError(await validate('123', { custom: true }))
|
|
417
|
-
noError(await validate('', { custom: true }))
|
|
418
|
-
})
|
|
419
|
-
|
|
420
|
-
test('validate custom formats', async () => {
|
|
421
|
-
hasError(await validate('aa asd', 'custom'))
|
|
422
|
-
hasError(await validate('aa asd 中文', 'custom'))
|
|
423
|
-
noError(await validate('中文', 'custom'))
|
|
424
|
-
})
|
|
425
|
-
|
|
426
|
-
test('validate undefined format', async () => {
|
|
427
|
-
expect(
|
|
428
|
-
(
|
|
429
|
-
await validate('a', {
|
|
430
|
-
required: false,
|
|
431
|
-
pattern: '(\\d{3,4}-\\d{7,8}-\\d{4})|(4\\d{4,9})|(\\d{3,4}-\\d{7,8})',
|
|
432
|
-
format: undefined,
|
|
433
|
-
message: 'error',
|
|
434
|
-
})
|
|
435
|
-
).error
|
|
436
|
-
).toEqual(['error'])
|
|
437
|
-
})
|
|
438
|
-
|
|
439
|
-
test('validator return boolean', async () => {
|
|
440
|
-
hasError(
|
|
441
|
-
await validate('123', {
|
|
442
|
-
customBool: true,
|
|
443
|
-
message: 'custom error',
|
|
444
|
-
}),
|
|
445
|
-
'custom error'
|
|
446
|
-
)
|
|
447
|
-
noError(
|
|
448
|
-
await validate('123', {
|
|
449
|
-
customBool2: true,
|
|
450
|
-
message: 'custom error',
|
|
451
|
-
})
|
|
452
|
-
)
|
|
453
|
-
})
|
|
454
|
-
|
|
455
|
-
test('language', async () => {
|
|
456
|
-
setValidateLanguage('zh-CN')
|
|
457
|
-
hasError(
|
|
458
|
-
await validate('', {
|
|
459
|
-
required: true,
|
|
460
|
-
}),
|
|
461
|
-
'该字段是必填字段'
|
|
462
|
-
)
|
|
463
|
-
setValidateLanguage('en-US')
|
|
464
|
-
hasError(
|
|
465
|
-
await validate('', {
|
|
466
|
-
required: true,
|
|
467
|
-
}),
|
|
468
|
-
'The field value is required'
|
|
469
|
-
)
|
|
470
|
-
})
|
|
471
|
-
|
|
472
|
-
test('validator template', async () => {
|
|
473
|
-
registerValidateMessageTemplateEngine(message => {
|
|
474
|
-
if (typeof message !== 'string') return message
|
|
475
|
-
return message.replace(/<<\s*([\w.]+)\s*>>/g, (_, $0) => {
|
|
476
|
-
return { aa: 123 }[$0]
|
|
477
|
-
})
|
|
478
|
-
})
|
|
479
|
-
hasError(
|
|
480
|
-
await validate('', () => {
|
|
481
|
-
return `<<aa>>=123`
|
|
482
|
-
}),
|
|
483
|
-
'123=123'
|
|
484
|
-
)
|
|
485
|
-
})
|
|
486
|
-
|
|
487
|
-
test('validator template with format', async () => {
|
|
488
|
-
registerValidateMessageTemplateEngine(message => {
|
|
489
|
-
if (typeof message !== 'string') return message
|
|
490
|
-
return message.replace(/<<\s*([\w.]+)\s*>>/g, (_, $0) => {
|
|
491
|
-
return { aa: 123 }[$0]
|
|
492
|
-
})
|
|
493
|
-
})
|
|
494
|
-
hasError(
|
|
495
|
-
await validate('', (value, rules, ctx, format) => {
|
|
496
|
-
return `<<aa>>=123&${format('<<aa>>')}`
|
|
497
|
-
}),
|
|
498
|
-
'123=123&123'
|
|
499
|
-
)
|
|
500
|
-
})
|
|
501
|
-
|
|
502
|
-
test('validator template with format and scope', async () => {
|
|
503
|
-
registerValidateMessageTemplateEngine(message => {
|
|
504
|
-
if (typeof message !== 'string') return message
|
|
505
|
-
return message.replace(/<<\s*([\w.]+)\s*>>/g, (_, $0) => {
|
|
506
|
-
return { aa: 123 }[$0]
|
|
507
|
-
})
|
|
508
|
-
})
|
|
509
|
-
|
|
510
|
-
const result = await validate(
|
|
511
|
-
'',
|
|
512
|
-
(value, rules, ctx, format) => {
|
|
513
|
-
return `<<aa>>=123&${format('<<aa>>{{name}}')}`
|
|
514
|
-
},
|
|
515
|
-
{
|
|
516
|
-
context: {
|
|
517
|
-
name: 'scopeName',
|
|
518
|
-
},
|
|
519
|
-
}
|
|
520
|
-
)
|
|
521
|
-
|
|
522
|
-
expect(result.error![0]).toEqual('123=123&123scopeName')
|
|
523
|
-
})
|
|
524
|
-
|
|
525
|
-
test('validator order with format', async () => {
|
|
526
|
-
hasError(
|
|
527
|
-
await validate('', [
|
|
528
|
-
{ required: true },
|
|
529
|
-
{
|
|
530
|
-
format: 'url',
|
|
531
|
-
},
|
|
532
|
-
]),
|
|
533
|
-
'The field value is required'
|
|
534
|
-
)
|
|
535
|
-
})
|