valgen 5.1.0 → 5.2.1

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 CHANGED
@@ -1,3 +1,17 @@
1
+ # v5.2.1
2
+ [2024-03-27]
3
+
4
+ ### Changes
5
+
6
+ * Updated dependencies ([`117bab5`](https://github.com/panates/valgen/commit/117bab58f731c1572959668dd4d6fca36d779433))
7
+
8
+ # v5.2.0
9
+ [2024-01-04]
10
+
11
+ ### Changes
12
+
13
+ * Now validator options persist in context ([`418901f`](https://github.com/panates/valgen/commit/418901f74ee266d84e50ee82ba5a7221bd2be627))
14
+
1
15
  # v5.1.0
2
16
  [2023-12-23]
3
17
 
@@ -9,14 +9,7 @@ const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
9
9
  class Context {
10
10
  constructor(options) {
11
11
  this.errors = [];
12
- if (options?.maxErrors != null)
13
- this.maxErrors = options.maxErrors;
14
- if (typeof options?.onFail === 'function')
15
- this.onFail = options.onFail;
16
- if (options?.coerce != null)
17
- this.coerce = options?.coerce;
18
- if (options?.label != null)
19
- this.label = options?.label;
12
+ Object.assign(this, options);
20
13
  }
21
14
  fail(rule, message, value, details) {
22
15
  const issue = (0, omit_undefined_js_1.omitUndefined)({
@@ -1,10 +1,30 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
4
24
  };
5
25
  Object.defineProperty(exports, "__esModule", { value: true });
6
26
  exports.isHexadecimal = exports.isDecimal = exports.isAscii = exports.isAlphanumeric = exports.isAlpha = exports.isUppercase = exports.isLowercase = exports.isJWT = exports.isHexColor = exports.isHash = exports.isETHAddress = exports.isBtcAddress = exports.isVAT = exports.isISSN = exports.isFQDN = exports.isEAN = exports.isPassportNumber = exports.isIBAN = exports.isCreditCard = exports.isBIC = exports.isBase64 = exports.isURL = exports.isPort = exports.isMACAddress = exports.isIPRange = exports.isIP = exports.isMobilePhone = exports.isEmail = exports.isUUID = void 0;
7
- const validator_1 = __importDefault(require("validator"));
27
+ const validatorJS = __importStar(require("validator"));
8
28
  const index_js_1 = require("../core/index.js");
9
29
  /**
10
30
  * Validates if value is an "UUID".
@@ -12,7 +32,7 @@ const index_js_1 = require("../core/index.js");
12
32
  */
13
33
  function isUUID(version, options) {
14
34
  return (0, index_js_1.validator)('isUUID', function (input, context, _this) {
15
- if (input != null && typeof input === 'string' && validator_1.default.isUUID(input, version))
35
+ if (input != null && typeof input === 'string' && validatorJS.isUUID(input, version))
16
36
  return input;
17
37
  context.fail(_this, `{{label}} is not a valid UUID${version ? ' v' + version : ''}`, input);
18
38
  }, options);
@@ -24,7 +44,7 @@ exports.isUUID = isUUID;
24
44
  */
25
45
  function isEmail(options) {
26
46
  return (0, index_js_1.validator)('isEmail', function (input, context, _this) {
27
- if (typeof input === 'string' && validator_1.default.isEmail(input, options))
47
+ if (typeof input === 'string' && validatorJS.isEmail(input, options))
28
48
  return input;
29
49
  context.fail(_this, `{{label}} is not a valid a EMail`, input);
30
50
  }, options);
@@ -36,7 +56,7 @@ exports.isEmail = isEmail;
36
56
  */
37
57
  function isMobilePhone(options) {
38
58
  return (0, index_js_1.validator)('isMobilePhone', function (input, context, _this) {
39
- if (typeof input === 'string' && validator_1.default.isMobilePhone(input, options?.locale, options))
59
+ if (typeof input === 'string' && validatorJS.isMobilePhone(input, options?.locale, options))
40
60
  return input;
41
61
  context.fail(_this, `{{label}} is not a valid a Mobile Phone Number`, input);
42
62
  }, options);
@@ -48,7 +68,7 @@ exports.isMobilePhone = isMobilePhone;
48
68
  */
49
69
  function isIP(version, options) {
50
70
  return (0, index_js_1.validator)('isIP', function (input, context, _this) {
51
- if (input != null && typeof input === 'string' && validator_1.default.isIP(input, version))
71
+ if (input != null && typeof input === 'string' && validatorJS.isIP(input, version))
52
72
  return input;
53
73
  context.fail(_this, `{{label}} is not a valid IP${version ? ' v' + version : ''}`, input);
54
74
  }, options);
@@ -60,7 +80,7 @@ exports.isIP = isIP;
60
80
  */
61
81
  function isIPRange(version, options) {
62
82
  return (0, index_js_1.validator)('isIPRange', function (input, context, _this) {
63
- if (input != null && typeof input === 'string' && validator_1.default.isIPRange(input, version))
83
+ if (input != null && typeof input === 'string' && validatorJS.isIPRange(input, version))
64
84
  return input;
65
85
  context.fail(_this, `{{label}} is not a valid IP${version ? ' v' + version : ''} range`, input);
66
86
  }, options);
@@ -72,7 +92,7 @@ exports.isIPRange = isIPRange;
72
92
  */
73
93
  function isMACAddress(options) {
74
94
  return (0, index_js_1.validator)('isMACAddress', function (input, context, _this) {
75
- if (input != null && typeof input === 'string' && validator_1.default.isMACAddress(input, options))
95
+ if (input != null && typeof input === 'string' && validatorJS.isMACAddress(input, options))
76
96
  return input;
77
97
  context.fail(_this, `{{label}} is not a valid MAC address`, input);
78
98
  }, options);
@@ -84,7 +104,7 @@ exports.isMACAddress = isMACAddress;
84
104
  */
85
105
  function isPort(options) {
86
106
  return (0, index_js_1.validator)('isPort', function (input, context, _this) {
87
- if (input != null && typeof input === 'string' && validator_1.default.isPort(input))
107
+ if (input != null && typeof input === 'string' && validatorJS.isPort(input))
88
108
  return input;
89
109
  context.fail(_this, `{{label}} is not a valid port number`, input);
90
110
  }, options);
@@ -96,7 +116,7 @@ exports.isPort = isPort;
96
116
  */
97
117
  function isURL(options) {
98
118
  return (0, index_js_1.validator)('isURL', function (input, context, _this) {
99
- if (input != null && typeof input === 'string' && validator_1.default.isURL(input, options))
119
+ if (input != null && typeof input === 'string' && validatorJS.isURL(input, options))
100
120
  return input;
101
121
  context.fail(_this, `{{label}} is not a valid URL`, input);
102
122
  }, options);
@@ -108,7 +128,7 @@ exports.isURL = isURL;
108
128
  */
109
129
  function isBase64(options) {
110
130
  return (0, index_js_1.validator)('isBase64', function (input, context, _this) {
111
- if (typeof input === 'string' && validator_1.default.isBase64(input, options))
131
+ if (typeof input === 'string' && validatorJS.isBase64(input, options))
112
132
  return input;
113
133
  context.fail(_this, `{{label}} is not a valid a Base64 string`, input);
114
134
  }, options);
@@ -120,7 +140,7 @@ exports.isBase64 = isBase64;
120
140
  */
121
141
  function isBIC(options) {
122
142
  return (0, index_js_1.validator)('isBIC', function (input, context, _this) {
123
- if (typeof input === 'string' && validator_1.default.isBIC(input))
143
+ if (typeof input === 'string' && validatorJS.isBIC(input))
124
144
  return input;
125
145
  context.fail(_this, `{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`, input);
126
146
  }, options);
@@ -132,7 +152,7 @@ exports.isBIC = isBIC;
132
152
  */
133
153
  function isCreditCard(options) {
134
154
  return (0, index_js_1.validator)('isCreditCard', function (input, context, _this) {
135
- if (typeof input === 'string' && validator_1.default.isCreditCard(input, options))
155
+ if (typeof input === 'string' && validatorJS.isCreditCard(input, options))
136
156
  return input;
137
157
  context.fail(_this, `{{label}} is not a valid Credit Card number`, input);
138
158
  }, options);
@@ -144,7 +164,7 @@ exports.isCreditCard = isCreditCard;
144
164
  */
145
165
  function isIBAN(options) {
146
166
  return (0, index_js_1.validator)('isIBAN', function (input, context, _this) {
147
- if (typeof input === 'string' && validator_1.default.isIBAN(input))
167
+ if (typeof input === 'string' && validatorJS.isIBAN(input))
148
168
  return input;
149
169
  context.fail(_this, `{{label}} is not a valid IBAN (International Bank Account Number)`, input);
150
170
  }, options);
@@ -156,7 +176,7 @@ exports.isIBAN = isIBAN;
156
176
  */
157
177
  function isPassportNumber(countryCode, options) {
158
178
  return (0, index_js_1.validator)('isPassportNumber', function (input, context, _this) {
159
- if (typeof input === 'string' && validator_1.default.isPassportNumber(input, countryCode))
179
+ if (typeof input === 'string' && validatorJS.isPassportNumber(input, countryCode))
160
180
  return input;
161
181
  context.fail(_this, `{{label}} is not a valid ${countryCode} PassportNumber)`, input);
162
182
  }, options);
@@ -168,7 +188,7 @@ exports.isPassportNumber = isPassportNumber;
168
188
  */
169
189
  function isEAN(options) {
170
190
  return (0, index_js_1.validator)('isEAN', function (input, context, _this) {
171
- if (typeof input === 'string' && validator_1.default.isEAN(input))
191
+ if (typeof input === 'string' && validatorJS.isEAN(input))
172
192
  return input;
173
193
  context.fail(_this, `{{label}} is not a valid EAN (European Article Number)`, input);
174
194
  }, options);
@@ -180,7 +200,7 @@ exports.isEAN = isEAN;
180
200
  */
181
201
  function isFQDN(options) {
182
202
  return (0, index_js_1.validator)('isFQDN', function (input, context, _this) {
183
- if (typeof input === 'string' && validator_1.default.isFQDN(input, options))
203
+ if (typeof input === 'string' && validatorJS.isFQDN(input, options))
184
204
  return input;
185
205
  context.fail(_this, `{{label}} is not valid FQDN`, input);
186
206
  }, options);
@@ -192,7 +212,7 @@ exports.isFQDN = isFQDN;
192
212
  */
193
213
  function isISSN(options) {
194
214
  return (0, index_js_1.validator)('isISSN', function (input, context, _this) {
195
- if (typeof input === 'string' && validator_1.default.isISSN(input, options))
215
+ if (typeof input === 'string' && validatorJS.isISSN(input, options))
196
216
  return input;
197
217
  context.fail(_this, `{{label}} is not a valid ISSN`, input);
198
218
  }, options);
@@ -204,7 +224,7 @@ exports.isISSN = isISSN;
204
224
  */
205
225
  function isVAT(countryCode, options) {
206
226
  return (0, index_js_1.validator)('isVAT', function (input, context, _this) {
207
- if (typeof input === 'string' && validator_1.default.isVAT(input, countryCode))
227
+ if (typeof input === 'string' && validatorJS.isVAT(input, countryCode))
208
228
  return input;
209
229
  context.fail(_this, `{{label}} is not a valid VAT number`, input);
210
230
  }, options);
@@ -216,7 +236,7 @@ exports.isVAT = isVAT;
216
236
  */
217
237
  function isBtcAddress(options) {
218
238
  return (0, index_js_1.validator)('isBtcAddress', function (input, context, _this) {
219
- if (typeof input === 'string' && validator_1.default.isBtcAddress(input))
239
+ if (typeof input === 'string' && validatorJS.isBtcAddress(input))
220
240
  return input;
221
241
  context.fail(_this, `{{label}} is not a valid a BTC address`, input);
222
242
  }, options);
@@ -228,7 +248,7 @@ exports.isBtcAddress = isBtcAddress;
228
248
  */
229
249
  function isETHAddress(options) {
230
250
  return (0, index_js_1.validator)('isETHAddress', function (input, context, _this) {
231
- if (typeof input === 'string' && validator_1.default.isEthereumAddress(input))
251
+ if (typeof input === 'string' && validatorJS.isEthereumAddress(input))
232
252
  return input;
233
253
  context.fail(_this, `{{label}} is not a valid a ETH (Ethereum) address`, input);
234
254
  }, options);
@@ -240,7 +260,7 @@ exports.isETHAddress = isETHAddress;
240
260
  */
241
261
  function isHash(algorithm, options) {
242
262
  return (0, index_js_1.validator)('isHash', function (input, context, _this) {
243
- if (typeof input === 'string' && validator_1.default.isHash(input, algorithm))
263
+ if (typeof input === 'string' && validatorJS.isHash(input, algorithm))
244
264
  return input;
245
265
  context.fail(_this, `{{label}} is not a valid ${algorithm} hash`, input);
246
266
  }, options);
@@ -252,7 +272,7 @@ exports.isHash = isHash;
252
272
  */
253
273
  function isHexColor(options) {
254
274
  return (0, index_js_1.validator)('isHexColor', function (input, context, _this) {
255
- if (typeof input === 'string' && validator_1.default.isHexColor(input))
275
+ if (typeof input === 'string' && validatorJS.isHexColor(input))
256
276
  return input;
257
277
  context.fail(_this, `{{label}} is not a valid Hex Color`, input);
258
278
  }, options);
@@ -264,7 +284,7 @@ exports.isHexColor = isHexColor;
264
284
  */
265
285
  function isJWT(options) {
266
286
  return (0, index_js_1.validator)('isJWT', function (input, context, _this) {
267
- if (typeof input === 'string' && validator_1.default.isJWT(input))
287
+ if (typeof input === 'string' && validatorJS.isJWT(input))
268
288
  return input;
269
289
  context.fail(_this, `{{label}} is not a valid JWT token`, input);
270
290
  }, options);
@@ -276,7 +296,7 @@ exports.isJWT = isJWT;
276
296
  */
277
297
  function isLowercase(options) {
278
298
  return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
279
- if (typeof input === 'string' && validator_1.default.isLowercase(input))
299
+ if (typeof input === 'string' && validatorJS.isLowercase(input))
280
300
  return input;
281
301
  context.fail(_this, `{{label}} is not a lowercase string`, input);
282
302
  }, options);
@@ -288,7 +308,7 @@ exports.isLowercase = isLowercase;
288
308
  */
289
309
  function isUppercase(options) {
290
310
  return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
291
- if (typeof input === 'string' && validator_1.default.isUppercase(input))
311
+ if (typeof input === 'string' && validatorJS.isUppercase(input))
292
312
  return input;
293
313
  context.fail(_this, `{{label}} is not an uppercase string`, input);
294
314
  }, options);
@@ -300,7 +320,7 @@ exports.isUppercase = isUppercase;
300
320
  */
301
321
  function isAlpha(options) {
302
322
  return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
303
- if (typeof input === 'string' && validator_1.default.isAlpha(input))
323
+ if (typeof input === 'string' && validatorJS.isAlpha(input))
304
324
  return input;
305
325
  context.fail(_this, `{{label}} is not an alpha string`, input);
306
326
  }, options);
@@ -312,7 +332,7 @@ exports.isAlpha = isAlpha;
312
332
  */
313
333
  function isAlphanumeric(options) {
314
334
  return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
315
- if (typeof input === 'string' && validator_1.default.isAlphanumeric(input))
335
+ if (typeof input === 'string' && validatorJS.isAlphanumeric(input))
316
336
  return input;
317
337
  context.fail(_this, `{{label}} is not an alphanumeric string`, input);
318
338
  }, options);
@@ -324,7 +344,7 @@ exports.isAlphanumeric = isAlphanumeric;
324
344
  */
325
345
  function isAscii(options) {
326
346
  return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
327
- if (typeof input === 'string' && validator_1.default.isAscii(input))
347
+ if (typeof input === 'string' && validatorJS.isAscii(input))
328
348
  return input;
329
349
  context.fail(_this, `{{label}} is not an ascii string`, input);
330
350
  }, options);
@@ -337,7 +357,7 @@ exports.isAscii = isAscii;
337
357
  */
338
358
  function isDecimal(options) {
339
359
  return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
340
- if (typeof input === 'string' && validator_1.default.isDecimal(input))
360
+ if (typeof input === 'string' && validatorJS.isDecimal(input))
341
361
  return input;
342
362
  context.fail(_this, `{{label}} is not an decimal number string`, input);
343
363
  }, options);
@@ -349,7 +369,7 @@ exports.isDecimal = isDecimal;
349
369
  */
350
370
  function isHexadecimal(options) {
351
371
  return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
352
- if (typeof input === 'string' && validator_1.default.isHexadecimal(input))
372
+ if (typeof input === 'string' && validatorJS.isHexadecimal(input))
353
373
  return input;
354
374
  context.fail(_this, `{{label}} is not an hexadecimal string`, input);
355
375
  }, options);
@@ -6,14 +6,7 @@ const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
6
6
  export class Context {
7
7
  constructor(options) {
8
8
  this.errors = [];
9
- if (options?.maxErrors != null)
10
- this.maxErrors = options.maxErrors;
11
- if (typeof options?.onFail === 'function')
12
- this.onFail = options.onFail;
13
- if (options?.coerce != null)
14
- this.coerce = options?.coerce;
15
- if (options?.label != null)
16
- this.label = options?.label;
9
+ Object.assign(this, options);
17
10
  }
18
11
  fail(rule, message, value, details) {
19
12
  const issue = omitUndefined({
@@ -1,4 +1,4 @@
1
- import validatorJS from 'validator';
1
+ import * as validatorJS from 'validator';
2
2
  import { validator } from '../core/index.js';
3
3
  /**
4
4
  * Validates if value is an "UUID".
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "valgen",
3
3
  "description": "Fast runtime type validator, converter and io (encoding/decoding) library",
4
- "version": "5.1.0",
4
+ "version": "5.2.1",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -12,9 +12,9 @@
12
12
  "url": "https://github.com/panates/valgen.git"
13
13
  },
14
14
  "dependencies": {
15
- "@types/validator": "^13.11.7",
16
- "date-fns": "^3.0.6",
17
- "ts-gems": "^3.1.0",
15
+ "@types/validator": "^13.11.9",
16
+ "date-fns": "^3.6.0",
17
+ "ts-gems": "^3.2.1",
18
18
  "validator": "^13.11.0"
19
19
  },
20
20
  "type": "module",
@@ -12,6 +12,7 @@ export declare class Context implements ExecutionOptions {
12
12
  property?: string;
13
13
  index?: number;
14
14
  label?: string;
15
+ [key: string]: any;
15
16
  constructor(options?: ExecutionOptions);
16
17
  fail(rule: Validator, message: string | Error, value: any, details?: Record<string, any>): void;
17
18
  extend(options?: ExecutionOptions): Context;
@@ -19,4 +19,5 @@ export interface ExecutionOptions extends ValidationOptions {
19
19
  coerce?: boolean;
20
20
  maxErrors?: number;
21
21
  label?: string;
22
+ [key: string]: any;
22
23
  }
@@ -15,4 +15,4 @@ export interface Validator<T = any, I = any, O extends ExecutionOptions = Execut
15
15
  }
16
16
  export declare function validator<T, I = T, O extends ExecutionOptions = ExecutionOptions>(fn: ValidateFunction<T, I>, validatorOptions?: ValidationOptions): Validator<T, I, O>;
17
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>;
18
+ export declare function isValidator(x: any): x is Validator;
@@ -23,5 +23,5 @@ export declare function isTuple<T1, I1, T2, I2, T3, I3, T4, I4, T5, I5, T6, I6>(
23
23
  Validator<T4, I4>,
24
24
  Validator<T5, I5>,
25
25
  Validator<T6, I6>,
26
- ...Validator<any, any>[]
26
+ ...Validator[]
27
27
  ], options?: ValidationOptions): Validator<[T1, T2, T3, T4, T5, T6, ...any[]], [I1, I2, I3, I4, I5, I5, ...any[]]>;
@@ -3,9 +3,9 @@ import { Validator } from '../core/index.js';
3
3
  *
4
4
  * @validator pipe
5
5
  */
6
- export declare function pipe<T>(...rules: [...Validator<any, any>[], Validator<T, any>]): Validator<T, any, import("../core/types.js").ExecutionOptions>;
6
+ export declare function pipe<T>(...rules: [...Validator[], Validator<T, any>]): Validator<T, any, import("../core/types.js").ExecutionOptions>;
7
7
  /**
8
8
  * Test given value against to all codecs and returns original input
9
9
  * @validator allOf
10
10
  */
11
- export declare function allOf(...rules: Validator<any, any>[]): Validator<any, any, import("../core/types.js").ExecutionOptions>;
11
+ export declare function allOf(...rules: Validator[]): Validator<any, any, import("../core/types.js").ExecutionOptions>;
@@ -1,4 +1,4 @@
1
- import validatorJS from 'validator';
1
+ import * as validatorJS from 'validator';
2
2
  import { ValidationOptions } from '../core/index.js';
3
3
  /**
4
4
  * Validates if value is an "UUID".