valgen 3.2.0 → 4.0.0-alpha.2
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 +6 -0
- package/README.md +12 -7
- package/cjs/core/constants.js +5 -0
- package/cjs/core/context.js +65 -0
- package/cjs/core/index.js +21 -0
- package/cjs/core/types.js +2 -0
- package/cjs/core/validation-error.js +11 -0
- package/cjs/core/validator.js +61 -0
- package/cjs/helpers/object.utils.js +19 -0
- package/cjs/helpers/string.utils.js +9 -0
- package/cjs/index.js +18 -0
- package/cjs/package.json +3 -0
- package/cjs/rules/index.js +38 -0
- package/cjs/rules/is-any.js +12 -0
- package/cjs/rules/is-array.js +38 -0
- package/cjs/rules/is-boolean.js +30 -0
- package/cjs/rules/is-date.js +63 -0
- package/cjs/rules/is-empty.js +68 -0
- package/cjs/rules/is-enum.js +30 -0
- package/cjs/rules/is-integer.js +26 -0
- package/cjs/rules/is-matches.js +24 -0
- package/cjs/rules/is-null.js +40 -0
- package/cjs/rules/is-number.js +26 -0
- package/cjs/rules/is-object-id.js +28 -0
- package/cjs/rules/is-object.js +103 -0
- package/cjs/rules/is-record.js +41 -0
- package/cjs/rules/is-string.js +79 -0
- package/cjs/rules/is-tuple.js +32 -0
- package/cjs/rules/length.js +41 -0
- package/cjs/rules/optional.js +44 -0
- package/cjs/rules/pipe.js +43 -0
- package/cjs/rules/range.js +89 -0
- package/cjs/rules/string-formats.js +296 -0
- package/cjs/rules/union.js +31 -0
- package/cjs/rules/utilities.js +31 -0
- package/esm/core/constants.js +2 -0
- package/esm/core/context.js +61 -0
- package/esm/core/index.js +5 -0
- package/esm/core/types.js +1 -0
- package/esm/core/validation-error.js +7 -0
- package/esm/core/validator.js +56 -0
- package/esm/helpers/object.utils.js +14 -0
- package/esm/helpers/string.utils.js +5 -0
- package/esm/index.js +2 -0
- package/esm/rules/index.js +22 -0
- package/esm/rules/is-any.js +8 -0
- package/esm/rules/is-array.js +34 -0
- package/esm/rules/is-boolean.js +26 -0
- package/esm/rules/is-date.js +55 -0
- package/esm/rules/is-empty.js +63 -0
- package/esm/rules/is-enum.js +26 -0
- package/esm/rules/is-integer.js +22 -0
- package/esm/rules/is-matches.js +20 -0
- package/esm/rules/is-null.js +34 -0
- package/esm/rules/is-number.js +22 -0
- package/esm/rules/is-object-id.js +21 -0
- package/esm/rules/is-object.js +99 -0
- package/esm/rules/is-record.js +37 -0
- package/esm/rules/is-string.js +70 -0
- package/esm/rules/is-tuple.js +28 -0
- package/esm/rules/length.js +35 -0
- package/esm/rules/optional.js +38 -0
- package/esm/rules/pipe.js +38 -0
- package/esm/rules/range.js +81 -0
- package/esm/rules/string-formats.js +266 -0
- package/esm/rules/union.js +27 -0
- package/esm/rules/utilities.js +26 -0
- package/package.json +33 -37
- package/typings/core/constants.d.ts +2 -0
- package/typings/core/context.d.ts +26 -0
- package/typings/core/index.d.ts +6 -0
- package/typings/core/types.d.ts +18 -0
- package/typings/core/validation-error.d.ts +5 -0
- package/typings/core/validator.d.ts +18 -0
- package/typings/helpers/object.utils.d.ts +2 -0
- package/typings/helpers/string.utils.d.ts +1 -0
- package/typings/index.d.ts +2 -0
- package/typings/rules/index.d.ts +22 -0
- package/typings/rules/is-any.d.ts +5 -0
- package/typings/rules/is-array.d.ts +7 -0
- package/typings/rules/is-boolean.d.ts +7 -0
- package/typings/rules/is-date.d.ts +19 -0
- package/typings/rules/is-empty.d.ts +13 -0
- package/typings/rules/is-enum.d.ts +9 -0
- package/typings/rules/is-integer.d.ts +7 -0
- package/typings/rules/is-matches.d.ts +9 -0
- package/typings/rules/is-null.d.ts +16 -0
- package/typings/rules/is-number.d.ts +7 -0
- package/typings/rules/is-object-id.d.ts +10 -0
- package/typings/rules/is-object.d.ts +22 -0
- package/typings/rules/is-record.d.ts +7 -0
- package/typings/rules/is-string.d.ts +42 -0
- package/typings/rules/is-tuple.d.ts +27 -0
- package/typings/rules/length.d.ts +21 -0
- package/typings/rules/optional.d.ts +17 -0
- package/typings/rules/pipe.d.ts +11 -0
- package/typings/rules/range.d.ts +48 -0
- package/typings/rules/string-formats.d.ts +140 -0
- package/typings/rules/union.d.ts +5 -0
- package/typings/rules/utilities.d.ts +11 -0
- package/lib/ArrayType.d.ts +0 -5
- package/lib/ArrayType.js +0 -43
- package/lib/DataType.d.ts +0 -25
- package/lib/DataType.js +0 -214
- package/lib/ObjectType.d.ts +0 -5
- package/lib/ObjectType.js +0 -78
- package/lib/SchemaError.d.ts +0 -3
- package/lib/SchemaError.js +0 -17
- package/lib/UnionType.d.ts +0 -5
- package/lib/UnionType.js +0 -54
- package/lib/Valgen.d.ts +0 -88
- package/lib/Valgen.js +0 -289
- package/lib/ValidationError.d.ts +0 -2
- package/lib/ValidationError.js +0 -4
- package/lib/factories/AnyFactory.d.ts +0 -28
- package/lib/factories/AnyFactory.js +0 -73
- package/lib/factories/ArrayFactory.d.ts +0 -16
- package/lib/factories/ArrayFactory.js +0 -148
- package/lib/factories/Base64Factory.d.ts +0 -12
- package/lib/factories/Base64Factory.js +0 -31
- package/lib/factories/BooleanFactory.d.ts +0 -12
- package/lib/factories/BooleanFactory.js +0 -49
- package/lib/factories/DateFactory.d.ts +0 -40
- package/lib/factories/DateFactory.js +0 -239
- package/lib/factories/FunctionFactory.d.ts +0 -12
- package/lib/factories/FunctionFactory.js +0 -28
- package/lib/factories/IntegerFactory.d.ts +0 -14
- package/lib/factories/IntegerFactory.js +0 -33
- package/lib/factories/NilFactory.d.ts +0 -4
- package/lib/factories/NilFactory.js +0 -20
- package/lib/factories/NumberFactory.d.ts +0 -36
- package/lib/factories/NumberFactory.js +0 -178
- package/lib/factories/ObjectFactory.d.ts +0 -38
- package/lib/factories/ObjectFactory.js +0 -326
- package/lib/factories/StringFactory.d.ts +0 -16
- package/lib/factories/StringFactory.js +0 -94
- package/lib/factories/UnionFactory.d.ts +0 -13
- package/lib/factories/UnionFactory.js +0 -62
- package/lib/index.d.ts +0 -19
- package/lib/index.js +0 -37
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is an "UUID".
|
|
5
|
+
* @validator isUUID
|
|
6
|
+
*/
|
|
7
|
+
export function isUUID(version, options) {
|
|
8
|
+
return validator('isUUID', function (input, context) {
|
|
9
|
+
if (input != null && typeof input === 'string' && validatorJS.isUUID(input, version))
|
|
10
|
+
return input;
|
|
11
|
+
context.failure(`{{label}} is not a valid UUID${version ? ' v' + version : ''}`);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Validates if value is a valid Email
|
|
16
|
+
* @validator isEmail
|
|
17
|
+
*/
|
|
18
|
+
export function isEmail(options) {
|
|
19
|
+
return validator('isEmail', function (input, context) {
|
|
20
|
+
if (typeof input === 'string' && validatorJS.isEmail(input, options))
|
|
21
|
+
return input;
|
|
22
|
+
context.failure(`{{label}} is not a valid a EMail`);
|
|
23
|
+
}, options);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Validates if value is a valid Email
|
|
27
|
+
* @validator isMobilePhone
|
|
28
|
+
*/
|
|
29
|
+
export function isMobilePhone(options) {
|
|
30
|
+
return validator('isMobilePhone', function (input, context) {
|
|
31
|
+
if (typeof input === 'string' && validatorJS.isMobilePhone(input, options?.locale, options))
|
|
32
|
+
return input;
|
|
33
|
+
context.failure(`{{label}} is not a valid a Mobile Phone Number`);
|
|
34
|
+
}, options);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Validates if value is an IP
|
|
38
|
+
* @validator isIP
|
|
39
|
+
*/
|
|
40
|
+
export function isIP(version, options) {
|
|
41
|
+
return validator('isIP', function (input, context) {
|
|
42
|
+
if (input != null && typeof input === 'string' && validatorJS.isIP(input, version))
|
|
43
|
+
return input;
|
|
44
|
+
context.failure(`{{label}} is not a valid IP${version ? ' v' + version : ''}`);
|
|
45
|
+
}, options);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Validates if value is an IP
|
|
49
|
+
* @validator isUUID
|
|
50
|
+
*/
|
|
51
|
+
export function isIPRange(version, options) {
|
|
52
|
+
return validator('isIPRange', function (input, context) {
|
|
53
|
+
if (input != null && typeof input === 'string' && validatorJS.isIPRange(input, version))
|
|
54
|
+
return input;
|
|
55
|
+
context.failure(`{{label}} is not a valid IP${version ? ' v' + version : ''} range`);
|
|
56
|
+
}, options);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Validates if value is an MACAddress
|
|
60
|
+
* @validator isMACAddress
|
|
61
|
+
*/
|
|
62
|
+
export function isMACAddress(options) {
|
|
63
|
+
return validator('isMACAddress', function (input, context) {
|
|
64
|
+
if (input != null && typeof input === 'string' && validatorJS.isMACAddress(input, options))
|
|
65
|
+
return input;
|
|
66
|
+
context.failure(`{{label}} is not a valid MAC address`);
|
|
67
|
+
}, options);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Validates if value is a port number
|
|
71
|
+
* @validator isPort
|
|
72
|
+
*/
|
|
73
|
+
export function isPort(options) {
|
|
74
|
+
return validator('isPort', function (input, context) {
|
|
75
|
+
if (input != null && typeof input === 'string' && validatorJS.isPort(input))
|
|
76
|
+
return input;
|
|
77
|
+
context.failure(`{{label}} is not a valid port number`);
|
|
78
|
+
}, options);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Validates if value is an MACAddress
|
|
82
|
+
* @validator isURL
|
|
83
|
+
*/
|
|
84
|
+
export function isURL(options) {
|
|
85
|
+
return validator('isURL', function (input, context) {
|
|
86
|
+
if (input != null && typeof input === 'string' && validatorJS.isURL(input, options))
|
|
87
|
+
return input;
|
|
88
|
+
context.failure(`{{label}} is not a valid URL`);
|
|
89
|
+
}, options);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Validates if value is a "Base64" string.
|
|
93
|
+
* @validator isBase64
|
|
94
|
+
*/
|
|
95
|
+
export function isBase64(options) {
|
|
96
|
+
return validator('isBase64', function (input, context) {
|
|
97
|
+
if (typeof input === 'string' && validatorJS.isBase64(input, options))
|
|
98
|
+
return input;
|
|
99
|
+
context.failure(`{{label}} is not a valid a Base64 string`);
|
|
100
|
+
}, options);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Validates if value is a BIC (Bank Identification Code) or SWIFT code
|
|
104
|
+
* @validator isBIC
|
|
105
|
+
*/
|
|
106
|
+
export function isBIC(options) {
|
|
107
|
+
return validator('isBIC', function (input, context) {
|
|
108
|
+
if (typeof input === 'string' && validatorJS.isBIC(input))
|
|
109
|
+
return input;
|
|
110
|
+
context.failure(`{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`);
|
|
111
|
+
}, options);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Validates if value is a "Base64" formatted string.
|
|
115
|
+
* @validator isCreditCard
|
|
116
|
+
*/
|
|
117
|
+
export function isCreditCard(options) {
|
|
118
|
+
return validator('isCreditCard', function (input, context) {
|
|
119
|
+
if (typeof input === 'string' && validatorJS.isCreditCard(input, options))
|
|
120
|
+
return input;
|
|
121
|
+
context.failure(`{{label}} is not a valid Credit Card number`);
|
|
122
|
+
}, options);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Validates if value is an IBAN (International Bank Account Number)
|
|
126
|
+
* @validator isEAN
|
|
127
|
+
*/
|
|
128
|
+
export function isIBAN(options) {
|
|
129
|
+
return validator('isIBAN', function (input, context) {
|
|
130
|
+
if (typeof input === 'string' && validatorJS.isIBAN(input))
|
|
131
|
+
return input;
|
|
132
|
+
context.failure(`{{label}} is not a valid IBAN (International Bank Account Number)`);
|
|
133
|
+
}, options);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Validates if value is an passport number
|
|
137
|
+
* @validator isPassportNumber
|
|
138
|
+
*/
|
|
139
|
+
export function isPassportNumber(countryCode, options) {
|
|
140
|
+
return validator('isPassportNumber', function (input, context) {
|
|
141
|
+
if (typeof input === 'string' && validatorJS.isPassportNumber(input, countryCode))
|
|
142
|
+
return input;
|
|
143
|
+
context.failure(`{{label}} is not a valid ${countryCode} PassportNumber)`);
|
|
144
|
+
}, options);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Validates if value is an EAN (European Article Number)
|
|
148
|
+
* @validator isEAN
|
|
149
|
+
*/
|
|
150
|
+
export function isEAN(options) {
|
|
151
|
+
return validator('isEAN', function (input, context) {
|
|
152
|
+
if (typeof input === 'string' && validatorJS.isEAN(input))
|
|
153
|
+
return input;
|
|
154
|
+
context.failure(`{{label}} is not a valid EAN (European Article Number)`);
|
|
155
|
+
}, options);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Validates if value is an FQDN
|
|
159
|
+
* @validator isFQDN
|
|
160
|
+
*/
|
|
161
|
+
export function isFQDN(options) {
|
|
162
|
+
return validator('isFQDN', function (input, context) {
|
|
163
|
+
if (typeof input === 'string' && validatorJS.isFQDN(input, options))
|
|
164
|
+
return input;
|
|
165
|
+
context.failure(`{{label}} is not valid FQDN`);
|
|
166
|
+
}, options);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Validates if value is an ISSN
|
|
170
|
+
* @validator isISSN
|
|
171
|
+
*/
|
|
172
|
+
export function isISSN(options) {
|
|
173
|
+
return validator('isISSN', function (input, context) {
|
|
174
|
+
if (typeof input === 'string' && validatorJS.isISSN(input, options))
|
|
175
|
+
return input;
|
|
176
|
+
context.failure(`{{label}} is not a valid ISSN`);
|
|
177
|
+
}, options);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Validates if value is an VAT number
|
|
181
|
+
* @validator isVAT
|
|
182
|
+
*/
|
|
183
|
+
export function isVAT(countryCode, options) {
|
|
184
|
+
return validator('isVAT', function (input, context) {
|
|
185
|
+
if (typeof input === 'string' && validatorJS.isVAT(input, countryCode))
|
|
186
|
+
return input;
|
|
187
|
+
context.failure(`{{label}} is not a valid VAT number`);
|
|
188
|
+
}, options);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Validates if value is a BTC address.
|
|
192
|
+
* @validator isBtcAddress
|
|
193
|
+
*/
|
|
194
|
+
export function isBtcAddress(options) {
|
|
195
|
+
return validator('isBtcAddress', function (input, context) {
|
|
196
|
+
if (typeof input === 'string' && validatorJS.isBtcAddress(input))
|
|
197
|
+
return input;
|
|
198
|
+
context.failure(`{{label}} is not a valid a BTC address`);
|
|
199
|
+
}, options);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Validates if value is a ETH (Ethereum) address.
|
|
203
|
+
* @validator isBtcAddress
|
|
204
|
+
*/
|
|
205
|
+
export function isETHAddress(options) {
|
|
206
|
+
return validator('isETHAddress', function (input, context) {
|
|
207
|
+
if (typeof input === 'string' && validatorJS.isEthereumAddress(input))
|
|
208
|
+
return input;
|
|
209
|
+
context.failure(`{{label}} is not a valid a ETH (Ethereum) address`);
|
|
210
|
+
}, options);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Validates if value a hash of type algorithm
|
|
214
|
+
* @validator isHash
|
|
215
|
+
*/
|
|
216
|
+
export function isHash(algorithm, options) {
|
|
217
|
+
return validator('isHash', function (input, context) {
|
|
218
|
+
if (typeof input === 'string' && validatorJS.isHash(input, algorithm))
|
|
219
|
+
return input;
|
|
220
|
+
context.failure(`{{label}} is not a valid ${algorithm} hash`);
|
|
221
|
+
}, options);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Validates if value is a Hex Color
|
|
225
|
+
* @validator isHexColor
|
|
226
|
+
*/
|
|
227
|
+
export function isHexColor(options) {
|
|
228
|
+
return validator('isHexColor', function (input, context) {
|
|
229
|
+
if (typeof input === 'string' && validatorJS.isHexColor(input))
|
|
230
|
+
return input;
|
|
231
|
+
context.failure(`{{label}} is not a valid Hex Color`);
|
|
232
|
+
}, options);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Validates if value a valid JWT token
|
|
236
|
+
* @validator isHash
|
|
237
|
+
*/
|
|
238
|
+
export function isJWT(options) {
|
|
239
|
+
return validator('isJWT', function (input, context) {
|
|
240
|
+
if (typeof input === 'string' && validatorJS.isJWT(input))
|
|
241
|
+
return input;
|
|
242
|
+
context.failure(`{{label}} is not a valid JWT token`);
|
|
243
|
+
}, options);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Validates if value a Lowercase string
|
|
247
|
+
* @validator isLowercase
|
|
248
|
+
*/
|
|
249
|
+
export function isLowercase(options) {
|
|
250
|
+
return validator('isLowercase', function (input, context) {
|
|
251
|
+
if (typeof input === 'string' && validatorJS.isLowercase(input))
|
|
252
|
+
return input;
|
|
253
|
+
context.failure(`{{label}} is not a lowercase string`);
|
|
254
|
+
}, options);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Validates if value a Uppercase string
|
|
258
|
+
* @validator isUppercase
|
|
259
|
+
*/
|
|
260
|
+
export function isUppercase(options) {
|
|
261
|
+
return validator('isLowercase', function (input, context) {
|
|
262
|
+
if (typeof input === 'string' && validatorJS.isUppercase(input))
|
|
263
|
+
return input;
|
|
264
|
+
context.failure(`{{label}} is not an uppercase string`);
|
|
265
|
+
}, options);
|
|
266
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { validator } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
*/
|
|
5
|
+
export function union(codecs) {
|
|
6
|
+
const l = codecs.length;
|
|
7
|
+
return validator('union', function (input, context) {
|
|
8
|
+
let i;
|
|
9
|
+
let c;
|
|
10
|
+
let v;
|
|
11
|
+
let passed = false;
|
|
12
|
+
for (i = 0; i < l; i++) {
|
|
13
|
+
c = codecs[i];
|
|
14
|
+
try {
|
|
15
|
+
v = c(input, context);
|
|
16
|
+
passed = true;
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
//
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (passed)
|
|
24
|
+
return v;
|
|
25
|
+
context.failure(`{{location}} didn't match any of required format`);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isValidator, kValidatorFn, validator } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Forwards codec process to a sub codec. Useful for circular checks
|
|
4
|
+
* @validator forwardRef
|
|
5
|
+
*/
|
|
6
|
+
export function forwardRef(fn) {
|
|
7
|
+
return validator('forwardRef', function (input, context) {
|
|
8
|
+
const nested = fn(context);
|
|
9
|
+
return nested[kValidatorFn](input, context, nested);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export function iif(check, _then, _else) {
|
|
13
|
+
return validator('iif', function (input, context) {
|
|
14
|
+
let c = _else;
|
|
15
|
+
try {
|
|
16
|
+
if (check(input) !== undefined)
|
|
17
|
+
c = _then;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
// ignored
|
|
21
|
+
}
|
|
22
|
+
if (isValidator(c))
|
|
23
|
+
return c[kValidatorFn](input, context, c);
|
|
24
|
+
return c;
|
|
25
|
+
});
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -1,56 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valgen",
|
|
3
|
-
"description": "Fast type
|
|
4
|
-
"version": "
|
|
3
|
+
"description": "Fast runtime type validator, converter and io (encoding/decoding) library",
|
|
4
|
+
"version": "4.0.0-alpha.2",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"contributors": [
|
|
7
|
-
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
8
|
-
"Arhun Hınçalan <ahincalan@hotmail.com>"
|
|
7
|
+
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
9
8
|
],
|
|
10
|
-
"main": "./lib/index.js",
|
|
11
|
-
"types": "./lib/index.d.ts",
|
|
12
9
|
"license": "MIT",
|
|
13
10
|
"repository": {
|
|
14
11
|
"type": "git",
|
|
15
12
|
"url": "https://github.com/panates/valgen.git"
|
|
16
13
|
},
|
|
17
|
-
"keywords": [
|
|
18
|
-
"javascript",
|
|
19
|
-
"validate",
|
|
20
|
-
"validation",
|
|
21
|
-
"validator",
|
|
22
|
-
"type",
|
|
23
|
-
"raml",
|
|
24
|
-
"swagger",
|
|
25
|
-
"generator"
|
|
26
|
-
],
|
|
27
14
|
"dependencies": {
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"putil-promisify": "^1.7.1",
|
|
31
|
-
"putil-varhelpers": "^0.0.5"
|
|
15
|
+
"dayjs": "^1.11.8",
|
|
16
|
+
"validator": "^13.9.0"
|
|
32
17
|
},
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
18
|
+
"type": "module",
|
|
19
|
+
"types": "typings/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"require": "./cjs/index.js",
|
|
23
|
+
"default": "./esm/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./cjs": "./cjs/index.js",
|
|
26
|
+
"./esm": "./esm/index.js"
|
|
38
27
|
},
|
|
39
|
-
"peerDependencies": {},
|
|
40
28
|
"engines": {
|
|
41
|
-
"node": ">=
|
|
29
|
+
"node": ">= 16.0"
|
|
42
30
|
},
|
|
43
31
|
"files": [
|
|
32
|
+
"cjs/",
|
|
33
|
+
"esm/",
|
|
34
|
+
"typings/",
|
|
44
35
|
"LICENSE",
|
|
45
36
|
"README.md",
|
|
46
|
-
"
|
|
37
|
+
"CHANGELOG.md"
|
|
47
38
|
],
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
|
|
39
|
+
"keywords": [
|
|
40
|
+
"validate",
|
|
41
|
+
"validation",
|
|
42
|
+
"validator",
|
|
43
|
+
"check",
|
|
44
|
+
"encoding",
|
|
45
|
+
"decoding",
|
|
46
|
+
"coerce",
|
|
47
|
+
"convert",
|
|
48
|
+
"converter",
|
|
49
|
+
"transform",
|
|
50
|
+
"type"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ErrorIssue, ExecutionOptions, OnFailFunction } from './types';
|
|
2
|
+
import type { Validator } from './validator';
|
|
3
|
+
export declare namespace Context {
|
|
4
|
+
interface InputDetails {
|
|
5
|
+
context?: string;
|
|
6
|
+
root?: string;
|
|
7
|
+
value?: any;
|
|
8
|
+
label?: string;
|
|
9
|
+
location?: string;
|
|
10
|
+
property?: string | number;
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export declare class Context implements ExecutionOptions {
|
|
15
|
+
rule: Validator<any>;
|
|
16
|
+
errors: ErrorIssue[];
|
|
17
|
+
maxErrors?: number;
|
|
18
|
+
onFail?: OnFailFunction;
|
|
19
|
+
coerce?: boolean;
|
|
20
|
+
input: Context.InputDetails;
|
|
21
|
+
parent?: Context;
|
|
22
|
+
root?: Context;
|
|
23
|
+
constructor(rule: Validator<any>, options?: ExecutionOptions);
|
|
24
|
+
failure(message: Partial<ErrorIssue> | string | Error): void;
|
|
25
|
+
extend(rule: Validator<any, any>, options?: ExecutionOptions): Context;
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Context } from './context';
|
|
2
|
+
export interface ErrorIssue {
|
|
3
|
+
rule: string;
|
|
4
|
+
message: string;
|
|
5
|
+
value: any;
|
|
6
|
+
root?: string;
|
|
7
|
+
context?: string;
|
|
8
|
+
property?: string;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
export type OnFailFunction = (issue: ErrorIssue, context: Context, _super?: OnFailFunction) => string | Omit<ErrorIssue, 'id' | 'input'>;
|
|
12
|
+
export interface ValidationOptions {
|
|
13
|
+
onFail?: OnFailFunction;
|
|
14
|
+
}
|
|
15
|
+
export interface ExecutionOptions extends ValidationOptions {
|
|
16
|
+
coerce?: boolean;
|
|
17
|
+
maxErrors?: number;
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Nullish } from 'ts-gems';
|
|
2
|
+
import { kValidatorFn } from './constants.js';
|
|
3
|
+
import { Context } from './context.js';
|
|
4
|
+
import { ErrorIssue, ExecutionOptions, ValidationOptions } from './types.js';
|
|
5
|
+
export type ValidateFunction<T, I = T, R extends Validator<T, I> = Validator<T, I>> = (input: I, context: Context, _this: R) => Nullish<T>;
|
|
6
|
+
export interface Validator<T, I = unknown, O extends ExecutionOptions = ExecutionOptions> {
|
|
7
|
+
(input: I, options?: O, context?: Context): T;
|
|
8
|
+
silent(input: I, options?: O, context?: Context): {
|
|
9
|
+
value?: T;
|
|
10
|
+
errors?: ErrorIssue[];
|
|
11
|
+
};
|
|
12
|
+
id: string;
|
|
13
|
+
args?: Record<string, any>;
|
|
14
|
+
[kValidatorFn]: ValidateFunction<T, I>;
|
|
15
|
+
}
|
|
16
|
+
export declare function validator<T, I = T, O extends ExecutionOptions = ExecutionOptions>(fn: ValidateFunction<T, I>, validatorOptions?: ValidationOptions): Validator<T, I, O>;
|
|
17
|
+
export declare function validator<T, I = T, O extends ExecutionOptions = ExecutionOptions>(id: string, fn: ValidateFunction<T, I>, validatorOptions?: ValidationOptions): Validator<T, I, O>;
|
|
18
|
+
export declare function isValidator(x: any): x is Validator<any, any>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function camelCase(v: string): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * from './is-any.js';
|
|
2
|
+
export * from './is-array.js';
|
|
3
|
+
export * from './is-boolean.js';
|
|
4
|
+
export * from './is-date.js';
|
|
5
|
+
export * from './is-empty.js';
|
|
6
|
+
export * from './is-enum.js';
|
|
7
|
+
export * from './is-integer.js';
|
|
8
|
+
export * from './is-null.js';
|
|
9
|
+
export * from './is-matches.js';
|
|
10
|
+
export * from './is-number.js';
|
|
11
|
+
export * from './is-object.js';
|
|
12
|
+
export * from './is-object-id.js';
|
|
13
|
+
export * from './is-record.js';
|
|
14
|
+
export * from './is-string.js';
|
|
15
|
+
export * from './is-tuple.js';
|
|
16
|
+
export * from './length.js';
|
|
17
|
+
export * from './optional.js';
|
|
18
|
+
export * from './pipe.js';
|
|
19
|
+
export * from './range.js';
|
|
20
|
+
export * from './string-formats.js';
|
|
21
|
+
export * from './union.js';
|
|
22
|
+
export * from './utilities.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationOptions, Validator } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is "array" and applies validation for each item.
|
|
4
|
+
* Converts input value to array if coerce option is set to 'true'.
|
|
5
|
+
* @validator isArray
|
|
6
|
+
*/
|
|
7
|
+
export declare function isArray<T, I>(itemValidator?: Validator<T, I>, options?: ValidationOptions): Validator<T[], I | I[], import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is "boolean".
|
|
4
|
+
* Converts input value to boolean if coerce option is set to 'true'.
|
|
5
|
+
* @validator isBoolean
|
|
6
|
+
*/
|
|
7
|
+
export declare function isBoolean(options?: ValidationOptions): import("../core/validator.js").Validator<boolean | undefined, unknown, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
export interface IsDateOptions extends ValidationOptions {
|
|
3
|
+
format?: string | string[];
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Validates if value is instance of "Date".
|
|
7
|
+
* Converts input value to Date if coerce option is set to 'true'.
|
|
8
|
+
* @validator isDate
|
|
9
|
+
*/
|
|
10
|
+
export declare function isDate(options?: IsDateOptions): import("../core/validator.js").Validator<Date, string | number | Date, import("../core/types.js").ExecutionOptions>;
|
|
11
|
+
export interface IsDateStringOptions extends ValidationOptions {
|
|
12
|
+
format?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Validates if value is DFS (date formatted string).
|
|
16
|
+
* Converts input value to DFS if coerce option is set to 'true'.
|
|
17
|
+
* @validator isDateString
|
|
18
|
+
*/
|
|
19
|
+
export declare function isDateString(options?: IsDateStringOptions): import("../core/validator.js").Validator<string, string | number | Date, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
type IsEmptyInput = string | any[] | object | Set<any> | Map<any, any>;
|
|
3
|
+
/**
|
|
4
|
+
* Checks if value is an empty. Value should be string, array, set, map or object
|
|
5
|
+
* @validator isEmpty
|
|
6
|
+
*/
|
|
7
|
+
export declare function isEmpty(options?: ValidationOptions): import("../core/validator.js").Validator<IsEmptyInput, IsEmptyInput, import("../core/types.js").ExecutionOptions>;
|
|
8
|
+
/**
|
|
9
|
+
* Checks if value is a not empty. Value should be string, array, set, map or object
|
|
10
|
+
* @validator isNotEmpty
|
|
11
|
+
*/
|
|
12
|
+
export declare function isNotEmpty(options?: ValidationOptions): import("../core/validator.js").Validator<IsEmptyInput, IsEmptyInput, import("../core/types.js").ExecutionOptions>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ValidationOptions, Validator } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if given value is one of enum values.
|
|
4
|
+
* @validator isEnum
|
|
5
|
+
*/
|
|
6
|
+
export declare function isEnum<T1, I1>(values: I1 | I1[], options?: ValidationOptions & {
|
|
7
|
+
caseInSensitive?: boolean;
|
|
8
|
+
enumName?: string;
|
|
9
|
+
}): Validator<T1, I1 | I1[]>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is "integer".
|
|
4
|
+
* Converts input value to integer number if coerce option is set to 'true'.
|
|
5
|
+
* @validator isInteger
|
|
6
|
+
*/
|
|
7
|
+
export declare function isInteger(options?: ValidationOptions): import("../core/validator.js").Validator<number, unknown, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
export interface IsMatchesOptions extends ValidationOptions {
|
|
3
|
+
formatName?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Coerces given value to "UUID" format or returns undefined if nullish
|
|
7
|
+
* @validator uuid
|
|
8
|
+
*/
|
|
9
|
+
export declare function isMatches(format: string | RegExp, options?: IsMatchesOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is "null".
|
|
4
|
+
* @validator isNull
|
|
5
|
+
*/
|
|
6
|
+
export declare function isNull(options?: ValidationOptions): import("../core/validator.js").Validator<null, unknown, import("../core/types.js").ExecutionOptions>;
|
|
7
|
+
/**
|
|
8
|
+
* Validates if value is not "undefined" nor "null"
|
|
9
|
+
* @validator isDefined
|
|
10
|
+
*/
|
|
11
|
+
export declare function isDefined(options?: ValidationOptions): import("../core/validator.js").Validator<any, unknown, import("../core/types.js").ExecutionOptions>;
|
|
12
|
+
/**
|
|
13
|
+
* Validates if value is "undefined"
|
|
14
|
+
* @validator isNotDefined
|
|
15
|
+
*/
|
|
16
|
+
export declare function isUndefined(options?: ValidationOptions): import("../core/validator.js").Validator<any, unknown, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is "number".
|
|
4
|
+
* Converts input value to number if coerce option is set to 'true'.
|
|
5
|
+
* @validator isNumber
|
|
6
|
+
*/
|
|
7
|
+
export declare function isNumber(options?: ValidationOptions): import("../core/validator.js").Validator<number, unknown, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
export declare interface ObjectIdLike {
|
|
3
|
+
id: string | Uint8Array;
|
|
4
|
+
toHexString(): string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Validates if value is an "ObjectId".
|
|
8
|
+
* @validator isObjectId
|
|
9
|
+
*/
|
|
10
|
+
export declare function isObjectId(options?: ValidationOptions): import("../core/validator.js").Validator<string | ObjectIdLike | Uint8Array, unknown, import("../core/types.js").ExecutionOptions>;
|