@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/src/formats.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
url: new RegExp(
|
|
3
|
-
// protocol identifier
|
|
4
|
-
'^(?:(?:(?:https?|ftp|rtmp):)?//)' +
|
|
5
|
-
// user:pass authentication
|
|
6
|
-
'(?:\\S+(?::\\S*)?@)?' +
|
|
7
|
-
'(?:' +
|
|
8
|
-
// IP address exclusion - private & local networks
|
|
9
|
-
// Reference: https://www.arin.net/knowledge/address_filters.html
|
|
10
|
-
|
|
11
|
-
// filter 10.*.*.* and 127.*.*.* addresses
|
|
12
|
-
'(?!(?:10|127)(?:\\.\\d{1,3}){3})' +
|
|
13
|
-
// filter 169.254.*.* and 192.168.*.*
|
|
14
|
-
'(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})' +
|
|
15
|
-
// filter 172.16.0.0 - 172.31.255.255
|
|
16
|
-
// TODO: add test to validate that it invalidates address in 16-31 range
|
|
17
|
-
'(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})' +
|
|
18
|
-
// IP address dotted notation octets
|
|
19
|
-
// excludes loopback network 0.0.0.0
|
|
20
|
-
// excludes reserved space >= 224.0.0.0
|
|
21
|
-
// excludes network & broadcast addresses
|
|
22
|
-
// (first & last IP address of each class)
|
|
23
|
-
|
|
24
|
-
// filter 1. part for 1-223
|
|
25
|
-
'(?:22[0-3]|2[01]\\d|[1-9]\\d?|1\\d\\d)' +
|
|
26
|
-
// filter 2. and 3. part for 0-255
|
|
27
|
-
'(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d{1,2})){2}' +
|
|
28
|
-
// filter 4. part for 1-254
|
|
29
|
-
'(?:\\.(?:25[0-4]|2[0-4]\\d|1\\d\\d|[1-9]\\d?))' +
|
|
30
|
-
'|' +
|
|
31
|
-
// host name
|
|
32
|
-
'(?:(?:[a-z\\u00a1-\\uffff0-9_]-*)*[a-z\\u00a1-\\uffff0-9_]+)' +
|
|
33
|
-
// domain name
|
|
34
|
-
'(?:\\.(?:[a-z\\u00a1-\\uffff0-9_]-*)*[a-z\\u00a1-\\uffff0-9_]+)*' +
|
|
35
|
-
// TLD identifier
|
|
36
|
-
'(?:\\.(?:[a-z\\u00a1-\\uffff_]{2,}))' +
|
|
37
|
-
')' +
|
|
38
|
-
// port number
|
|
39
|
-
'(?::\\d{2,5})?' +
|
|
40
|
-
// resource path
|
|
41
|
-
'(?:/?\\S*)?$'
|
|
42
|
-
),
|
|
43
|
-
email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
|
|
44
|
-
|
|
45
|
-
ipv6: /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/,
|
|
46
|
-
|
|
47
|
-
ipv4: /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/,
|
|
48
|
-
|
|
49
|
-
number: /^[+-]?\d+(\.\d+)?$/,
|
|
50
|
-
|
|
51
|
-
integer: /^[+-]?\d+$/,
|
|
52
|
-
|
|
53
|
-
qq: /^(\+?[1-9]\d*|0)$/,
|
|
54
|
-
|
|
55
|
-
phone: /^\d{3}-\d{8}$|^\d{4}-\d{7}$|^\d{11}$/,
|
|
56
|
-
|
|
57
|
-
idcard: /^\d{15}$|^\d{17}(\d|x|X)$/,
|
|
58
|
-
|
|
59
|
-
money: /^([\u0024\u00A2\u00A3\u00A4\u20AC\u00A5\u20B1\u20B9\uFFE5]\s*)(\d+,?)+(\.\d+)?\s*$/,
|
|
60
|
-
|
|
61
|
-
zh: /^[\u4e00-\u9fa5]+$/,
|
|
62
|
-
|
|
63
|
-
date: /^[0-9]+[./-][0-9]+[./-][0-9]+\s*(?:[0-9]+\s*:\s*[0-9]+\s*:\s*[0-9]+)?$/,
|
|
64
|
-
|
|
65
|
-
zip: /^[0-9]{6}$/,
|
|
66
|
-
}
|
package/src/index.ts
DELETED
package/src/locale.ts
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
en: {
|
|
3
|
-
pattern: 'This field is invalid',
|
|
4
|
-
invalid: 'This field is invalid',
|
|
5
|
-
required: 'The field value is required',
|
|
6
|
-
number: 'The field value is not a number',
|
|
7
|
-
integer: 'The field value is not an integer number',
|
|
8
|
-
url: 'The field value is a invalid url',
|
|
9
|
-
email: 'The field value is not a email format',
|
|
10
|
-
ipv6: 'The field value is not a ipv6 format',
|
|
11
|
-
ipv4: 'The field value is not a ipv4 format',
|
|
12
|
-
idcard: 'The field value is not an idcard format',
|
|
13
|
-
qq: 'The field value is not a qq number format',
|
|
14
|
-
phone: 'The field value is not a phone number format',
|
|
15
|
-
money: 'The field value is not a currency format',
|
|
16
|
-
zh: 'The field value is not a chinese string',
|
|
17
|
-
date: 'The field value is not a valid date format',
|
|
18
|
-
zip: 'The field value is not a zip format',
|
|
19
|
-
len: 'The length or number of entries must be {{len}}',
|
|
20
|
-
min: 'The length or number of entries must be at least {{min}}',
|
|
21
|
-
minLength: 'The length or number of entries must be at least {{minLength}}',
|
|
22
|
-
minItems: 'The length or number of entries must be at least {{minItems}}',
|
|
23
|
-
maximum: 'The field value cannot be greater than {{maximum}}',
|
|
24
|
-
exclusiveMaximum: 'The field value must be less than {{exclusiveMaximum}}',
|
|
25
|
-
minimum: 'The field value cannot be less than {{minimum}}',
|
|
26
|
-
exclusiveMinimum: 'The field value must be greater than {{exclusiveMinimum}}',
|
|
27
|
-
max: 'The field length or number of entries must be at most {{max}}',
|
|
28
|
-
maxLength: 'The field length or number of entries must be at most {{maxLength}}',
|
|
29
|
-
maxItems: 'The field length or number of entries must be at most {{maxItems}}',
|
|
30
|
-
whitespace: 'This field value cannot be blank string.',
|
|
31
|
-
enum: 'The field value must be one of {{enum}}',
|
|
32
|
-
|
|
33
|
-
const: 'The field value must be equal to {{const}}',
|
|
34
|
-
multipleOf: 'The field value must be divisible by {{multipleOf}}',
|
|
35
|
-
maxProperties: 'The number of field properties cannot be greater than {{maxProperties}}',
|
|
36
|
-
minProperties: 'The number of field properties cannot be less than {{maxProperties}}',
|
|
37
|
-
uniqueItems: 'Array elements are not unique',
|
|
38
|
-
},
|
|
39
|
-
zh: {
|
|
40
|
-
pattern: '该字段不是一个合法的字段',
|
|
41
|
-
invalid: '该字段不是一个合法的字段',
|
|
42
|
-
required: '该字段是必填字段',
|
|
43
|
-
number: '该字段不是合法的数字',
|
|
44
|
-
integer: '该字段不是合法的整型数字',
|
|
45
|
-
url: '该字段不是合法的url',
|
|
46
|
-
email: '该字段不是合法的邮箱格式',
|
|
47
|
-
ipv6: '该字段不是合法的ipv6格式',
|
|
48
|
-
ipv4: '该字段不是合法的ipv4格式',
|
|
49
|
-
idcard: '该字段不是合法的身份证格式',
|
|
50
|
-
qq: '该字段不符合QQ号格式',
|
|
51
|
-
phone: '该字段不是有效的手机号',
|
|
52
|
-
money: '该字段不是有效货币格式',
|
|
53
|
-
zh: '该字段不是合法的中文字符串',
|
|
54
|
-
date: '该字段不是合法的日期格式',
|
|
55
|
-
zip: '该字段不是合法的邮编格式',
|
|
56
|
-
len: '长度或条目数必须为{{len}}',
|
|
57
|
-
min: '长度或条目数不能小于{{min}}',
|
|
58
|
-
minLength: '长度或条目数不能小于{{minLength}}',
|
|
59
|
-
minItems: '长度或条目数不能小于{{minItems}}',
|
|
60
|
-
max: '长度或条目数不能大于{{max}}',
|
|
61
|
-
maxLength: '长度或条目数不能大于{{maxLength}}',
|
|
62
|
-
maxItems: '长度或条目数不能大于{{maxItems}}',
|
|
63
|
-
maximum: '数值不能大于{{maximum}}',
|
|
64
|
-
exclusiveMaximum: '数值必须小于{{exclusiveMaximum}}',
|
|
65
|
-
minimum: '数值不能小于{{minimum}}',
|
|
66
|
-
exclusiveMinimum: '数值必须大于{{exclusiveMinimum}}',
|
|
67
|
-
whitespace: '不能为纯空白字符串',
|
|
68
|
-
enum: '字段值必须为{{enum}}其中一个',
|
|
69
|
-
const: '字段值必须等于{{const}}',
|
|
70
|
-
multipleOf: '字段值不能被{{multipleOf}}整除',
|
|
71
|
-
maxProperties: '字段属性数量不能大于{{maxProperties}}',
|
|
72
|
-
minProperties: '字段属性数量不能小于{{minProperties}}',
|
|
73
|
-
uniqueItems: '数组元素不唯一',
|
|
74
|
-
},
|
|
75
|
-
'en-US': {
|
|
76
|
-
pattern: 'This field is invalid',
|
|
77
|
-
invalid: 'This field is invalid',
|
|
78
|
-
required: 'The field value is required',
|
|
79
|
-
number: 'The field value is not a number',
|
|
80
|
-
integer: 'The field value is not an integer number',
|
|
81
|
-
url: 'The field value is a invalid url',
|
|
82
|
-
email: 'The field value is not a email format',
|
|
83
|
-
ipv6: 'The field value is not a ipv6 format',
|
|
84
|
-
ipv4: 'The field value is not a ipv4 format',
|
|
85
|
-
idcard: 'The field value is not an idcard format',
|
|
86
|
-
qq: 'The field value is not a qq number format',
|
|
87
|
-
phone: 'The field value is not a phone number format',
|
|
88
|
-
money: 'The field value is not a currency format',
|
|
89
|
-
zh: 'The field value is not a chinese string',
|
|
90
|
-
date: 'The field value is not a valid date format',
|
|
91
|
-
zip: 'The field value is not a zip format',
|
|
92
|
-
len: 'The length or number of entries must be {{len}}',
|
|
93
|
-
min: 'The length or number of entries must be at least {{min}}',
|
|
94
|
-
minLength: 'The length or number of entries must be at least {{minLength}}',
|
|
95
|
-
minItems: 'The length or number of entries must be at least {{minItems}}',
|
|
96
|
-
maximum: 'The field value cannot be greater than {{maximum}}',
|
|
97
|
-
exclusiveMaximum: 'The field value must be less than {{exclusiveMaximum}}',
|
|
98
|
-
minimum: 'The field value cannot be less than {{minimum}}',
|
|
99
|
-
exclusiveMinimum: 'The field value must be greater than {{exclusiveMinimum}}',
|
|
100
|
-
max: 'The field length or number of entries must be at most {{max}}',
|
|
101
|
-
maxLength: 'The field length or number of entries must be at most {{maxLength}}',
|
|
102
|
-
maxItems: 'The field length or number of entries must be at most {{maxItems}}',
|
|
103
|
-
whitespace: 'This field value cannot be blank string.',
|
|
104
|
-
enum: 'The field value must be one of {{enum}}',
|
|
105
|
-
const: 'The field value must be equal to {{const}}',
|
|
106
|
-
multipleOf: 'The field value must be divisible by {{multipleOf}}',
|
|
107
|
-
maxProperties: 'The number of field properties cannot be greater than {{maxProperties}}',
|
|
108
|
-
minProperties: 'The number of field properties cannot be less than {{maxProperties}}',
|
|
109
|
-
uniqueItems: 'Array elements are not unique',
|
|
110
|
-
},
|
|
111
|
-
'zh-CN': {
|
|
112
|
-
pattern: '该字段不是一个合法的字段',
|
|
113
|
-
invalid: '该字段不是一个合法的字段',
|
|
114
|
-
required: '该字段是必填字段',
|
|
115
|
-
number: '该字段不是合法的数字',
|
|
116
|
-
integer: '该字段不是合法的整型数字',
|
|
117
|
-
url: '该字段不是合法的url',
|
|
118
|
-
email: '该字段不是合法的邮箱格式',
|
|
119
|
-
ipv6: '该字段不是合法的ipv6格式',
|
|
120
|
-
ipv4: '该字段不是合法的ipv4格式',
|
|
121
|
-
idcard: '该字段不是合法的身份证格式',
|
|
122
|
-
qq: '该字段不符合QQ号格式',
|
|
123
|
-
phone: '该字段不是有效的手机号',
|
|
124
|
-
money: '该字段不是有效货币格式',
|
|
125
|
-
zh: '该字段不是合法的中文字符串',
|
|
126
|
-
date: '该字段不是合法的日期格式',
|
|
127
|
-
zip: '该字段不是合法的邮编格式',
|
|
128
|
-
len: '长度或条目数必须为{{len}}',
|
|
129
|
-
min: '长度或条目数不能小于{{min}}',
|
|
130
|
-
minLength: '长度或条目数不能小于{{minLength}}',
|
|
131
|
-
minItems: '长度或条目数不能小于{{minItems}}',
|
|
132
|
-
maxLength: '长度或条目数不能大于{{maxLength}}',
|
|
133
|
-
maxItems: '长度或条目数不能大于{{maxItems}}',
|
|
134
|
-
max: '长度或条目数不能大于{{max}}',
|
|
135
|
-
maximum: '数值不能大于{{maximum}}',
|
|
136
|
-
exclusiveMaximum: '数值必须小于{{exclusiveMaximum}}',
|
|
137
|
-
minimum: '数值不能小于{{minimum}}',
|
|
138
|
-
exclusiveMinimum: '数值必须大于{{exclusiveMinimum}}',
|
|
139
|
-
whitespace: '不能为纯空白字符串',
|
|
140
|
-
enum: '字段值必须为{{enum}}其中一个',
|
|
141
|
-
const: '字段值必须等于{{const}}',
|
|
142
|
-
multipleOf: '字段值不能被{{multipleOf}}整除',
|
|
143
|
-
maxProperties: '字段属性数量不能大于{{maxProperties}}',
|
|
144
|
-
minProperties: '字段属性数量不能小于{{minProperties}}',
|
|
145
|
-
uniqueItems: '数组元素不唯一',
|
|
146
|
-
},
|
|
147
|
-
'zh-TW': {
|
|
148
|
-
pattern: '該字段不是一個合法的字段',
|
|
149
|
-
invalid: '該字段不是一個合法的字段',
|
|
150
|
-
required: '該字段是必填字段',
|
|
151
|
-
number: '該字段不是合法的數字',
|
|
152
|
-
integer: '該字段不是合法的整型數字',
|
|
153
|
-
url: '該字段不是合法的url',
|
|
154
|
-
email: '該字段不是合法的郵箱格式',
|
|
155
|
-
ipv6: '該字段不是合法的ipv6格式',
|
|
156
|
-
ipv4: '該字段不是合法的ipv4格式',
|
|
157
|
-
idcard: '該字段不是合法的身份證格式',
|
|
158
|
-
qq: '該字段不符合QQ號格式',
|
|
159
|
-
phone: '該字段不是有效的手機號',
|
|
160
|
-
money: '該字段不是有效貨幣格式',
|
|
161
|
-
zh: '該字段不是合法的中文字符串',
|
|
162
|
-
date: '該字段不是合法的日期格式',
|
|
163
|
-
zip: '該字段不是合法的郵編格式',
|
|
164
|
-
len: '長度或條目數必須為{{len}}',
|
|
165
|
-
min: '長度或條目數不能小於{{min}}',
|
|
166
|
-
minItems: '長度或條目數不能小於{{minItems}}',
|
|
167
|
-
minLength: '長度或條目數不能小於{{minLength}}',
|
|
168
|
-
max: '長度或條目數不能大於{{max}}',
|
|
169
|
-
maxItems: '長度或條目數不能大於{{maxItems}}',
|
|
170
|
-
maxLength: '長度或條目數不能大於{{maxLength}}',
|
|
171
|
-
maximum: '數值不能大於{{maximum}}',
|
|
172
|
-
exclusiveMaximum: '數值必須小於{{exclusiveMaximum}}',
|
|
173
|
-
minimum: '數值不能小於{{minimum}}',
|
|
174
|
-
exclusiveMinimum: '數值必須大於{{exclusiveMinimum}}',
|
|
175
|
-
whitespace: '不能為純空白字符串',
|
|
176
|
-
enum: '字段值必須為{{enum}}其中一個',
|
|
177
|
-
const: '字段值必須等於{{const}}',
|
|
178
|
-
multipleOf: '字段值不能被{{multipleOf}}整除',
|
|
179
|
-
maxProperties: '字段屬性數量不能大於{{maxProperties}}',
|
|
180
|
-
minProperties: '字段屬性數量不能小於{{minProperties}}',
|
|
181
|
-
uniqueItems: '數組元素不唯一',
|
|
182
|
-
},
|
|
183
|
-
ja: {
|
|
184
|
-
url: 'このフィールドは無効なURLです',
|
|
185
|
-
whitespace: 'このフィールドを空の文字列にすることはできません。',
|
|
186
|
-
zh: 'このフィールドは中国語の文字列ではありません',
|
|
187
|
-
zip: 'このフィールドはzip形式ではありません',
|
|
188
|
-
date: 'このフィールドは有効な日付形式ではありません',
|
|
189
|
-
email: 'このフィールドはメール形式ではありません',
|
|
190
|
-
exclusiveMaximum: '値は{{exclusiveMaximum}}未満である必要があります',
|
|
191
|
-
exclusiveMinimum: '値は{{exclusiveMinimum}}より大きい必要があります',
|
|
192
|
-
idcard: 'このフィールドはIDカード形式ではありません',
|
|
193
|
-
integer: 'このフィールドは整数ではありません',
|
|
194
|
-
ipv4: 'このフィールドはIPv4形式ではありません',
|
|
195
|
-
ipv6: 'このフィールドはIPv6形式ではありません',
|
|
196
|
-
len: 'エントリの長さまたは数は{{len}}でなければなりません',
|
|
197
|
-
max: 'エントリの長さまたは数は最大{{max}}でなければなりません',
|
|
198
|
-
maxItems: 'エントリの長さまたは数は最大{{maxItems}}でなければなりません',
|
|
199
|
-
maxLength: 'エントリの長さまたは数は最大{{maxLength}}でなければなりません',
|
|
200
|
-
maximum: '値は{{最大}}を超えることはできません',
|
|
201
|
-
min: 'エントリの長さまたは数は、少なくとも{{min}}である必要があります',
|
|
202
|
-
minItems: 'エントリの長さまたは数は、少なくとも{{minItems}}である必要があります',
|
|
203
|
-
minLength: 'エントリの長さまたは数は、少なくとも{{minLength}}である必要があります',
|
|
204
|
-
minimum: '値は{{minimum}}以上にする必要があります',
|
|
205
|
-
money: 'このフィールドは通貨形式ではありません',
|
|
206
|
-
number: 'このフィールドは数値ではありません',
|
|
207
|
-
pattern: 'このフィールドはどのパターンとも一致しません',
|
|
208
|
-
invalid: 'このフィールドはどのパターンとも一致しません',
|
|
209
|
-
phone: 'このフィールドは電話番号の形式ではありません',
|
|
210
|
-
qq: 'このフィールドはqq数値形式ではありません',
|
|
211
|
-
required: 'この項目は必須です',
|
|
212
|
-
enum: 'フィールド値は{{enum}}のいずれかである必要があります',
|
|
213
|
-
cons: 'フィールド値は{{const}}と等しくなければなりません',
|
|
214
|
-
multipleOf: 'フィールド値を{{multipleOf}}で割り切れない',
|
|
215
|
-
maxProperties: 'フィールドプロパティの数は{{maxProperties}}を超えることはできません',
|
|
216
|
-
minProperties: 'フィールドプロパティの数は{{minProperties}}未満にすることはできません',
|
|
217
|
-
uniqueItems: '配列要素は一意ではありません',
|
|
218
|
-
},
|
|
219
|
-
}
|
package/src/parser.ts
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { isArr, isBool, isFn, isStr } from '@formvk/shared'
|
|
2
|
-
|
|
3
|
-
import { getValidateLocale, getValidateRules } from './registry'
|
|
4
|
-
import { render } from './template'
|
|
5
|
-
import type {
|
|
6
|
-
IValidatorOptions,
|
|
7
|
-
IValidatorRules,
|
|
8
|
-
Validator,
|
|
9
|
-
ValidatorDescription,
|
|
10
|
-
ValidatorFunction,
|
|
11
|
-
ValidatorParsedFunction,
|
|
12
|
-
} from './types'
|
|
13
|
-
import { isValidateResult } from './types'
|
|
14
|
-
|
|
15
|
-
function getRuleMessage(rule: IValidatorRules, type: string) {
|
|
16
|
-
if (rule.format) {
|
|
17
|
-
return rule.message || getValidateLocale(rule.format)
|
|
18
|
-
}
|
|
19
|
-
return rule.message || getValidateLocale(type)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function parseValidatorDescription(description: ValidatorDescription): IValidatorRules {
|
|
23
|
-
if (!description) return {}
|
|
24
|
-
let rules: IValidatorRules = {}
|
|
25
|
-
if (isStr(description)) {
|
|
26
|
-
rules.format = description
|
|
27
|
-
} else if (isFn(description)) {
|
|
28
|
-
rules.validator = description
|
|
29
|
-
} else {
|
|
30
|
-
rules = Object.assign(rules, description)
|
|
31
|
-
}
|
|
32
|
-
return rules
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function parseValidatorDescriptions<Context = any>(validator?: Validator<Context>): IValidatorRules[] {
|
|
36
|
-
if (!validator) return []
|
|
37
|
-
const array = isArr(validator) ? validator : [validator]
|
|
38
|
-
return array.map(description => {
|
|
39
|
-
return parseValidatorDescription(description)
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function parseValidatorRules(rules: IValidatorRules = {}): ValidatorParsedFunction[] {
|
|
44
|
-
function getRulesKeys(): string[] {
|
|
45
|
-
const keys: any[] = []
|
|
46
|
-
if ('required' in rules) {
|
|
47
|
-
keys.push('required')
|
|
48
|
-
}
|
|
49
|
-
for (const key in rules) {
|
|
50
|
-
if (key === 'required' || key === 'validator') continue
|
|
51
|
-
keys.push(key)
|
|
52
|
-
}
|
|
53
|
-
if ('validator' in rules) {
|
|
54
|
-
keys.push('validator')
|
|
55
|
-
}
|
|
56
|
-
return keys
|
|
57
|
-
}
|
|
58
|
-
function getContext(context: any, value: any) {
|
|
59
|
-
return {
|
|
60
|
-
...rules,
|
|
61
|
-
...context,
|
|
62
|
-
value,
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
function createValidate(callback: ValidatorFunction, message: string): ValidatorParsedFunction {
|
|
66
|
-
return async (value, context) => {
|
|
67
|
-
const context_ = getContext(context, value)
|
|
68
|
-
try {
|
|
69
|
-
const results = await callback(value, { ...rules, message }, context_, (message: string, scope: any) => {
|
|
70
|
-
return render(
|
|
71
|
-
{
|
|
72
|
-
type: 'error',
|
|
73
|
-
message,
|
|
74
|
-
},
|
|
75
|
-
Object.assign(context_, scope)
|
|
76
|
-
)?.message
|
|
77
|
-
})
|
|
78
|
-
if (isBool(results)) {
|
|
79
|
-
if (!results) {
|
|
80
|
-
return render(
|
|
81
|
-
{
|
|
82
|
-
type: 'error',
|
|
83
|
-
message,
|
|
84
|
-
},
|
|
85
|
-
context_
|
|
86
|
-
)
|
|
87
|
-
}
|
|
88
|
-
return {
|
|
89
|
-
type: 'error',
|
|
90
|
-
message: undefined as unknown as string,
|
|
91
|
-
}
|
|
92
|
-
} else if (results) {
|
|
93
|
-
if (isValidateResult(results)) {
|
|
94
|
-
return render(results, context_)
|
|
95
|
-
}
|
|
96
|
-
return render(
|
|
97
|
-
{
|
|
98
|
-
type: 'error',
|
|
99
|
-
message: results,
|
|
100
|
-
},
|
|
101
|
-
context_
|
|
102
|
-
)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return {
|
|
106
|
-
type: 'error',
|
|
107
|
-
message: undefined as unknown as string,
|
|
108
|
-
}
|
|
109
|
-
} catch (e) {
|
|
110
|
-
return {
|
|
111
|
-
type: 'error',
|
|
112
|
-
message: ((e as Error)?.message || e) as string,
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return getRulesKeys().reduce<ValidatorParsedFunction[]>((buf, key) => {
|
|
118
|
-
const callback = getValidateRules(key)
|
|
119
|
-
if (callback) {
|
|
120
|
-
const validator = createValidate(callback, getRuleMessage(rules, key))
|
|
121
|
-
return buf.concat(validator)
|
|
122
|
-
}
|
|
123
|
-
return buf
|
|
124
|
-
}, [])
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export function parseValidator<Context = any>(validator?: Validator<Context>, options: IValidatorOptions = {}) {
|
|
128
|
-
if (!validator) return []
|
|
129
|
-
const array = isArr(validator) ? validator : [validator]
|
|
130
|
-
return array.reduce<ValidatorParsedFunction<Context>[]>((buf, description) => {
|
|
131
|
-
const rules = parseValidatorDescription(description)
|
|
132
|
-
const triggerType = rules.triggerType ?? 'onInput'
|
|
133
|
-
if (options?.triggerType && options.triggerType !== triggerType) return buf
|
|
134
|
-
return rules ? buf.concat(parseValidatorRules(rules)) : buf
|
|
135
|
-
}, [])
|
|
136
|
-
}
|
package/src/registry.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { FormPath, merge as deepmerge, each, globalThisPolyfill, isFn, isStr, lowerCase } from '@formvk/shared'
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
IRegistryFormats,
|
|
5
|
-
IRegistryLocaleMessages,
|
|
6
|
-
IRegistryLocales,
|
|
7
|
-
IRegistryRules,
|
|
8
|
-
ValidatorFunction,
|
|
9
|
-
ValidatorFunctionResponse,
|
|
10
|
-
} from './types'
|
|
11
|
-
|
|
12
|
-
const getIn = FormPath.getIn
|
|
13
|
-
|
|
14
|
-
const self = globalThisPolyfill
|
|
15
|
-
|
|
16
|
-
const defaultLanguage = 'en'
|
|
17
|
-
|
|
18
|
-
function getBrowserLanguage() {
|
|
19
|
-
if (!self.navigator) {
|
|
20
|
-
return defaultLanguage
|
|
21
|
-
}
|
|
22
|
-
return self.navigator.language || defaultLanguage
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const registry: any = {
|
|
26
|
-
locales: {
|
|
27
|
-
messages: {},
|
|
28
|
-
language: getBrowserLanguage(),
|
|
29
|
-
},
|
|
30
|
-
formats: {},
|
|
31
|
-
rules: {},
|
|
32
|
-
template: null,
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function getISOCode(language: string) {
|
|
36
|
-
let isoCode = registry.locales.language
|
|
37
|
-
if (registry.locales.messages[language]) {
|
|
38
|
-
return language
|
|
39
|
-
}
|
|
40
|
-
const lang = lowerCase(language)
|
|
41
|
-
each(registry.locales.messages, (messages: IRegistryLocaleMessages, key: string) => {
|
|
42
|
-
const target = lowerCase(key)
|
|
43
|
-
if (target.indexOf(lang) > -1 || lang.indexOf(target) > -1) {
|
|
44
|
-
isoCode = key
|
|
45
|
-
return false
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
return isoCode
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const getValidateLocaleIOSCode = getISOCode
|
|
52
|
-
|
|
53
|
-
export function setValidateLanguage(lang: string) {
|
|
54
|
-
registry.locales.language = lang || defaultLanguage
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function getValidateLanguage() {
|
|
58
|
-
return registry.locales.language
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function getLocaleByPath(path: string, lang: string = registry.locales.language) {
|
|
62
|
-
return getIn(registry.locales.messages, `${getISOCode(lang)}.${path}`)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function getValidateLocale(path: string) {
|
|
66
|
-
const message = getLocaleByPath(path)
|
|
67
|
-
return message || getLocaleByPath('pattern') || getLocaleByPath('pattern', defaultLanguage)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function getValidateMessageTemplateEngine() {
|
|
71
|
-
return registry.template
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function getValidateFormats(key?: string) {
|
|
75
|
-
return key ? registry.formats[key] : registry.formats
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function getValidateRules<T>(
|
|
79
|
-
key?: T
|
|
80
|
-
): T extends string ? ValidatorFunction : { [key: string]: ValidatorFunction } {
|
|
81
|
-
return key ? registry.rules[key as any] : registry.rules
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function registerValidateLocale(locale: IRegistryLocales) {
|
|
85
|
-
registry.locales.messages = deepmerge(registry.locales.messages, locale)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function registerValidateRules(rules: IRegistryRules) {
|
|
89
|
-
each(rules, (rule, key) => {
|
|
90
|
-
if (isFn(rule)) {
|
|
91
|
-
registry.rules[key] = rule
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function registerValidateFormats(formats: IRegistryFormats) {
|
|
97
|
-
each(formats, (pattern, key) => {
|
|
98
|
-
if (isStr(pattern) || pattern instanceof RegExp) {
|
|
99
|
-
registry.formats[key] = new RegExp(pattern)
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function registerValidateMessageTemplateEngine(
|
|
105
|
-
template: (message: ValidatorFunctionResponse, context: any) => any
|
|
106
|
-
) {
|
|
107
|
-
registry.template = template
|
|
108
|
-
}
|
package/src/rules.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { each, isArr, isBool, isEmpty, isEqual, isFn, isNum, isStr, isValid, stringLength, toArr } from '@formvk/shared'
|
|
2
|
-
|
|
3
|
-
import { getValidateFormats } from './registry'
|
|
4
|
-
import type { IRegistryRules } from './types'
|
|
5
|
-
|
|
6
|
-
function isValidateEmpty(value: any) {
|
|
7
|
-
if (isArr(value)) {
|
|
8
|
-
for (let i = 0; i < value.length; i++) {
|
|
9
|
-
if (isValid(value[i])) return false
|
|
10
|
-
}
|
|
11
|
-
return true
|
|
12
|
-
} else {
|
|
13
|
-
//compat to draft-js
|
|
14
|
-
if (value?.getCurrentContent) {
|
|
15
|
-
/* istanbul ignore next */
|
|
16
|
-
return !value.getCurrentContent()?.hasText()
|
|
17
|
-
}
|
|
18
|
-
return isEmpty(value)
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function getLength(value: any) {
|
|
23
|
-
return isStr(value) ? stringLength(value) : value ? value.length : 0
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function extendSameRules(rules: IRegistryRules, names: Record<string, string>) {
|
|
27
|
-
each(names, (realName, name) => {
|
|
28
|
-
rules[name] = (value, rule, ...args) => rules[realName](value, { ...rule, [realName]: rule[name] }, ...args)
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const RULES: IRegistryRules = {
|
|
33
|
-
format(value, rule) {
|
|
34
|
-
if (isValidateEmpty(value)) return ''
|
|
35
|
-
if (rule.format) {
|
|
36
|
-
const format = getValidateFormats(rule.format)
|
|
37
|
-
if (format) {
|
|
38
|
-
return !new RegExp(format).test(value) ? rule.message : ''
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return ''
|
|
42
|
-
},
|
|
43
|
-
required(value, rule) {
|
|
44
|
-
if (rule.required === false) return ''
|
|
45
|
-
return isValidateEmpty(value) ? rule.message : ''
|
|
46
|
-
},
|
|
47
|
-
max(value, rule) {
|
|
48
|
-
if (isValidateEmpty(value)) return ''
|
|
49
|
-
const length = isNum(value) ? value : getLength(value)
|
|
50
|
-
const max = Number(rule.max)
|
|
51
|
-
return length > max ? rule.message : ''
|
|
52
|
-
},
|
|
53
|
-
min(value, rule) {
|
|
54
|
-
if (isValidateEmpty(value)) return ''
|
|
55
|
-
const length = isNum(value) ? value : getLength(value)
|
|
56
|
-
const min = Number(rule.min)
|
|
57
|
-
return length < min ? rule.message : ''
|
|
58
|
-
},
|
|
59
|
-
exclusiveMaximum(value, rule) {
|
|
60
|
-
if (isValidateEmpty(value)) return ''
|
|
61
|
-
const length = isNum(value) ? value : getLength(value)
|
|
62
|
-
const max = Number(rule.exclusiveMaximum)
|
|
63
|
-
return length >= max ? rule.message : ''
|
|
64
|
-
},
|
|
65
|
-
exclusiveMinimum(value, rule) {
|
|
66
|
-
if (isValidateEmpty(value)) return ''
|
|
67
|
-
const length = isNum(value) ? value : getLength(value)
|
|
68
|
-
const min = Number(rule.exclusiveMinimum)
|
|
69
|
-
return length <= min ? rule.message : ''
|
|
70
|
-
},
|
|
71
|
-
len(value, rule) {
|
|
72
|
-
if (isValidateEmpty(value)) return ''
|
|
73
|
-
const length = getLength(value)
|
|
74
|
-
const len = Number(rule.len)
|
|
75
|
-
return length !== len ? rule.message : ''
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
pattern(value, rule) {
|
|
79
|
-
if (isValidateEmpty(value)) return
|
|
80
|
-
if (!rule.pattern) return
|
|
81
|
-
return !new RegExp(rule.pattern).test(value) ? rule.message : undefined
|
|
82
|
-
},
|
|
83
|
-
async validator(value, rule, context, format) {
|
|
84
|
-
if (isFn(rule.validator)) {
|
|
85
|
-
const response = await Promise.resolve(rule.validator(value, rule, context, format))
|
|
86
|
-
if (isBool(response)) {
|
|
87
|
-
return !response ? rule.message : ''
|
|
88
|
-
} else {
|
|
89
|
-
return response
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/* istanbul ignore next */
|
|
93
|
-
throw new Error("The rule's validator property must be a function.")
|
|
94
|
-
},
|
|
95
|
-
whitespace(value, rule) {
|
|
96
|
-
if (isValidateEmpty(value)) return ''
|
|
97
|
-
if (rule.whitespace) {
|
|
98
|
-
return /^\s+$/.test(value) ? rule.message : ''
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
enum(value, rule) {
|
|
102
|
-
if (isValidateEmpty(value)) return ''
|
|
103
|
-
const enums = toArr(rule.enum)
|
|
104
|
-
return enums.indexOf(value) === -1 ? rule.message : ''
|
|
105
|
-
},
|
|
106
|
-
const(value, rule) {
|
|
107
|
-
if (isValidateEmpty(value)) return ''
|
|
108
|
-
return rule.const !== value ? rule.message : ''
|
|
109
|
-
},
|
|
110
|
-
multipleOf(value, rule) {
|
|
111
|
-
if (isValidateEmpty(value)) return ''
|
|
112
|
-
return Number(value) % Number(rule.multipleOf) !== 0 ? rule.message : ''
|
|
113
|
-
},
|
|
114
|
-
uniqueItems(value, rule) {
|
|
115
|
-
if (isValidateEmpty(value)) return ''
|
|
116
|
-
value = toArr(value)
|
|
117
|
-
return value.some((item: any, index: number) => {
|
|
118
|
-
for (let i = 0; i < value.length; i++) {
|
|
119
|
-
if (i !== index && !isEqual(value[i], item)) {
|
|
120
|
-
return false
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return true
|
|
124
|
-
})
|
|
125
|
-
? ''
|
|
126
|
-
: rule.message
|
|
127
|
-
},
|
|
128
|
-
maxProperties(value, rule) {
|
|
129
|
-
if (isValidateEmpty(value)) return ''
|
|
130
|
-
return Object.keys(value || {}).length <= Number(rule.maxProperties) ? '' : rule.message
|
|
131
|
-
},
|
|
132
|
-
minProperties(value, rule) {
|
|
133
|
-
if (isValidateEmpty(value)) return ''
|
|
134
|
-
return Object.keys(value || {}).length >= Number(rule.minProperties) ? '' : rule.message
|
|
135
|
-
},
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
extendSameRules(RULES, {
|
|
139
|
-
maximum: 'max',
|
|
140
|
-
minimum: 'min',
|
|
141
|
-
maxItems: 'max',
|
|
142
|
-
minItems: 'min',
|
|
143
|
-
maxLength: 'max',
|
|
144
|
-
minLength: 'min',
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
export default RULES
|