valgen 4.0.0-alpha.6 → 4.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.
Files changed (57) hide show
  1. package/CHANGELOG.md +13 -2
  2. package/cjs/core/context.js +29 -23
  3. package/cjs/core/validator.js +9 -8
  4. package/cjs/helpers/omit-undefined.js +15 -0
  5. package/cjs/rules/is-array.js +12 -10
  6. package/cjs/rules/is-bigint.js +2 -2
  7. package/cjs/rules/is-boolean.js +2 -2
  8. package/cjs/rules/is-date.js +7 -13
  9. package/cjs/rules/is-empty.js +4 -4
  10. package/cjs/rules/is-enum.js +2 -5
  11. package/cjs/rules/is-integer.js +2 -2
  12. package/cjs/rules/is-matches.js +2 -6
  13. package/cjs/rules/is-null.js +6 -6
  14. package/cjs/rules/is-number.js +2 -2
  15. package/cjs/rules/is-object-id.js +2 -2
  16. package/cjs/rules/is-object.js +16 -18
  17. package/cjs/rules/is-record.js +13 -13
  18. package/cjs/rules/is-string.js +2 -2
  19. package/cjs/rules/is-tuple.js +12 -9
  20. package/cjs/rules/length.js +2 -2
  21. package/cjs/rules/optional.js +8 -8
  22. package/cjs/rules/pipe.js +2 -4
  23. package/cjs/rules/range.js +10 -10
  24. package/cjs/rules/string-formats.js +48 -48
  25. package/cjs/rules/union.js +10 -5
  26. package/cjs/rules/utilities.js +2 -2
  27. package/esm/core/context.js +29 -23
  28. package/esm/core/validator.js +9 -8
  29. package/esm/helpers/omit-undefined.js +11 -0
  30. package/esm/rules/is-array.js +13 -11
  31. package/esm/rules/is-bigint.js +2 -2
  32. package/esm/rules/is-boolean.js +2 -2
  33. package/esm/rules/is-date.js +7 -13
  34. package/esm/rules/is-empty.js +4 -4
  35. package/esm/rules/is-enum.js +2 -5
  36. package/esm/rules/is-integer.js +2 -2
  37. package/esm/rules/is-matches.js +2 -6
  38. package/esm/rules/is-null.js +6 -6
  39. package/esm/rules/is-number.js +2 -2
  40. package/esm/rules/is-object-id.js +2 -2
  41. package/esm/rules/is-object.js +17 -19
  42. package/esm/rules/is-record.js +14 -14
  43. package/esm/rules/is-string.js +2 -2
  44. package/esm/rules/is-tuple.js +13 -10
  45. package/esm/rules/length.js +2 -2
  46. package/esm/rules/optional.js +9 -9
  47. package/esm/rules/pipe.js +3 -5
  48. package/esm/rules/range.js +10 -10
  49. package/esm/rules/string-formats.js +48 -48
  50. package/esm/rules/union.js +10 -5
  51. package/esm/rules/utilities.js +3 -3
  52. package/package.json +4 -4
  53. package/typings/core/context.d.ts +11 -19
  54. package/typings/core/types.d.ts +4 -1
  55. package/typings/core/validator.d.ts +1 -1
  56. package/typings/helpers/omit-undefined.d.ts +1 -0
  57. package/typings/rules/is-object.d.ts +1 -1
@@ -5,10 +5,10 @@ import { validator } from '../core/index.js';
5
5
  * @validator isUUID
6
6
  */
7
7
  export function isUUID(version, options) {
8
- return validator('isUUID', function (input, context) {
8
+ return validator('isUUID', function (input, context, _this) {
9
9
  if (input != null && typeof input === 'string' && validatorJS.isUUID(input, version))
10
10
  return input;
11
- context.failure(`{{label}} is not a valid UUID${version ? ' v' + version : ''}`);
11
+ context.fail(_this, `{{label}} is not a valid UUID${version ? ' v' + version : ''}`, input);
12
12
  }, options);
13
13
  }
14
14
  /**
@@ -16,10 +16,10 @@ export function isUUID(version, options) {
16
16
  * @validator isEmail
17
17
  */
18
18
  export function isEmail(options) {
19
- return validator('isEmail', function (input, context) {
19
+ return validator('isEmail', function (input, context, _this) {
20
20
  if (typeof input === 'string' && validatorJS.isEmail(input, options))
21
21
  return input;
22
- context.failure(`{{label}} is not a valid a EMail`);
22
+ context.fail(_this, `{{label}} is not a valid a EMail`, input);
23
23
  }, options);
24
24
  }
25
25
  /**
@@ -27,10 +27,10 @@ export function isEmail(options) {
27
27
  * @validator isMobilePhone
28
28
  */
29
29
  export function isMobilePhone(options) {
30
- return validator('isMobilePhone', function (input, context) {
30
+ return validator('isMobilePhone', function (input, context, _this) {
31
31
  if (typeof input === 'string' && validatorJS.isMobilePhone(input, options?.locale, options))
32
32
  return input;
33
- context.failure(`{{label}} is not a valid a Mobile Phone Number`);
33
+ context.fail(_this, `{{label}} is not a valid a Mobile Phone Number`, input);
34
34
  }, options);
35
35
  }
36
36
  /**
@@ -38,10 +38,10 @@ export function isMobilePhone(options) {
38
38
  * @validator isIP
39
39
  */
40
40
  export function isIP(version, options) {
41
- return validator('isIP', function (input, context) {
41
+ return validator('isIP', function (input, context, _this) {
42
42
  if (input != null && typeof input === 'string' && validatorJS.isIP(input, version))
43
43
  return input;
44
- context.failure(`{{label}} is not a valid IP${version ? ' v' + version : ''}`);
44
+ context.fail(_this, `{{label}} is not a valid IP${version ? ' v' + version : ''}`, input);
45
45
  }, options);
46
46
  }
47
47
  /**
@@ -49,10 +49,10 @@ export function isIP(version, options) {
49
49
  * @validator isUUID
50
50
  */
51
51
  export function isIPRange(version, options) {
52
- return validator('isIPRange', function (input, context) {
52
+ return validator('isIPRange', function (input, context, _this) {
53
53
  if (input != null && typeof input === 'string' && validatorJS.isIPRange(input, version))
54
54
  return input;
55
- context.failure(`{{label}} is not a valid IP${version ? ' v' + version : ''} range`);
55
+ context.fail(_this, `{{label}} is not a valid IP${version ? ' v' + version : ''} range`, input);
56
56
  }, options);
57
57
  }
58
58
  /**
@@ -60,10 +60,10 @@ export function isIPRange(version, options) {
60
60
  * @validator isMACAddress
61
61
  */
62
62
  export function isMACAddress(options) {
63
- return validator('isMACAddress', function (input, context) {
63
+ return validator('isMACAddress', function (input, context, _this) {
64
64
  if (input != null && typeof input === 'string' && validatorJS.isMACAddress(input, options))
65
65
  return input;
66
- context.failure(`{{label}} is not a valid MAC address`);
66
+ context.fail(_this, `{{label}} is not a valid MAC address`, input);
67
67
  }, options);
68
68
  }
69
69
  /**
@@ -71,10 +71,10 @@ export function isMACAddress(options) {
71
71
  * @validator isPort
72
72
  */
73
73
  export function isPort(options) {
74
- return validator('isPort', function (input, context) {
74
+ return validator('isPort', function (input, context, _this) {
75
75
  if (input != null && typeof input === 'string' && validatorJS.isPort(input))
76
76
  return input;
77
- context.failure(`{{label}} is not a valid port number`);
77
+ context.fail(_this, `{{label}} is not a valid port number`, input);
78
78
  }, options);
79
79
  }
80
80
  /**
@@ -82,10 +82,10 @@ export function isPort(options) {
82
82
  * @validator isURL
83
83
  */
84
84
  export function isURL(options) {
85
- return validator('isURL', function (input, context) {
85
+ return validator('isURL', function (input, context, _this) {
86
86
  if (input != null && typeof input === 'string' && validatorJS.isURL(input, options))
87
87
  return input;
88
- context.failure(`{{label}} is not a valid URL`);
88
+ context.fail(_this, `{{label}} is not a valid URL`, input);
89
89
  }, options);
90
90
  }
91
91
  /**
@@ -93,10 +93,10 @@ export function isURL(options) {
93
93
  * @validator isBase64
94
94
  */
95
95
  export function isBase64(options) {
96
- return validator('isBase64', function (input, context) {
96
+ return validator('isBase64', function (input, context, _this) {
97
97
  if (typeof input === 'string' && validatorJS.isBase64(input, options))
98
98
  return input;
99
- context.failure(`{{label}} is not a valid a Base64 string`);
99
+ context.fail(_this, `{{label}} is not a valid a Base64 string`, input);
100
100
  }, options);
101
101
  }
102
102
  /**
@@ -104,10 +104,10 @@ export function isBase64(options) {
104
104
  * @validator isBIC
105
105
  */
106
106
  export function isBIC(options) {
107
- return validator('isBIC', function (input, context) {
107
+ return validator('isBIC', function (input, context, _this) {
108
108
  if (typeof input === 'string' && validatorJS.isBIC(input))
109
109
  return input;
110
- context.failure(`{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`);
110
+ context.fail(_this, `{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`, input);
111
111
  }, options);
112
112
  }
113
113
  /**
@@ -115,10 +115,10 @@ export function isBIC(options) {
115
115
  * @validator isCreditCard
116
116
  */
117
117
  export function isCreditCard(options) {
118
- return validator('isCreditCard', function (input, context) {
118
+ return validator('isCreditCard', function (input, context, _this) {
119
119
  if (typeof input === 'string' && validatorJS.isCreditCard(input, options))
120
120
  return input;
121
- context.failure(`{{label}} is not a valid Credit Card number`);
121
+ context.fail(_this, `{{label}} is not a valid Credit Card number`, input);
122
122
  }, options);
123
123
  }
124
124
  /**
@@ -126,10 +126,10 @@ export function isCreditCard(options) {
126
126
  * @validator isEAN
127
127
  */
128
128
  export function isIBAN(options) {
129
- return validator('isIBAN', function (input, context) {
129
+ return validator('isIBAN', function (input, context, _this) {
130
130
  if (typeof input === 'string' && validatorJS.isIBAN(input))
131
131
  return input;
132
- context.failure(`{{label}} is not a valid IBAN (International Bank Account Number)`);
132
+ context.fail(_this, `{{label}} is not a valid IBAN (International Bank Account Number)`, input);
133
133
  }, options);
134
134
  }
135
135
  /**
@@ -137,10 +137,10 @@ export function isIBAN(options) {
137
137
  * @validator isPassportNumber
138
138
  */
139
139
  export function isPassportNumber(countryCode, options) {
140
- return validator('isPassportNumber', function (input, context) {
140
+ return validator('isPassportNumber', function (input, context, _this) {
141
141
  if (typeof input === 'string' && validatorJS.isPassportNumber(input, countryCode))
142
142
  return input;
143
- context.failure(`{{label}} is not a valid ${countryCode} PassportNumber)`);
143
+ context.fail(_this, `{{label}} is not a valid ${countryCode} PassportNumber)`, input);
144
144
  }, options);
145
145
  }
146
146
  /**
@@ -148,10 +148,10 @@ export function isPassportNumber(countryCode, options) {
148
148
  * @validator isEAN
149
149
  */
150
150
  export function isEAN(options) {
151
- return validator('isEAN', function (input, context) {
151
+ return validator('isEAN', function (input, context, _this) {
152
152
  if (typeof input === 'string' && validatorJS.isEAN(input))
153
153
  return input;
154
- context.failure(`{{label}} is not a valid EAN (European Article Number)`);
154
+ context.fail(_this, `{{label}} is not a valid EAN (European Article Number)`, input);
155
155
  }, options);
156
156
  }
157
157
  /**
@@ -159,10 +159,10 @@ export function isEAN(options) {
159
159
  * @validator isFQDN
160
160
  */
161
161
  export function isFQDN(options) {
162
- return validator('isFQDN', function (input, context) {
162
+ return validator('isFQDN', function (input, context, _this) {
163
163
  if (typeof input === 'string' && validatorJS.isFQDN(input, options))
164
164
  return input;
165
- context.failure(`{{label}} is not valid FQDN`);
165
+ context.fail(_this, `{{label}} is not valid FQDN`, input);
166
166
  }, options);
167
167
  }
168
168
  /**
@@ -170,10 +170,10 @@ export function isFQDN(options) {
170
170
  * @validator isISSN
171
171
  */
172
172
  export function isISSN(options) {
173
- return validator('isISSN', function (input, context) {
173
+ return validator('isISSN', function (input, context, _this) {
174
174
  if (typeof input === 'string' && validatorJS.isISSN(input, options))
175
175
  return input;
176
- context.failure(`{{label}} is not a valid ISSN`);
176
+ context.fail(_this, `{{label}} is not a valid ISSN`, input);
177
177
  }, options);
178
178
  }
179
179
  /**
@@ -181,10 +181,10 @@ export function isISSN(options) {
181
181
  * @validator isVAT
182
182
  */
183
183
  export function isVAT(countryCode, options) {
184
- return validator('isVAT', function (input, context) {
184
+ return validator('isVAT', function (input, context, _this) {
185
185
  if (typeof input === 'string' && validatorJS.isVAT(input, countryCode))
186
186
  return input;
187
- context.failure(`{{label}} is not a valid VAT number`);
187
+ context.fail(_this, `{{label}} is not a valid VAT number`, input);
188
188
  }, options);
189
189
  }
190
190
  /**
@@ -192,10 +192,10 @@ export function isVAT(countryCode, options) {
192
192
  * @validator isBtcAddress
193
193
  */
194
194
  export function isBtcAddress(options) {
195
- return validator('isBtcAddress', function (input, context) {
195
+ return validator('isBtcAddress', function (input, context, _this) {
196
196
  if (typeof input === 'string' && validatorJS.isBtcAddress(input))
197
197
  return input;
198
- context.failure(`{{label}} is not a valid a BTC address`);
198
+ context.fail(_this, `{{label}} is not a valid a BTC address`, input);
199
199
  }, options);
200
200
  }
201
201
  /**
@@ -203,10 +203,10 @@ export function isBtcAddress(options) {
203
203
  * @validator isBtcAddress
204
204
  */
205
205
  export function isETHAddress(options) {
206
- return validator('isETHAddress', function (input, context) {
206
+ return validator('isETHAddress', function (input, context, _this) {
207
207
  if (typeof input === 'string' && validatorJS.isEthereumAddress(input))
208
208
  return input;
209
- context.failure(`{{label}} is not a valid a ETH (Ethereum) address`);
209
+ context.fail(_this, `{{label}} is not a valid a ETH (Ethereum) address`, input);
210
210
  }, options);
211
211
  }
212
212
  /**
@@ -214,10 +214,10 @@ export function isETHAddress(options) {
214
214
  * @validator isHash
215
215
  */
216
216
  export function isHash(algorithm, options) {
217
- return validator('isHash', function (input, context) {
217
+ return validator('isHash', function (input, context, _this) {
218
218
  if (typeof input === 'string' && validatorJS.isHash(input, algorithm))
219
219
  return input;
220
- context.failure(`{{label}} is not a valid ${algorithm} hash`);
220
+ context.fail(_this, `{{label}} is not a valid ${algorithm} hash`, input);
221
221
  }, options);
222
222
  }
223
223
  /**
@@ -225,10 +225,10 @@ export function isHash(algorithm, options) {
225
225
  * @validator isHexColor
226
226
  */
227
227
  export function isHexColor(options) {
228
- return validator('isHexColor', function (input, context) {
228
+ return validator('isHexColor', function (input, context, _this) {
229
229
  if (typeof input === 'string' && validatorJS.isHexColor(input))
230
230
  return input;
231
- context.failure(`{{label}} is not a valid Hex Color`);
231
+ context.fail(_this, `{{label}} is not a valid Hex Color`, input);
232
232
  }, options);
233
233
  }
234
234
  /**
@@ -236,10 +236,10 @@ export function isHexColor(options) {
236
236
  * @validator isHash
237
237
  */
238
238
  export function isJWT(options) {
239
- return validator('isJWT', function (input, context) {
239
+ return validator('isJWT', function (input, context, _this) {
240
240
  if (typeof input === 'string' && validatorJS.isJWT(input))
241
241
  return input;
242
- context.failure(`{{label}} is not a valid JWT token`);
242
+ context.fail(_this, `{{label}} is not a valid JWT token`, input);
243
243
  }, options);
244
244
  }
245
245
  /**
@@ -247,10 +247,10 @@ export function isJWT(options) {
247
247
  * @validator isLowercase
248
248
  */
249
249
  export function isLowercase(options) {
250
- return validator('isLowercase', function (input, context) {
250
+ return validator('isLowercase', function (input, context, _this) {
251
251
  if (typeof input === 'string' && validatorJS.isLowercase(input))
252
252
  return input;
253
- context.failure(`{{label}} is not a lowercase string`);
253
+ context.fail(_this, `{{label}} is not a lowercase string`, input);
254
254
  }, options);
255
255
  }
256
256
  /**
@@ -258,9 +258,9 @@ export function isLowercase(options) {
258
258
  * @validator isUppercase
259
259
  */
260
260
  export function isUppercase(options) {
261
- return validator('isLowercase', function (input, context) {
261
+ return validator('isLowercase', function (input, context, _this) {
262
262
  if (typeof input === 'string' && validatorJS.isUppercase(input))
263
263
  return input;
264
- context.failure(`{{label}} is not an uppercase string`);
264
+ context.fail(_this, `{{label}} is not an uppercase string`, input);
265
265
  }, options);
266
266
  }
@@ -4,24 +4,29 @@ import { validator } from '../core/index.js';
4
4
  */
5
5
  export function union(codecs) {
6
6
  const l = codecs.length;
7
- return validator('union', function (input, context) {
7
+ return validator('union', function (input, context, _this) {
8
8
  let i;
9
9
  let c;
10
10
  let v;
11
11
  let passed = false;
12
+ // Mock fail method to prevent errors
13
+ context.fail = () => passed = false;
12
14
  for (i = 0; i < l; i++) {
13
15
  c = codecs[i];
14
16
  try {
15
- v = c(input, context);
16
17
  passed = true;
17
- break;
18
+ v = c(input, context);
19
+ if (passed)
20
+ break;
18
21
  }
19
- catch (e) {
22
+ catch {
20
23
  //
21
24
  }
22
25
  }
26
+ // Restore fail method
27
+ delete context.fail;
23
28
  if (passed)
24
29
  return v;
25
- context.failure(`{{location}} didn't match any of required format`);
30
+ context.fail(_this, `{{location}} didn't match any of required format`, input);
26
31
  });
27
32
  }
@@ -1,4 +1,4 @@
1
- import { isValidator, kValidatorFn, validator } from '../core/index.js';
1
+ import { isValidator, validator } from '../core/index.js';
2
2
  /**
3
3
  * Forwards codec process to a sub codec. Useful for circular checks
4
4
  * @validator forwardRef
@@ -6,7 +6,7 @@ import { isValidator, kValidatorFn, validator } from '../core/index.js';
6
6
  export function forwardRef(fn) {
7
7
  return validator('forwardRef', function (input, context) {
8
8
  const nested = fn(context);
9
- return nested[kValidatorFn](input, context, nested);
9
+ return nested(input, context);
10
10
  });
11
11
  }
12
12
  export function iif(check, _then, _else) {
@@ -20,7 +20,7 @@ export function iif(check, _then, _else) {
20
20
  // ignored
21
21
  }
22
22
  if (isValidator(c))
23
- return c[kValidatorFn](input, context, c);
23
+ return c(input, context);
24
24
  return c;
25
25
  });
26
26
  }
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": "4.0.0-alpha.6",
4
+ "version": "4.0.0",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -12,10 +12,10 @@
12
12
  "url": "https://github.com/panates/valgen.git"
13
13
  },
14
14
  "dependencies": {
15
- "@types/validator": "^13.7.17",
15
+ "@types/validator": "^13.11.1",
16
16
  "dayjs": "^2.0.0-alpha.4",
17
- "ts-gems": "^2.4.0",
18
- "validator": "^13.9.0"
17
+ "ts-gems": "^2.4.1",
18
+ "validator": "^13.11.0"
19
19
  },
20
20
  "type": "module",
21
21
  "types": "typings/index.d.ts",
@@ -1,26 +1,18 @@
1
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
- }
2
+ import type { Validator } from './validator.js';
14
3
  export declare class Context implements ExecutionOptions {
15
- rule: Validator<any>;
16
4
  errors: ErrorIssue[];
17
5
  maxErrors?: number;
18
6
  onFail?: OnFailFunction;
19
7
  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;
8
+ root?: string;
9
+ location?: string;
10
+ scope?: object;
11
+ context?: string;
12
+ property?: string;
13
+ index?: number;
14
+ label?: string;
15
+ constructor(options?: ExecutionOptions);
16
+ fail(rule: Validator, message: string | Error, value: any, details?: Record<string, any>): void;
17
+ extend(options?: ExecutionOptions): Context;
26
18
  }
@@ -1,11 +1,14 @@
1
1
  import type { Context } from './context';
2
2
  export interface ErrorIssue {
3
- rule: string;
4
3
  message: string;
4
+ rule: string;
5
5
  value: any;
6
6
  root?: string;
7
+ location?: string;
7
8
  context?: string;
8
9
  property?: string;
10
+ index?: number;
11
+ label?: string;
9
12
  [key: string]: any;
10
13
  }
11
14
  export type OnFailFunction = (issue: ErrorIssue, context: Context, _super?: OnFailFunction) => string | Omit<ErrorIssue, 'id' | 'input'>;
@@ -3,7 +3,7 @@ import { kValidatorFn } from './constants.js';
3
3
  import { Context } from './context.js';
4
4
  import { ErrorIssue, ExecutionOptions, ValidationOptions } from './types.js';
5
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> {
6
+ export interface Validator<T = any, I = any, O extends ExecutionOptions = ExecutionOptions> {
7
7
  (input: I, options?: O, context?: Context): T;
8
8
  silent(input: I, options?: O, context?: Context): {
9
9
  value?: T;
@@ -0,0 +1 @@
1
+ export declare function omitUndefined<T>(obj: T, recursive?: boolean): T;
@@ -7,7 +7,7 @@ export type ObjectSchema = Record<string | number, Validator<any, any> | [Valida
7
7
  export interface IsObjectOptions<T> extends ValidationOptions {
8
8
  name?: string;
9
9
  ctor?: Type<T>;
10
- additionalFields?: boolean | Validator<any, any> | 'ignore';
10
+ additionalFields?: boolean | Validator<any, any> | 'error';
11
11
  caseInSensitive?: boolean;
12
12
  detectCircular?: boolean;
13
13
  }