valgen 5.3.0 → 5.3.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.
Files changed (172) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/cjs/core/context.js +2 -3
  3. package/cjs/core/validator.js +4 -4
  4. package/cjs/index.js +5 -5
  5. package/cjs/rules/format-rules/is-alpha.js +52 -0
  6. package/cjs/rules/format-rules/is-ascii.js +40 -0
  7. package/cjs/rules/format-rules/is-base64.js +40 -0
  8. package/cjs/rules/format-rules/is-btc-address.js +40 -0
  9. package/cjs/rules/format-rules/is-credit-card.js +40 -0
  10. package/cjs/rules/format-rules/is-decimal.js +41 -0
  11. package/cjs/rules/format-rules/is-ean.js +40 -0
  12. package/cjs/rules/format-rules/is-email.js +97 -0
  13. package/cjs/rules/format-rules/is-eth-address.js +40 -0
  14. package/cjs/rules/format-rules/is-fqdn.js +44 -0
  15. package/cjs/rules/format-rules/is-hash.js +40 -0
  16. package/cjs/rules/format-rules/is-hex-color.js +40 -0
  17. package/cjs/rules/format-rules/is-hex.js +40 -0
  18. package/cjs/rules/format-rules/is-iban.js +52 -0
  19. package/cjs/rules/format-rules/is-ip.js +52 -0
  20. package/cjs/rules/format-rules/is-issn.js +44 -0
  21. package/cjs/rules/format-rules/is-jwt.js +40 -0
  22. package/cjs/rules/format-rules/is-lowercase.js +52 -0
  23. package/cjs/rules/format-rules/is-mac-address.js +45 -0
  24. package/cjs/rules/format-rules/is-mobile-phone.js +43 -0
  25. package/cjs/rules/{is-object-id.js → format-rules/is-object-id.js} +3 -3
  26. package/cjs/rules/format-rules/is-passport-number.js +40 -0
  27. package/cjs/rules/format-rules/is-port.js +40 -0
  28. package/cjs/rules/format-rules/is-url.js +40 -0
  29. package/cjs/rules/format-rules/is-uuid.js +40 -0
  30. package/cjs/rules/format-rules/is-vat-number.js +40 -0
  31. package/cjs/rules/{is-regexp.js → format-rules/matches.js} +8 -5
  32. package/cjs/rules/index.js +49 -22
  33. package/cjs/rules/logical-rules/is-defined.js +30 -0
  34. package/cjs/rules/{is-empty.js → logical-rules/is-empty.js} +1 -1
  35. package/cjs/rules/{range.js → logical-rules/range.js} +63 -38
  36. package/cjs/rules/{is-any.js → type-rules/is-any.js} +1 -1
  37. package/cjs/rules/{is-array.js → type-rules/is-array.js} +2 -4
  38. package/cjs/rules/{is-bigint.js → type-rules/is-bigint.js} +2 -3
  39. package/cjs/rules/{is-boolean.js → type-rules/is-boolean.js} +1 -1
  40. package/cjs/rules/{is-date.js → type-rules/is-date.js} +5 -5
  41. package/cjs/rules/{is-enum.js → type-rules/is-enum.js} +6 -5
  42. package/cjs/rules/{is-integer.js → type-rules/is-integer.js} +2 -3
  43. package/cjs/rules/{is-null.js → type-rules/is-null.js} +2 -28
  44. package/cjs/rules/{is-number.js → type-rules/is-number.js} +2 -3
  45. package/cjs/rules/{is-object.js → type-rules/is-object.js} +3 -4
  46. package/cjs/rules/{is-record.js → type-rules/is-record.js} +1 -1
  47. package/cjs/rules/{is-string.js → type-rules/is-string.js} +1 -1
  48. package/cjs/rules/{is-tuple.js → type-rules/is-tuple.js} +2 -3
  49. package/cjs/rules/utility-rules/exists.js +16 -0
  50. package/cjs/rules/utility-rules/get-length.js +24 -0
  51. package/cjs/rules/utility-rules/optional.js +16 -0
  52. package/cjs/rules/{pipe.js → utility-rules/pipe.js} +1 -1
  53. package/cjs/rules/utility-rules/required.js +18 -0
  54. package/cjs/rules/{union.js → utility-rules/union.js} +2 -2
  55. package/esm/core/context.js +2 -3
  56. package/esm/core/validator.js +4 -4
  57. package/esm/index.js +3 -3
  58. package/esm/rules/format-rules/is-alpha.js +24 -0
  59. package/esm/rules/format-rules/is-ascii.js +13 -0
  60. package/esm/rules/format-rules/is-base64.js +13 -0
  61. package/esm/rules/format-rules/is-btc-address.js +13 -0
  62. package/esm/rules/format-rules/is-credit-card.js +13 -0
  63. package/esm/rules/format-rules/is-decimal.js +14 -0
  64. package/esm/rules/format-rules/is-ean.js +13 -0
  65. package/esm/rules/format-rules/is-email.js +70 -0
  66. package/esm/rules/format-rules/is-eth-address.js +13 -0
  67. package/esm/rules/format-rules/is-fqdn.js +17 -0
  68. package/esm/rules/format-rules/is-hash.js +13 -0
  69. package/esm/rules/format-rules/is-hex-color.js +13 -0
  70. package/esm/rules/format-rules/is-hex.js +13 -0
  71. package/esm/rules/format-rules/is-iban.js +24 -0
  72. package/esm/rules/format-rules/is-ip.js +24 -0
  73. package/esm/rules/format-rules/is-issn.js +17 -0
  74. package/esm/rules/format-rules/is-jwt.js +13 -0
  75. package/esm/rules/format-rules/is-lowercase.js +24 -0
  76. package/esm/rules/format-rules/is-mac-address.js +18 -0
  77. package/esm/rules/format-rules/is-mobile-phone.js +16 -0
  78. package/esm/rules/{is-object-id.js → format-rules/is-object-id.js} +3 -3
  79. package/esm/rules/format-rules/is-passport-number.js +13 -0
  80. package/esm/rules/format-rules/is-port.js +13 -0
  81. package/esm/rules/format-rules/is-url.js +13 -0
  82. package/esm/rules/format-rules/is-uuid.js +13 -0
  83. package/esm/rules/format-rules/is-vat-number.js +13 -0
  84. package/esm/rules/{is-regexp.js → format-rules/matches.js} +6 -3
  85. package/esm/rules/index.js +49 -22
  86. package/esm/rules/logical-rules/is-defined.js +25 -0
  87. package/esm/rules/{is-empty.js → logical-rules/is-empty.js} +1 -1
  88. package/esm/rules/{range.js → logical-rules/range.js} +59 -36
  89. package/esm/rules/{is-any.js → type-rules/is-any.js} +1 -1
  90. package/esm/rules/{is-array.js → type-rules/is-array.js} +2 -4
  91. package/esm/rules/{is-bigint.js → type-rules/is-bigint.js} +2 -3
  92. package/esm/rules/{is-boolean.js → type-rules/is-boolean.js} +1 -1
  93. package/esm/rules/{is-date.js → type-rules/is-date.js} +5 -5
  94. package/esm/rules/{is-enum.js → type-rules/is-enum.js} +6 -5
  95. package/esm/rules/{is-integer.js → type-rules/is-integer.js} +2 -3
  96. package/esm/rules/{is-null.js → type-rules/is-null.js} +1 -25
  97. package/esm/rules/{is-number.js → type-rules/is-number.js} +2 -3
  98. package/esm/rules/{is-object.js → type-rules/is-object.js} +3 -4
  99. package/esm/rules/{is-record.js → type-rules/is-record.js} +1 -1
  100. package/esm/rules/{is-string.js → type-rules/is-string.js} +1 -1
  101. package/esm/rules/{is-tuple.js → type-rules/is-tuple.js} +2 -3
  102. package/esm/rules/utility-rules/exists.js +12 -0
  103. package/esm/rules/utility-rules/get-length.js +20 -0
  104. package/esm/rules/utility-rules/optional.js +12 -0
  105. package/esm/rules/{pipe.js → utility-rules/pipe.js} +1 -1
  106. package/esm/rules/utility-rules/required.js +14 -0
  107. package/esm/rules/{union.js → utility-rules/union.js} +2 -2
  108. package/package.json +3 -3
  109. package/typings/index.d.ts +4 -4
  110. package/typings/rules/format-rules/is-alpha.d.ts +11 -0
  111. package/typings/rules/format-rules/is-ascii.d.ts +6 -0
  112. package/typings/rules/format-rules/is-base64.d.ts +9 -0
  113. package/typings/rules/format-rules/is-btc-address.d.ts +6 -0
  114. package/typings/rules/format-rules/is-credit-card.d.ts +9 -0
  115. package/typings/rules/format-rules/is-decimal.d.ts +7 -0
  116. package/typings/rules/format-rules/is-ean.d.ts +6 -0
  117. package/typings/rules/format-rules/is-email.d.ts +62 -0
  118. package/typings/rules/format-rules/is-eth-address.d.ts +6 -0
  119. package/typings/rules/format-rules/is-fqdn.d.ts +13 -0
  120. package/typings/rules/format-rules/is-hash.d.ts +8 -0
  121. package/typings/rules/format-rules/is-hex-color.d.ts +6 -0
  122. package/typings/rules/format-rules/is-hex.d.ts +6 -0
  123. package/typings/rules/format-rules/is-iban.d.ts +11 -0
  124. package/typings/rules/format-rules/is-ip.d.ts +11 -0
  125. package/typings/rules/format-rules/is-issn.d.ts +14 -0
  126. package/typings/rules/format-rules/is-jwt.d.ts +6 -0
  127. package/typings/rules/format-rules/is-lowercase.d.ts +11 -0
  128. package/typings/rules/format-rules/is-mac-address.d.ts +21 -0
  129. package/typings/rules/format-rules/is-mobile-phone.d.ts +20 -0
  130. package/typings/rules/{is-object-id.d.ts → format-rules/is-object-id.d.ts} +2 -2
  131. package/typings/rules/format-rules/is-passport-number.d.ts +6 -0
  132. package/typings/rules/format-rules/is-port.d.ts +6 -0
  133. package/typings/rules/format-rules/is-url.d.ts +9 -0
  134. package/typings/rules/format-rules/is-uuid.d.ts +6 -0
  135. package/typings/rules/format-rules/is-vat-number.d.ts +6 -0
  136. package/typings/rules/format-rules/matches.d.ts +9 -0
  137. package/typings/rules/index.d.ts +49 -22
  138. package/typings/rules/logical-rules/is-defined.d.ts +11 -0
  139. package/typings/rules/{is-empty.d.ts → logical-rules/is-empty.d.ts} +3 -3
  140. package/typings/rules/{range.d.ts → logical-rules/range.d.ts} +14 -4
  141. package/typings/rules/type-rules/is-any.d.ts +5 -0
  142. package/typings/rules/{is-array.d.ts → type-rules/is-array.d.ts} +2 -2
  143. package/typings/rules/{is-bigint.d.ts → type-rules/is-bigint.d.ts} +2 -2
  144. package/typings/rules/{is-boolean.d.ts → type-rules/is-boolean.d.ts} +2 -2
  145. package/typings/rules/{is-date.d.ts → type-rules/is-date.d.ts} +3 -3
  146. package/typings/rules/{is-enum.d.ts → type-rules/is-enum.d.ts} +1 -1
  147. package/typings/rules/{is-integer.d.ts → type-rules/is-integer.d.ts} +2 -2
  148. package/typings/rules/type-rules/is-null.d.ts +11 -0
  149. package/typings/rules/{is-number.d.ts → type-rules/is-number.d.ts} +2 -2
  150. package/typings/rules/{is-object.d.ts → type-rules/is-object.d.ts} +1 -1
  151. package/typings/rules/{is-record.d.ts → type-rules/is-record.d.ts} +2 -2
  152. package/typings/rules/{is-string.d.ts → type-rules/is-string.d.ts} +5 -5
  153. package/typings/rules/{is-tuple.d.ts → type-rules/is-tuple.d.ts} +2 -8
  154. package/typings/rules/utility-rules/exists.d.ts +6 -0
  155. package/typings/rules/utility-rules/get-length.d.ts +11 -0
  156. package/typings/rules/utility-rules/optional.d.ts +7 -0
  157. package/typings/rules/{pipe.d.ts → utility-rules/pipe.d.ts} +1 -1
  158. package/typings/rules/utility-rules/required.d.ts +7 -0
  159. package/typings/rules/utility-rules/union.d.ts +5 -0
  160. package/cjs/rules/length.js +0 -41
  161. package/cjs/rules/optional.js +0 -44
  162. package/cjs/rules/string-formats.js +0 -377
  163. package/esm/rules/length.js +0 -35
  164. package/esm/rules/optional.js +0 -38
  165. package/esm/rules/string-formats.js +0 -322
  166. package/typings/rules/is-any.d.ts +0 -5
  167. package/typings/rules/is-null.d.ts +0 -21
  168. package/typings/rules/is-regexp.d.ts +0 -9
  169. package/typings/rules/length.d.ts +0 -21
  170. package/typings/rules/optional.d.ts +0 -17
  171. package/typings/rules/string-formats.d.ts +0 -166
  172. package/typings/rules/union.d.ts +0 -5
@@ -1,22 +1,49 @@
1
- export * from './is-any.js';
2
- export * from './is-array.js';
3
- export * from './is-bigint.js';
4
- export * from './is-boolean.js';
5
- export * from './is-date.js';
6
- export * from './is-empty.js';
7
- export * from './is-enum.js';
8
- export * from './is-integer.js';
9
- export * from './is-null.js';
10
- export * from './is-regexp.js';
11
- export * from './is-number.js';
12
- export * from './is-object.js';
13
- export * from './is-object-id.js';
14
- export * from './is-record.js';
15
- export * from './is-string.js';
16
- export * from './is-tuple.js';
17
- export * from './length.js';
18
- export * from './optional.js';
19
- export * from './pipe.js';
20
- export * from './range.js';
21
- export * from './string-formats.js';
22
- export * from './union.js';
1
+ export * from './type-rules/is-any.js';
2
+ export * from './type-rules/is-array.js';
3
+ export * from './type-rules/is-bigint.js';
4
+ export * from './type-rules/is-boolean.js';
5
+ export * from './type-rules/is-date.js';
6
+ export * from './type-rules/is-enum.js';
7
+ export * from './type-rules/is-integer.js';
8
+ export * from './type-rules/is-null.js';
9
+ export * from './type-rules/is-number.js';
10
+ export * from './type-rules/is-object.js';
11
+ export * from './type-rules/is-record.js';
12
+ export * from './type-rules/is-string.js';
13
+ export * from './type-rules/is-tuple.js';
14
+ export * from './format-rules/is-alpha.js';
15
+ export * from './format-rules/is-ascii.js';
16
+ export * from './format-rules/is-base64.js';
17
+ export * from './format-rules/is-btc-address.js';
18
+ export * from './format-rules/is-credit-card.js';
19
+ export * from './format-rules/is-decimal.js';
20
+ export * from './format-rules/is-ean.js';
21
+ export * from './format-rules/is-email.js';
22
+ export * from './format-rules/is-eth-address.js';
23
+ export * from './format-rules/is-fqdn.js';
24
+ export * from './format-rules/is-hash.js';
25
+ export * from './format-rules/is-hex.js';
26
+ export * from './format-rules/is-hex-color.js';
27
+ export * from './format-rules/is-iban.js';
28
+ export * from './format-rules/is-ip.js';
29
+ export * from './format-rules/is-issn.js';
30
+ export * from './format-rules/is-jwt.js';
31
+ export * from './format-rules/is-lowercase.js';
32
+ export * from './format-rules/is-mac-address.js';
33
+ export * from './format-rules/is-mobile-phone.js';
34
+ export * from './format-rules/is-object-id.js';
35
+ export * from './format-rules/is-passport-number.js';
36
+ export * from './format-rules/is-port.js';
37
+ export * from './format-rules/is-url.js';
38
+ export * from './format-rules/is-uuid.js';
39
+ export * from './format-rules/is-vat-number.js';
40
+ export * from './format-rules/matches.js';
41
+ export * from './logical-rules/is-defined.js';
42
+ export * from './logical-rules/is-empty.js';
43
+ export * from './logical-rules/range.js';
44
+ export * from './utility-rules/exists.js';
45
+ export * from './utility-rules/get-length.js';
46
+ export * from './utility-rules/optional.js';
47
+ export * from './utility-rules/pipe.js';
48
+ export * from './utility-rules/required.js';
49
+ export * from './utility-rules/union.js';
@@ -0,0 +1,25 @@
1
+ import { validator } from '../../core/index.js';
2
+ /**
3
+ * Validates if value is not "undefined" nor "null"
4
+ * @validator isDefined
5
+ */
6
+ export function isDefined(options) {
7
+ return validator('is-defined', function (input, context, _this) {
8
+ if (input !== undefined)
9
+ return input;
10
+ context.fail(_this, `Is not defined`, input);
11
+ }, options);
12
+ }
13
+ /**
14
+ * Validates if value is "undefined"
15
+ * @validator isUndefined
16
+ */
17
+ export function isUndefined(options) {
18
+ return validator('isUndefined', function (input, context, _this) {
19
+ if (options?.coerce || context.coerce)
20
+ return undefined;
21
+ if (input === undefined)
22
+ return;
23
+ context.fail(_this, `{{label}} mustn\'t be defined`, input);
24
+ }, options);
25
+ }
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Checks if value is an empty. Value should be string, array, set, map or object
4
4
  * @validator isEmpty
@@ -1,4 +1,33 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
+ import { getLength } from '../utility-rules/get-length.js';
3
+ import { allOf, pipe } from '../utility-rules/pipe.js';
4
+ /**
5
+ * Checks if value is between minValue and maxValue
6
+ * @validator range
7
+ */
8
+ export function range(minValue, maxValue, options) {
9
+ return validator('range', function (input, context, _this) {
10
+ if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
11
+ (typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
12
+ (typeof input === 'number' || typeof input === 'bigint') &&
13
+ input >= minValue &&
14
+ input <= maxValue)
15
+ return input;
16
+ if (minValue instanceof Date &&
17
+ maxValue instanceof Date &&
18
+ input instanceof Date &&
19
+ input >= minValue &&
20
+ input <= maxValue)
21
+ return input;
22
+ if (typeof minValue === 'string' &&
23
+ typeof maxValue === 'string' &&
24
+ typeof input === 'string' &&
25
+ input >= minValue &&
26
+ input <= maxValue)
27
+ return input;
28
+ context.fail(_this, `Value must be between ${minValue} and ${maxValue}`, input);
29
+ }, options);
30
+ }
2
31
  export function isGt(minValue, options) {
3
32
  return validator('isGt', function (input, context, _this) {
4
33
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
@@ -7,11 +36,11 @@ export function isGt(minValue, options) {
7
36
  return input;
8
37
  if (minValue instanceof Date && input instanceof Date && input > minValue)
9
38
  return input;
10
- if (typeof minValue === 'string' && typeof input === 'string' &&
11
- (input > minValue ||
12
- (options?.caseInsensitive && input.toLowerCase() > minValue.toLowerCase())))
39
+ if (typeof minValue === 'string' &&
40
+ typeof input === 'string' &&
41
+ (input > minValue || (options?.caseInsensitive && input.toLowerCase() > minValue.toLowerCase())))
13
42
  return input;
14
- context.fail(_this, `{{label}} must be greater than ${(typeof minValue === 'string' ? `"${minValue}"` : minValue)}`, input);
43
+ context.fail(_this, `{{label}} must be greater than ${typeof minValue === 'string' ? `"${minValue}"` : minValue}`, input);
15
44
  }, options);
16
45
  }
17
46
  export function isGte(minValue, options) {
@@ -22,11 +51,11 @@ export function isGte(minValue, options) {
22
51
  return input;
23
52
  if (minValue instanceof Date && input instanceof Date && input >= minValue)
24
53
  return input;
25
- if (typeof minValue === 'string' && typeof input === 'string' &&
26
- (input >= minValue ||
27
- (options?.caseInsensitive && input.toLowerCase() >= minValue.toLowerCase())))
54
+ if (typeof minValue === 'string' &&
55
+ typeof input === 'string' &&
56
+ (input >= minValue || (options?.caseInsensitive && input.toLowerCase() >= minValue.toLowerCase())))
28
57
  return input;
29
- context.fail(_this, `{{label}} must be greater than or equal to ${(typeof minValue === 'string' ? `"${minValue}"` : minValue)}`, input);
58
+ context.fail(_this, `{{label}} must be greater than or equal to ${typeof minValue === 'string' ? `"${minValue}"` : minValue}`, input);
30
59
  }, options);
31
60
  }
32
61
  export function isLt(maxValue, options) {
@@ -37,45 +66,39 @@ export function isLt(maxValue, options) {
37
66
  return input;
38
67
  if (maxValue instanceof Date && input instanceof Date && input < maxValue)
39
68
  return input;
40
- if (typeof maxValue === 'string' && typeof input === 'string' &&
41
- (input < maxValue ||
42
- (options?.caseInsensitive && input.toLowerCase() < maxValue.toLowerCase())))
69
+ if (typeof maxValue === 'string' &&
70
+ typeof input === 'string' &&
71
+ (input < maxValue || (options?.caseInsensitive && input.toLowerCase() < maxValue.toLowerCase())))
43
72
  return input;
44
- context.fail(_this, `{{label}} must be lover than ${(typeof maxValue === 'string' ? `"${maxValue}"` : maxValue)}`, input);
73
+ context.fail(_this, `{{label}} must be lover than ${typeof maxValue === 'string' ? `"${maxValue}"` : maxValue}`, input);
45
74
  }, options);
46
75
  }
47
76
  export function isLte(maxValue, options) {
48
- return validator('isLt', function (input, context, _this) {
77
+ return validator('isLte', function (input, context, _this) {
49
78
  if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
50
79
  (typeof input === 'number' || typeof input === 'bigint') &&
51
80
  input <= maxValue)
52
81
  return input;
53
82
  if (maxValue instanceof Date && input instanceof Date && input <= maxValue)
54
83
  return input;
55
- if (typeof maxValue === 'string' && typeof input === 'string' &&
56
- (input <= maxValue ||
57
- (options?.caseInsensitive && input.toLowerCase() <= maxValue.toLowerCase())))
84
+ if (typeof maxValue === 'string' &&
85
+ typeof input === 'string' &&
86
+ (input <= maxValue || (options?.caseInsensitive && input.toLowerCase() <= maxValue.toLowerCase())))
58
87
  return input;
59
- context.fail(_this, `{{label}} must be lover than or equal to ${(typeof maxValue === 'string' ? `"${maxValue}"` : maxValue)}`, input);
88
+ context.fail(_this, `{{label}} must be lover than or equal to ${typeof maxValue === 'string' ? `"${maxValue}"` : maxValue}`, input);
60
89
  }, options);
61
90
  }
62
91
  /**
63
- * Checks if value is between minValue and maxValue
64
- * @validator range
92
+ * Checks the length is at least "minValue"
93
+ * @validator lengthMin
65
94
  */
66
- export function range(minValue, maxValue, options) {
67
- return validator('range', function (input, context, _this) {
68
- if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
69
- (typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
70
- (typeof input === 'number' || typeof input === 'bigint') &&
71
- input >= minValue && input <= maxValue)
72
- return input;
73
- if (minValue instanceof Date && maxValue instanceof Date &&
74
- input instanceof Date && input >= minValue && input <= maxValue)
75
- return input;
76
- if (typeof minValue === 'string' && typeof maxValue === 'string' &&
77
- typeof input === 'string' && input >= minValue && input <= maxValue)
78
- return input;
79
- context.fail(_this, `Value must be between ${minValue} and ${maxValue}`, input);
80
- }, options);
81
- }
95
+ export const lengthMin = (minValue) => allOf(pipe(getLength(), isGte(minValue, {
96
+ onFail: () => `The length of {{label}} must be at least ${minValue}`,
97
+ })));
98
+ /**
99
+ * Checks if the length is at most "maxValue"
100
+ * @validator lengthMax
101
+ */
102
+ export const lengthMax = (maxValue) => allOf(pipe(getLength(), isLte(maxValue, {
103
+ onFail: () => `The length of {{label}} must be at most ${maxValue}`,
104
+ })));
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Does nothing, just returns original input value.
4
4
  * @validator isAny
@@ -1,4 +1,4 @@
1
- import { validator, } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if value is "array" and applies validation for each item.
4
4
  * Converts input value to array if coerce option is set to 'true'.
@@ -25,9 +25,7 @@ export function isArray(itemValidator, options) {
25
25
  v = input[i];
26
26
  itemContext.scope = input;
27
27
  // itemContext.location = location + '[' + i + ']';
28
- itemContext.location = context.location
29
- ? context.location + `[${i}]`
30
- : `<Array>[${i}]`;
28
+ itemContext.location = context.location ? context.location + `[${i}]` : `<Array>[${i}]`;
31
29
  itemContext.index = i;
32
30
  v = itemValidator(v, itemContext);
33
31
  out.push(v);
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if value is "BigInt".
4
4
  * Converts input value to number if coerce option is set to 'true'.
@@ -9,8 +9,7 @@ export function isBigint(options) {
9
9
  const coerce = options?.coerce || context.coerce;
10
10
  if (typeof input === 'bigint')
11
11
  return input;
12
- if ((typeof input === 'number' && !isNaN(input)) ||
13
- (typeof input === 'string' && coerce))
12
+ if ((typeof input === 'number' && !isNaN(input)) || (typeof input === 'string' && coerce))
14
13
  return BigInt(input);
15
14
  const t = typeof input === 'string' ? 'String ' : '';
16
15
  context.fail(_this, `${t}"{{value}}" is not a valid BigInt value`, input);
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  const TRUE_PATTERN = /^true|t|1|yes|y$/i;
3
3
  const FALSE_PATTERN = /^false|f|0|no|n$/i;
4
4
  /**
@@ -1,7 +1,9 @@
1
1
  import { formatISO, parseISO } from 'date-fns';
2
- import { validator } from '../core/index.js';
2
+ import { validator } from '../../core/index.js';
3
3
  /* eslint-disable-next-line max-len */ // noinspection RegExpUnnecessaryNonCapturingGroup
4
- const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[T ](([0-1][0-9]|2[0-4]):([0-5][0-9])(?::([0-5][0-9]))?(?:\.(\d{0,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
4
+ const DATE_PATTERN =
5
+ // eslint-disable-next-line max-len
6
+ /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[T ](([0-1][0-9]|2[0-4]):([0-5][0-9])(?::([0-5][0-9]))?(?:\.(\d{0,3}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
5
7
  /**
6
8
  * Validates if value is instance of "Date".
7
9
  * Converts input value to Date if coerce option is set to 'true'.
@@ -81,9 +83,7 @@ export function isDateString(options) {
81
83
  }
82
84
  }
83
85
  else if (input instanceof Date) {
84
- return trim === 'date'
85
- ? formatISO(input, { representation: 'date' })
86
- : formatISO(input).substring(0, 19);
86
+ return trim === 'date' ? formatISO(input, { representation: 'date' }) : formatISO(input).substring(0, 19);
87
87
  }
88
88
  context.fail(_this, `"{{value}}" is not a valid date string`, input, { ...options });
89
89
  }, options);
@@ -1,14 +1,13 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if given value is one of enum values.
4
4
  * @validator isEnum
5
5
  */
6
6
  export function isEnum(values, options) {
7
- const caseInSensitive = !!(options?.caseInSensitive);
7
+ const caseInSensitive = !!options?.caseInSensitive;
8
8
  const enumName = options?.enumName;
9
9
  // Prepare an object for fast lookup
10
- const valObj = (Array.isArray(values) ? values : [values])
11
- .reduce((a, v) => {
10
+ const valObj = (Array.isArray(values) ? values : [values]).reduce((a, v) => {
12
11
  if (typeof v === 'string' && caseInSensitive)
13
12
  a[v.toUpperCase()] = true;
14
13
  else
@@ -18,6 +17,8 @@ export function isEnum(values, options) {
18
17
  return validator('isEnum', function (input, context, _this) {
19
18
  if (input != null && valObj[typeof input === 'string' && caseInSensitive ? input.toUpperCase() : input])
20
19
  return input;
21
- context.fail(_this, `Value must be one of enumeration member${enumName ? ` (${enumName})` : ''}`, input, { enum: enumName });
20
+ context.fail(_this, `Value must be one of enumeration member${enumName ? ` (${enumName})` : ''}`, input, {
21
+ enum: enumName,
22
+ });
22
23
  }, options);
23
24
  }
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if value is "integer".
4
4
  * Converts input value to integer number if coerce option is set to 'true'.
@@ -18,8 +18,7 @@ export function isInteger(options) {
18
18
  }
19
19
  if (typeof input === 'number' && !isNaN(input) && Number.isInteger(input))
20
20
  return input;
21
- const t = typeof input === 'bigint' ? 'BigInt ' :
22
- (typeof input === 'string' ? 'String ' : '');
21
+ const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
23
22
  context.fail(_this, `${t}"{{value}}" is not a valid integer value`, input);
24
23
  }, options);
25
24
  }
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if value is "null".
4
4
  * @validator isNull
@@ -27,27 +27,3 @@ export function isNullish(options) {
27
27
  context.fail(_this, `"{{value}}" is not nullish`, input);
28
28
  }, options);
29
29
  }
30
- /**
31
- * Validates if value is not "undefined" nor "null"
32
- * @validator isDefined
33
- */
34
- export function isDefined(options) {
35
- return validator('is-defined', function (input, context, _this) {
36
- if (input !== undefined)
37
- return input;
38
- context.fail(_this, `Is not defined`, input);
39
- }, options);
40
- }
41
- /**
42
- * Validates if value is "undefined"
43
- * @validator isUndefined
44
- */
45
- export function isUndefined(options) {
46
- return validator('isUndefined', function (input, context, _this) {
47
- if (options?.coerce || context.coerce)
48
- return undefined;
49
- if (input === undefined)
50
- return;
51
- context.fail(_this, `{{label}} mustn\'t be defined`, input);
52
- }, options);
53
- }
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if value is "number".
4
4
  * Converts input value to number if coerce option is set to 'true'.
@@ -18,8 +18,7 @@ export function isNumber(options) {
18
18
  }
19
19
  if (typeof input === 'number' && !isNaN(input))
20
20
  return input;
21
- const t = typeof input === 'bigint' ? 'BigInt ' :
22
- (typeof input === 'string' ? 'String ' : '');
21
+ const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
23
22
  context.fail(_this, `${t}"{{value}}" is not a valid number value`, input);
24
23
  }, options);
25
24
  }
@@ -1,5 +1,5 @@
1
- import { postValidation, preValidation } from '../constants.js';
2
- import { isValidator, validator } from '../core/index.js';
1
+ import { postValidation, preValidation } from '../../constants.js';
2
+ import { isValidator, validator, } from '../../core/index.js';
3
3
  /**
4
4
  * Validates object according to schema
5
5
  * Converts properties according to schema rules if coerce option is set to 'true'.
@@ -66,8 +66,7 @@ export function isObject(schema, options) {
66
66
  inputKey = keys[i];
67
67
  schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
68
68
  v = input[inputKey];
69
- _propRule = propertyRules[schemaKey] ||
70
- (isValidator(additionalFields) ? additionalFields : undefined);
69
+ _propRule = propertyRules[schemaKey] || (isValidator(additionalFields) ? additionalFields : undefined);
71
70
  if (_propRule) {
72
71
  if (processedSchemaKeys[schemaKey])
73
72
  continue;
@@ -1,4 +1,4 @@
1
- import { validator, } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates record object according to given "key" and "value" rules
4
4
  * Converts properties according to rules if coerce option is set to 'true'.
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if value is "string".
4
4
  * Converts input value to string if coerce option is set to 'true'.
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  export function isTuple(items, options) {
3
3
  return validator('isTuple', function (input, context, _this) {
4
4
  const coerce = options?.coerce || context.coerce;
@@ -22,8 +22,7 @@ export function isTuple(items, options) {
22
22
  itemContext.label = context.label ? context.label + `[${i}]` : undefined;
23
23
  itemContext.index = i;
24
24
  itemContext.location = location + '[' + i + ']';
25
- itemContext.label = (itemContext.label || itemContext.property || 'Value at ') +
26
- `[${i}]`;
25
+ itemContext.label = (itemContext.label || itemContext.property || 'Value at ') + `[${i}]`;
27
26
  v = itemRule(input[i], itemContext);
28
27
  out.push(v);
29
28
  }
@@ -0,0 +1,12 @@
1
+ import { validator } from '../../core/index.js';
2
+ /**
3
+ * Validates if property exists
4
+ * @validator exists
5
+ */
6
+ export function exists(options) {
7
+ return validator('exists', function (input, context, _this) {
8
+ if (input !== undefined || (context.scope && Object.getOwnPropertyDescriptor(context.scope, input)))
9
+ return input;
10
+ context.fail(_this, `{{label}} must exist`, input);
11
+ }, options);
12
+ }
@@ -0,0 +1,20 @@
1
+ import { validator } from '../../core/index.js';
2
+ /**
3
+ * Returns length of an Array, String, ArrayBuffer, Buffer or any object with the "length" property.
4
+ * @validator getLength
5
+ */
6
+ export function getLength() {
7
+ return validator('getLength', function (input, context, _this) {
8
+ if (typeof input === 'string')
9
+ return input.length;
10
+ if (Array.isArray(input))
11
+ return input.length;
12
+ if (input instanceof ArrayBuffer)
13
+ return input.byteLength;
14
+ if (input && typeof input === 'object' && typeof input.length === 'number')
15
+ return input.length;
16
+ if (input && typeof input === 'object' && typeof input.size === 'number')
17
+ return input.size;
18
+ context.fail(_this, `Unable to get length of {{label}}`, input);
19
+ });
20
+ }
@@ -0,0 +1,12 @@
1
+ import { validator } from '../../core/index.js';
2
+ /**
3
+ * Makes sub-rule optional
4
+ * @validator optional
5
+ */
6
+ export function optional(nested, options) {
7
+ return validator('optional', function (input, context) {
8
+ if (input == null)
9
+ return input;
10
+ return nested(input, context);
11
+ }, options);
12
+ }
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  *
4
4
  * @validator pipe
@@ -0,0 +1,14 @@
1
+ import { validator } from '../../core/index.js';
2
+ /**
3
+ * Makes sub-rule required
4
+ * @validator required
5
+ */
6
+ export function required(nested, options) {
7
+ return validator('required', function (input, context, _this) {
8
+ if (input == null) {
9
+ context.fail(_this, `{{label}} is required`, input);
10
+ return;
11
+ }
12
+ return nested(input, context);
13
+ }, options);
14
+ }
@@ -1,4 +1,4 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  *
4
4
  */
@@ -10,7 +10,7 @@ export function union(codecs) {
10
10
  let v;
11
11
  let passed = false;
12
12
  // Mock fail method to prevent errors
13
- context.fail = () => passed = false;
13
+ context.fail = () => (passed = false);
14
14
  for (i = 0; i < l; i++) {
15
15
  c = codecs[i];
16
16
  try {
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.3.0",
4
+ "version": "5.3.2",
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.11.9",
15
+ "@types/validator": "^13.11.10",
16
16
  "date-fns": "^3.6.0",
17
17
  "ts-gems": "^3.4.0",
18
- "validator": "^13.11.0"
18
+ "validator": "^13.12.0"
19
19
  },
20
20
  "type": "module",
21
21
  "types": "typings/index.d.ts",
@@ -1,7 +1,7 @@
1
1
  import * as vg from './rules/index.js';
2
2
  export * from './constants.js';
3
3
  export * from './core/index.js';
4
- export { IsObject } from './rules/is-object.js';
4
+ export { IsObject } from './rules/type-rules/is-object.js';
5
5
  declare const isAny: import("./core/validator.js").Validator<any, any, import("./core/types.js").ExecutionOptions>;
6
6
  declare const isArray: import("./core/validator.js").Validator<unknown[], unknown, import("./core/types.js").ExecutionOptions>;
7
7
  declare const isBigint: import("./core/validator.js").Validator<bigint, unknown, import("./core/types.js").ExecutionOptions>;
@@ -33,7 +33,7 @@ declare const isMACAddress: import("./core/validator.js").Validator<string, stri
33
33
  declare const isPort: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
34
34
  declare const isURL: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
35
35
  declare const isBase64: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
36
- declare const isBIC: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
36
+ declare const isSWIFT: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
37
37
  declare const isCreditCard: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
38
38
  declare const isIBAN: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
39
39
  declare const isEAN: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
@@ -49,7 +49,7 @@ declare const isAlpha: import("./core/validator.js").Validator<string, string, i
49
49
  declare const isAlphanumeric: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
50
50
  declare const isAscii: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
51
51
  declare const isDecimal: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
52
- declare const isHexadecimal: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
52
+ declare const isHex: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
53
53
  declare const toArray: import("./core/validator.js").Validator<any[], any, import("./core/types.js").ExecutionOptions>;
54
54
  declare const toBoolean: import("./core/validator.js").Validator<boolean | undefined, unknown, import("./core/types.js").ExecutionOptions>;
55
55
  declare const toDate: import("./core/validator.js").Validator<Date, string | number | Date, import("./core/types.js").ExecutionOptions>;
@@ -57,4 +57,4 @@ declare const toDateString: import("./core/validator.js").Validator<string, stri
57
57
  declare const toInteger: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
58
58
  declare const toNumber: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
59
59
  declare const toString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
60
- export { vg, isAny, isArray, isBigint, isBoolean, isDate, isDateString, isEmpty, isNotEmpty, isInteger, isNull, isNullish, isDefined, isUndefined, isNumber, isObject, isObjectId, isString, isUUID, isUUID1, isUUID2, isUUID3, isUUID4, isUUID5, isEmail, isMobilePhone, isIP, isIPRange, isMACAddress, isPort, isURL, isBase64, isBIC, isCreditCard, isIBAN, isEAN, isFQDN, isISSN, isBtcAddress, isETHAddress, isHexColor, isJWT, isLowercase, isUppercase, isAlpha, isAlphanumeric, isAscii, isDecimal, isHexadecimal, toArray, toBoolean, toDate, toDateString, toInteger, toNumber, toString };
60
+ export { vg, isAny, isArray, isBigint, isBoolean, isDate, isDateString, isEmpty, isNotEmpty, isInteger, isNull, isNullish, isDefined, isUndefined, isNumber, isObject, isObjectId, isString, isUUID, isUUID1, isUUID2, isUUID3, isUUID4, isUUID5, isEmail, isMobilePhone, isIP, isIPRange, isMACAddress, isPort, isURL, isBase64, isSWIFT, isCreditCard, isIBAN, isEAN, isFQDN, isISSN, isBtcAddress, isETHAddress, isHexColor, isJWT, isLowercase, isUppercase, isAlpha, isAlphanumeric, isAscii, isDecimal, isHex, toArray, toBoolean, toDate, toDateString, toInteger, toNumber, toString, };
@@ -0,0 +1,11 @@
1
+ import { ValidationOptions } from '../../core/index.js';
2
+ /**
3
+ * Check if the string contains only letters (a-zA-Z).
4
+ * @validator isAlpha
5
+ */
6
+ export declare function isAlpha(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
7
+ /**
8
+ * Check if the string contains only letters and numbers.
9
+ * @validator isAlphanumeric
10
+ */
11
+ export declare function isAlphanumeric(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
@@ -0,0 +1,6 @@
1
+ import { ValidationOptions } from '../../core/index.js';
2
+ /**
3
+ * Check if the string contains ASCII chars only.
4
+ * @validator isAscii
5
+ */
6
+ export declare function isAscii(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;