valgen 5.2.2 → 5.3.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.
Files changed (176) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/cjs/core/context.js +7 -4
  3. package/cjs/core/validator.js +4 -4
  4. package/cjs/index.js +20 -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} +4 -4
  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} +13 -3
  35. package/cjs/rules/logical-rules/range.js +114 -0
  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} +5 -6
  38. package/cjs/rules/{is-bigint.js → type-rules/is-bigint.js} +5 -4
  39. package/cjs/rules/{is-boolean.js → type-rules/is-boolean.js} +5 -3
  40. package/cjs/rules/{is-date.js → type-rules/is-date.js} +12 -10
  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} +5 -3
  43. package/cjs/rules/type-rules/is-null.js +34 -0
  44. package/cjs/rules/{is-number.js → type-rules/is-number.js} +6 -4
  45. package/cjs/rules/{is-object.js → type-rules/is-object.js} +5 -5
  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} +3 -2
  48. package/cjs/rules/{is-tuple.js → type-rules/is-tuple.js} +5 -5
  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} +3 -3
  55. package/esm/core/context.js +7 -4
  56. package/esm/core/validator.js +4 -4
  57. package/esm/index.js +10 -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} +4 -4
  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} +13 -3
  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} +5 -6
  91. package/esm/rules/{is-bigint.js → type-rules/is-bigint.js} +5 -4
  92. package/esm/rules/{is-boolean.js → type-rules/is-boolean.js} +5 -3
  93. package/esm/rules/{is-date.js → type-rules/is-date.js} +12 -10
  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} +5 -3
  96. package/esm/rules/type-rules/is-null.js +29 -0
  97. package/esm/rules/{is-number.js → type-rules/is-number.js} +6 -4
  98. package/esm/rules/{is-object.js → type-rules/is-object.js} +5 -5
  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} +3 -2
  101. package/esm/rules/{is-tuple.js → type-rules/is-tuple.js} +5 -5
  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} +3 -3
  108. package/package.json +2 -2
  109. package/typings/core/types.d.ts +1 -0
  110. package/typings/index.d.ts +11 -4
  111. package/typings/rules/format-rules/is-alpha.d.ts +11 -0
  112. package/typings/rules/format-rules/is-ascii.d.ts +6 -0
  113. package/typings/rules/format-rules/is-base64.d.ts +9 -0
  114. package/typings/rules/format-rules/is-btc-address.d.ts +6 -0
  115. package/typings/rules/format-rules/is-credit-card.d.ts +9 -0
  116. package/typings/rules/format-rules/is-decimal.d.ts +7 -0
  117. package/typings/rules/format-rules/is-ean.d.ts +6 -0
  118. package/typings/rules/format-rules/is-email.d.ts +62 -0
  119. package/typings/rules/format-rules/is-eth-address.d.ts +6 -0
  120. package/typings/rules/format-rules/is-fqdn.d.ts +13 -0
  121. package/typings/rules/format-rules/is-hash.d.ts +8 -0
  122. package/typings/rules/format-rules/is-hex-color.d.ts +6 -0
  123. package/typings/rules/format-rules/is-hex.d.ts +6 -0
  124. package/typings/rules/format-rules/is-iban.d.ts +11 -0
  125. package/typings/rules/format-rules/is-ip.d.ts +11 -0
  126. package/typings/rules/format-rules/is-issn.d.ts +14 -0
  127. package/typings/rules/format-rules/is-jwt.d.ts +6 -0
  128. package/typings/rules/format-rules/is-lowercase.d.ts +11 -0
  129. package/typings/rules/format-rules/is-mac-address.d.ts +21 -0
  130. package/typings/rules/format-rules/is-mobile-phone.d.ts +20 -0
  131. package/typings/rules/{is-object-id.d.ts → format-rules/is-object-id.d.ts} +2 -2
  132. package/typings/rules/format-rules/is-passport-number.d.ts +6 -0
  133. package/typings/rules/format-rules/is-port.d.ts +6 -0
  134. package/typings/rules/format-rules/is-url.d.ts +9 -0
  135. package/typings/rules/format-rules/is-uuid.d.ts +6 -0
  136. package/typings/rules/format-rules/is-vat-number.d.ts +6 -0
  137. package/typings/rules/format-rules/matches.d.ts +9 -0
  138. package/typings/rules/index.d.ts +49 -22
  139. package/typings/rules/logical-rules/is-defined.d.ts +11 -0
  140. package/typings/rules/{is-empty.d.ts → logical-rules/is-empty.d.ts} +3 -3
  141. package/typings/rules/{range.d.ts → logical-rules/range.d.ts} +14 -4
  142. package/typings/rules/type-rules/is-any.d.ts +5 -0
  143. package/typings/rules/{is-array.d.ts → type-rules/is-array.d.ts} +2 -2
  144. package/typings/rules/{is-bigint.d.ts → type-rules/is-bigint.d.ts} +2 -2
  145. package/typings/rules/{is-boolean.d.ts → type-rules/is-boolean.d.ts} +2 -2
  146. package/typings/rules/{is-date.d.ts → type-rules/is-date.d.ts} +3 -3
  147. package/typings/rules/{is-enum.d.ts → type-rules/is-enum.d.ts} +1 -1
  148. package/typings/rules/{is-integer.d.ts → type-rules/is-integer.d.ts} +2 -2
  149. package/typings/rules/type-rules/is-null.d.ts +11 -0
  150. package/typings/rules/{is-number.d.ts → type-rules/is-number.d.ts} +2 -2
  151. package/typings/rules/{is-object.d.ts → type-rules/is-object.d.ts} +1 -1
  152. package/typings/rules/{is-record.d.ts → type-rules/is-record.d.ts} +2 -2
  153. package/typings/rules/{is-string.d.ts → type-rules/is-string.d.ts} +5 -5
  154. package/typings/rules/{is-tuple.d.ts → type-rules/is-tuple.d.ts} +2 -8
  155. package/typings/rules/utility-rules/exists.d.ts +6 -0
  156. package/typings/rules/utility-rules/get-length.d.ts +11 -0
  157. package/typings/rules/utility-rules/optional.d.ts +7 -0
  158. package/typings/rules/{pipe.d.ts → utility-rules/pipe.d.ts} +1 -1
  159. package/typings/rules/utility-rules/required.d.ts +7 -0
  160. package/typings/rules/utility-rules/union.d.ts +5 -0
  161. package/cjs/rules/is-null.js +0 -58
  162. package/cjs/rules/length.js +0 -41
  163. package/cjs/rules/optional.js +0 -44
  164. package/cjs/rules/range.js +0 -89
  165. package/cjs/rules/string-formats.js +0 -377
  166. package/esm/rules/is-null.js +0 -51
  167. package/esm/rules/length.js +0 -35
  168. package/esm/rules/optional.js +0 -38
  169. package/esm/rules/string-formats.js +0 -322
  170. package/typings/rules/is-any.d.ts +0 -5
  171. package/typings/rules/is-null.d.ts +0 -21
  172. package/typings/rules/is-regexp.d.ts +0 -9
  173. package/typings/rules/length.d.ts +0 -21
  174. package/typings/rules/optional.d.ts +0 -17
  175. package/typings/rules/string-formats.d.ts +0 -166
  176. package/typings/rules/union.d.ts +0 -5
@@ -0,0 +1,24 @@
1
+ import * as validatorJS from 'validator';
2
+ import { validator } from '../../core/index.js';
3
+ /**
4
+ * Validates if value a Lowercase string
5
+ * @validator isLowercase
6
+ */
7
+ export function isLowercase(options) {
8
+ return validator('isLowercase', function (input, context, _this) {
9
+ if (typeof input === 'string' && validatorJS.isLowercase(input))
10
+ return input;
11
+ context.fail(_this, `"{{value}}" is not a lowercase string`, input);
12
+ }, options);
13
+ }
14
+ /**
15
+ * Validates if value a Uppercase string
16
+ * @validator isUppercase
17
+ */
18
+ export function isUppercase(options) {
19
+ return validator('isUppercase', function (input, context, _this) {
20
+ if (typeof input === 'string' && validatorJS.isUppercase(input))
21
+ return input;
22
+ context.fail(_this, `"{{value}}" is not an uppercase string`, input);
23
+ }, options);
24
+ }
@@ -0,0 +1,18 @@
1
+ import * as validatorJS from 'validator';
2
+ import { validator } from '../../core/index.js';
3
+ /**
4
+ * Validates if value is an MACAddress
5
+ * @validator isMACAddress
6
+ */
7
+ export function isMACAddress(options) {
8
+ const opts = {
9
+ // eslint-disable-next-line camelcase
10
+ no_separators: options?.noSeparators,
11
+ eui: options?.eui,
12
+ };
13
+ return validator('isMACAddress', function (input, context, _this) {
14
+ if (input != null && typeof input === 'string' && validatorJS.isMACAddress(input, opts))
15
+ return input;
16
+ context.fail(_this, `{{label}} is not a valid MAC address`, input);
17
+ }, options);
18
+ }
@@ -0,0 +1,16 @@
1
+ import * as validatorJS from 'validator';
2
+ import { validator } from '../../core/index.js';
3
+ /**
4
+ * Validates if value is a valid Email
5
+ * @validator isMobilePhone
6
+ */
7
+ export function isMobilePhone(options) {
8
+ const opts = {
9
+ strictMode: options?.strictMode,
10
+ };
11
+ return validator('isMobilePhone', function (input, context, _this) {
12
+ if (typeof input === 'string' && validatorJS.isMobilePhone(input, options?.locale, opts))
13
+ return input;
14
+ context.fail(_this, `"{{value}}" is not a valid a Mobile Phone Number`, input);
15
+ }, options);
16
+ }
@@ -1,5 +1,5 @@
1
1
  import validatorJS from 'validator';
2
- import { validator } from '../core/index.js';
2
+ import { validator } from '../../core/index.js';
3
3
  /**
4
4
  * Validates if value is an "ObjectId".
5
5
  * @validator isObjectId
@@ -12,10 +12,10 @@ export function isObjectId(options) {
12
12
  typeof input.toHexString === 'function' &&
13
13
  _isObjectIdValue(input.toHexString()))))
14
14
  return input;
15
- context.fail(_this, `{{label}} must be an ObjectId`, input);
15
+ context.fail(_this, `"{{value}}" is not a valid ObjectId`, input);
16
16
  }, options);
17
17
  }
18
18
  function _isObjectIdValue(input) {
19
- return (input instanceof Uint8Array && input.length === 12) ||
20
- (typeof input === 'string' && input.length === 24 && validatorJS.isHexadecimal(input));
19
+ return ((input instanceof Uint8Array && input.length === 12) ||
20
+ (typeof input === 'string' && input.length === 24 && validatorJS.isHexadecimal(input)));
21
21
  }
@@ -0,0 +1,13 @@
1
+ import * as validatorJS from 'validator';
2
+ import { validator } from '../../core/index.js';
3
+ /**
4
+ * Validates if value is a passport number
5
+ * @validator isPassportNumber
6
+ */
7
+ export function isPassportNumber(countryCode, options) {
8
+ return validator('isPassportNumber', function (input, context, _this) {
9
+ if (typeof input === 'string' && validatorJS.isPassportNumber(input, countryCode))
10
+ return input;
11
+ context.fail(_this, `"{{value}}" is not a valid ${countryCode} PassportNumber)`, input);
12
+ }, options);
13
+ }
@@ -0,0 +1,13 @@
1
+ import * as validatorJS from 'validator';
2
+ import { validator } from '../../core/index.js';
3
+ /**
4
+ * Validates if value is a port number
5
+ * @validator isPort
6
+ */
7
+ export function isPort(options) {
8
+ return validator('isPort', function (input, context, _this) {
9
+ if (input != null && typeof input === 'string' && validatorJS.isPort(input))
10
+ return input;
11
+ context.fail(_this, `{{label}} is not a valid port number`, input);
12
+ }, options);
13
+ }
@@ -0,0 +1,13 @@
1
+ import * as validatorJS from 'validator';
2
+ import { validator } from '../../core/index.js';
3
+ /**
4
+ * Validates if value is an URL
5
+ * @validator isURL
6
+ */
7
+ export function isURL(options) {
8
+ return validator('isURL', function (input, context, _this) {
9
+ if (input != null && typeof input === 'string' && validatorJS.isURL(input, options))
10
+ return input;
11
+ context.fail(_this, `{{value}} is not a valid URL`, input);
12
+ }, options);
13
+ }
@@ -0,0 +1,13 @@
1
+ import * as 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, _this) {
9
+ if (input != null && typeof input === 'string' && validatorJS.isUUID(input, version))
10
+ return input;
11
+ context.fail(_this, `{{label}} is not a valid UUID${version ? ' v' + version : ''}`, input);
12
+ }, options);
13
+ }
@@ -0,0 +1,13 @@
1
+ import * as validatorJS from 'validator';
2
+ import { validator } from '../../core/index.js';
3
+ /**
4
+ * Validates if value is an VAT number
5
+ * @validator isVAT
6
+ */
7
+ export function isVATNumber(countryCode, options) {
8
+ return validator('isVATNumber', function (input, context, _this) {
9
+ if (typeof input === 'string' && validatorJS.isVAT(input, countryCode))
10
+ return input;
11
+ context.fail(_this, `"{{value}}" is not a valid VAT number`, input);
12
+ }, options);
13
+ }
@@ -1,9 +1,9 @@
1
- import { validator } from '../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Coerces given value to "UUID" format or returns undefined if nullish
4
4
  * @validator uuid
5
5
  */
6
- export function isRegExp(format, options) {
6
+ export function matches(format, options) {
7
7
  const regExp = format instanceof RegExp ? format : new RegExp(format);
8
8
  const formatName = options?.formatName;
9
9
  return validator('matches', function (input, context, _this) {
@@ -11,6 +11,9 @@ export function isRegExp(format, options) {
11
11
  return;
12
12
  if (typeof input === 'string' && regExp.test(input))
13
13
  return input;
14
- context.fail(_this, `{{label}} does not match ${formatName || 'requested'} format`, input, { format, formatName });
14
+ context.fail(_this, `"{{value}}" does not match ${formatName || 'requested'} format`, input, {
15
+ format,
16
+ formatName,
17
+ });
15
18
  }, options);
16
19
  }
@@ -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
@@ -10,24 +10,29 @@ export function isEmpty(options) {
10
10
  if (typeof input === 'string') {
11
11
  if (!input)
12
12
  return input;
13
+ context.fail(_this, `Is not an empty string`, input);
13
14
  }
14
15
  else if (Array.isArray(input)) {
15
16
  if (!input.length)
16
17
  return input;
18
+ context.fail(_this, `Is not an empty array`, input);
17
19
  }
18
20
  else if (input instanceof Set) {
19
21
  if (!input.size)
20
22
  return input;
23
+ context.fail(_this, `Is not an empty Set`, input);
21
24
  }
22
25
  else if (input instanceof Map) {
23
26
  if (!input.size)
24
27
  return input;
28
+ context.fail(_this, `Is not an empty Map`, input);
25
29
  }
26
30
  else if (typeof input === 'object') {
27
31
  if (!Object.keys(input).length)
28
32
  return input;
33
+ context.fail(_this, `Is not an empty Object`, input);
29
34
  }
30
- context.fail(_this, `{{label}} must be empty`, input);
35
+ context.fail(_this, `Is not empty`, input);
31
36
  }, options);
32
37
  }
33
38
  /**
@@ -40,24 +45,29 @@ export function isNotEmpty(options) {
40
45
  if (typeof input === 'string') {
41
46
  if (input)
42
47
  return input;
48
+ context.fail(_this, `Value is an empty string`, input);
43
49
  }
44
50
  else if (Array.isArray(input)) {
45
51
  if (input.length)
46
52
  return input;
53
+ context.fail(_this, `Value is an empty array`, input);
47
54
  }
48
55
  else if (input instanceof Set) {
49
56
  if (input.size)
50
57
  return input;
58
+ context.fail(_this, `Value is an empty Set`, input);
51
59
  }
52
60
  else if (input instanceof Map) {
53
61
  if (input.size)
54
62
  return input;
63
+ context.fail(_this, `Value is an empty Map`, input);
55
64
  }
56
65
  else if (typeof input === 'object') {
57
66
  if (Object.keys(input).length)
58
67
  return input;
68
+ context.fail(_this, `Value is an empty Object`, input);
59
69
  }
60
70
  }
61
- context.fail(_this, `{{label}} mustn't be empty`, input);
71
+ context.fail(_this, `Value is empty`, input);
62
72
  }, options);
63
73
  }
@@ -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, `{{label}} 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'.
@@ -6,10 +6,11 @@ import { validator, } from '../core/index.js';
6
6
  */
7
7
  export function isArray(itemValidator, options) {
8
8
  return validator('isArray', function (input, context, _this) {
9
- if (input != null && context.coerce && !Array.isArray(input))
9
+ const coerce = options?.coerce || context.coerce;
10
+ if (input != null && coerce && !Array.isArray(input))
10
11
  input = [input];
11
12
  if (!Array.isArray(input)) {
12
- context.fail(_this, `{{label}} must be an array`, input);
13
+ context.fail(_this, `"{{value}}" is not an array value`, input);
13
14
  return;
14
15
  }
15
16
  if (!itemValidator)
@@ -24,9 +25,7 @@ export function isArray(itemValidator, options) {
24
25
  v = input[i];
25
26
  itemContext.scope = input;
26
27
  // itemContext.location = location + '[' + i + ']';
27
- itemContext.location = context.location
28
- ? context.location + `[${i}]`
29
- : `<Array>[${i}]`;
28
+ itemContext.location = context.location ? context.location + `[${i}]` : `<Array>[${i}]`;
30
29
  itemContext.index = i;
31
30
  v = itemValidator(v, itemContext);
32
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'.
@@ -6,11 +6,12 @@ import { validator } from '../core/index.js';
6
6
  */
7
7
  export function isBigint(options) {
8
8
  return validator('isBigint', function (input, context, _this) {
9
+ const coerce = options?.coerce || context.coerce;
9
10
  if (typeof input === 'bigint')
10
11
  return input;
11
- if ((typeof input === 'number' && !isNaN(input)) ||
12
- (typeof input === 'string' && context.coerce))
12
+ if ((typeof input === 'number' && !isNaN(input)) || (typeof input === 'string' && coerce))
13
13
  return BigInt(input);
14
- context.fail(_this, `{{label}} must be an integer number`, input);
14
+ const t = typeof input === 'string' ? 'String ' : '';
15
+ context.fail(_this, `${t}"{{value}}" is not a valid BigInt value`, input);
15
16
  }, options);
16
17
  }
@@ -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
  /**
@@ -8,7 +8,8 @@ const FALSE_PATTERN = /^false|f|0|no|n$/i;
8
8
  */
9
9
  export function isBoolean(options) {
10
10
  return validator('isBoolean', function (input, context, _this) {
11
- if (input != null && typeof input !== 'boolean' && context.coerce) {
11
+ const coerce = options?.coerce || context.coerce;
12
+ if (input != null && typeof input !== 'boolean' && coerce) {
12
13
  if (typeof input === 'string') {
13
14
  if (TRUE_PATTERN.test(input))
14
15
  return true;
@@ -21,6 +22,7 @@ export function isBoolean(options) {
21
22
  }
22
23
  if (typeof input === 'boolean')
23
24
  return input;
24
- context.fail(_this, `{{label}} must be a boolean`, input);
25
+ const t = typeof input === 'string' ? 'String ' : '';
26
+ context.fail(_this, `${t}"{{value}}" is not a valid boolean value`, input);
25
27
  }, options);
26
28
  }
@@ -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'.
@@ -10,11 +12,12 @@ const DATE_PATTERN = /^(\d{4})(?:-(0[0-9]|1[0-2]))?(?:-([0-2][0-9]|3[0-1]))?(?:[
10
12
  export function isDate(options) {
11
13
  const precision = options?.precision;
12
14
  return validator('isDate', function (input, context, _this) {
15
+ const coerce = options?.coerce || context.coerce;
13
16
  let d;
14
17
  if (input instanceof Date)
15
18
  d = input;
16
- else if (input != null && context.coerce) {
17
- if (typeof input === 'string' && context.coerce) {
19
+ else if (input != null && coerce) {
20
+ if (typeof input === 'string' && coerce) {
18
21
  d = parseISO(input);
19
22
  }
20
23
  else if (typeof input === 'number')
@@ -33,7 +36,7 @@ export function isDate(options) {
33
36
  d.setHours(0, 0, 0, 0);
34
37
  return d;
35
38
  }
36
- context.fail(_this, `{{label}} must be a Date instance or a date string`, input);
39
+ context.fail(_this, `"{{value}}" is not a valid date value`, input, { ...options });
37
40
  }, options);
38
41
  }
39
42
  /**
@@ -45,6 +48,7 @@ export function isDateString(options) {
45
48
  const precision = options?.precision;
46
49
  const trim = options?.trim;
47
50
  return validator('isDateString', function (input, context, _this) {
51
+ const coerce = options?.coerce || context.coerce;
48
52
  if (typeof input === 'string') {
49
53
  const m = DATE_PATTERN.exec(input);
50
54
  if (m) {
@@ -55,7 +59,7 @@ export function isDateString(options) {
55
59
  (precision === 'month' && m[2]) ||
56
60
  (precision === 'date' && m[2] && m[3]) ||
57
61
  (precision === 'time' && m[2] && m[3] && m[4])) {
58
- if (!context.coerce)
62
+ if (!coerce)
59
63
  return input;
60
64
  let s = m[1];
61
65
  if (m[2])
@@ -79,10 +83,8 @@ export function isDateString(options) {
79
83
  }
80
84
  }
81
85
  else if (input instanceof Date) {
82
- return trim === 'date'
83
- ? formatISO(input, { representation: 'date' })
84
- : formatISO(input).substring(0, 19);
86
+ return trim === 'date' ? formatISO(input, { representation: 'date' }) : formatISO(input).substring(0, 19);
85
87
  }
86
- context.fail(_this, `{{label}} is not a valid date string`, input, { ...options });
88
+ context.fail(_this, `"{{value}}" is not a valid date string`, input, { ...options });
87
89
  }, options);
88
90
  }