@formvk/validator 0.0.0-stage → 0.1.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/__tests__/parser.spec.ts +17 -0
- package/__tests__/registry.spec.ts +23 -0
- package/__tests__/validator.spec.ts +535 -0
- package/dist/index.d.ts +118 -0
- package/dist/index.js +487 -0
- package/dist/index.js.map +1 -0
- package/package.json +16 -3
- package/src/formats.ts +66 -0
- package/src/index.ts +4 -0
- package/src/locale.ts +219 -0
- package/src/parser.ts +136 -0
- package/src/registry.ts +108 -0
- package/src/rules.ts +147 -0
- package/src/template.ts +18 -0
- package/src/types.ts +105 -0
- package/src/validator.ts +36 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.json +4 -0
- package/vite.config.ts +18 -0
- package/README.md +0 -3
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,535 @@
|
|
|
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
|
+
})
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
declare function getISOCode(language: string): any;
|
|
2
|
+
|
|
3
|
+
export declare function getLocaleByPath(path: string, lang?: string): any;
|
|
4
|
+
|
|
5
|
+
export declare function getValidateFormats(key?: string): any;
|
|
6
|
+
|
|
7
|
+
export declare function getValidateLanguage(): any;
|
|
8
|
+
|
|
9
|
+
export declare function getValidateLocale(path: string): any;
|
|
10
|
+
|
|
11
|
+
export declare const getValidateLocaleIOSCode: typeof getISOCode;
|
|
12
|
+
|
|
13
|
+
export declare function getValidateMessageTemplateEngine(): any;
|
|
14
|
+
|
|
15
|
+
export declare function getValidateRules<T>(key?: T): T extends string ? ValidatorFunction : {
|
|
16
|
+
[key: string]: ValidatorFunction;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export declare interface IRegistryFormats {
|
|
20
|
+
[key: string]: string | RegExp;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export declare interface IRegistryLocaleMessages {
|
|
24
|
+
[key: string]: string | IRegistryLocaleMessages;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export declare interface IRegistryLocales {
|
|
28
|
+
[language: string]: IRegistryLocaleMessages;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare interface IRegistryRules<Context = any> {
|
|
32
|
+
[key: string]: ValidatorFunction<Context>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export declare function isValidateResult(obj: any): obj is IValidateResult;
|
|
36
|
+
|
|
37
|
+
export declare interface IValidateResult {
|
|
38
|
+
type: 'error' | 'warning' | 'success' | (string & {});
|
|
39
|
+
message: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export declare interface IValidateResults {
|
|
43
|
+
error?: string[];
|
|
44
|
+
warning?: string[];
|
|
45
|
+
success?: string[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export declare interface IValidatorOptions<Context = any> {
|
|
49
|
+
validateFirst?: boolean;
|
|
50
|
+
triggerType?: ValidatorTriggerType;
|
|
51
|
+
context?: Context;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export declare interface IValidatorRules<Context = any> {
|
|
55
|
+
triggerType?: ValidatorTriggerType;
|
|
56
|
+
format?: ValidatorFormats;
|
|
57
|
+
validator?: ValidatorFunction<Context>;
|
|
58
|
+
required?: boolean;
|
|
59
|
+
pattern?: RegExp | string;
|
|
60
|
+
max?: number;
|
|
61
|
+
maximum?: number;
|
|
62
|
+
maxItems?: number;
|
|
63
|
+
minItems?: number;
|
|
64
|
+
maxLength?: number;
|
|
65
|
+
minLength?: number;
|
|
66
|
+
exclusiveMaximum?: number;
|
|
67
|
+
exclusiveMinimum?: number;
|
|
68
|
+
minimum?: number;
|
|
69
|
+
min?: number;
|
|
70
|
+
len?: number;
|
|
71
|
+
whitespace?: boolean;
|
|
72
|
+
enum?: any[];
|
|
73
|
+
const?: any;
|
|
74
|
+
multipleOf?: number;
|
|
75
|
+
uniqueItems?: boolean;
|
|
76
|
+
maxProperties?: number;
|
|
77
|
+
minProperties?: number;
|
|
78
|
+
message?: string;
|
|
79
|
+
[key: string]: any;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export declare type MultiValidator<Context = any> = ValidatorDescription<Context>[];
|
|
83
|
+
|
|
84
|
+
export declare function parseValidator<Context = any>(validator?: Validator<Context>, options?: IValidatorOptions): ValidatorParsedFunction<Context>[];
|
|
85
|
+
|
|
86
|
+
export declare function parseValidatorDescription(description: ValidatorDescription): IValidatorRules;
|
|
87
|
+
|
|
88
|
+
export declare function parseValidatorDescriptions<Context = any>(validator?: Validator<Context>): IValidatorRules[];
|
|
89
|
+
|
|
90
|
+
export declare function parseValidatorRules(rules?: IValidatorRules): ValidatorParsedFunction[];
|
|
91
|
+
|
|
92
|
+
export declare function registerValidateFormats(formats: IRegistryFormats): void;
|
|
93
|
+
|
|
94
|
+
export declare function registerValidateLocale(locale: IRegistryLocales): void;
|
|
95
|
+
|
|
96
|
+
export declare function registerValidateMessageTemplateEngine(template: (message: ValidatorFunctionResponse, context: any) => any): void;
|
|
97
|
+
|
|
98
|
+
export declare function registerValidateRules(rules: IRegistryRules): void;
|
|
99
|
+
|
|
100
|
+
export declare function setValidateLanguage(lang: string): void;
|
|
101
|
+
|
|
102
|
+
export declare function validate<Context = any>(value: any, validator?: Validator<Context>, options?: IValidatorOptions<Context>): Promise<IValidateResults>;
|
|
103
|
+
|
|
104
|
+
export declare type Validator<Context = any> = ValidatorDescription<Context> | MultiValidator<Context>;
|
|
105
|
+
|
|
106
|
+
export declare type ValidatorDescription<Context = any> = ValidatorFormats | ValidatorFunction<Context> | IValidatorRules<Context>;
|
|
107
|
+
|
|
108
|
+
export declare type ValidatorFormats = 'url' | 'email' | 'ipv6' | 'ipv4' | 'number' | 'integer' | 'idcard' | 'qq' | 'phone' | 'money' | 'zh' | 'date' | 'zip' | (string & {});
|
|
109
|
+
|
|
110
|
+
export declare type ValidatorFunction<Context = any> = (value: any, rule: IValidatorRules<Context>, ctx: Context, render: (message: string, scope?: any) => string) => ValidatorFunctionResponse | Promise<ValidatorFunctionResponse>;
|
|
111
|
+
|
|
112
|
+
export declare type ValidatorFunctionResponse = string | boolean | IValidateResult | undefined | null;
|
|
113
|
+
|
|
114
|
+
export declare type ValidatorParsedFunction<Context = any> = (value: any, ctx?: Context) => IValidateResult | Promise<IValidateResult> | null;
|
|
115
|
+
|
|
116
|
+
export declare type ValidatorTriggerType = 'onInput' | 'onFocus' | 'onBlur' | (string & {});
|
|
117
|
+
|
|
118
|
+
export { }
|