valgen 5.4.1 → 5.6.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 (126) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/cjs/core/context.js +13 -10
  3. package/cjs/core/index.js +2 -2
  4. package/cjs/core/utilities.js +4 -5
  5. package/cjs/core/validator.js +10 -7
  6. package/cjs/helpers/object.utils.js +2 -3
  7. package/cjs/helpers/omit-undefined.js +1 -2
  8. package/cjs/helpers/string.utils.js +2 -5
  9. package/cjs/index.js +6 -2
  10. package/cjs/rules/format-rules/is-alpha.js +6 -6
  11. package/cjs/rules/format-rules/is-ascii.js +2 -3
  12. package/cjs/rules/format-rules/is-base64.js +4 -4
  13. package/cjs/rules/format-rules/is-btc-address.js +4 -4
  14. package/cjs/rules/format-rules/is-credit-card.js +5 -4
  15. package/cjs/rules/format-rules/is-decimal.js +4 -4
  16. package/cjs/rules/format-rules/is-ean.js +2 -3
  17. package/cjs/rules/format-rules/is-email.js +4 -4
  18. package/cjs/rules/format-rules/is-eth-address.js +4 -4
  19. package/cjs/rules/format-rules/is-fqdn.js +4 -4
  20. package/cjs/rules/format-rules/is-hash.js +4 -4
  21. package/cjs/rules/format-rules/is-hex-color.js +4 -4
  22. package/cjs/rules/format-rules/is-hex.js +4 -4
  23. package/cjs/rules/format-rules/is-iban.js +4 -5
  24. package/cjs/rules/format-rules/is-ip.js +12 -7
  25. package/cjs/rules/format-rules/is-issn.js +4 -4
  26. package/cjs/rules/format-rules/is-jwt.js +2 -3
  27. package/cjs/rules/format-rules/is-lowercase.js +8 -7
  28. package/cjs/rules/format-rules/is-mac-address.js +6 -4
  29. package/cjs/rules/format-rules/is-mobile-phone.js +5 -4
  30. package/cjs/rules/format-rules/is-object-id.js +7 -5
  31. package/cjs/rules/format-rules/is-passport-number.js +5 -4
  32. package/cjs/rules/format-rules/is-port.js +6 -4
  33. package/cjs/rules/format-rules/is-url.js +6 -4
  34. package/cjs/rules/format-rules/is-uuid.js +6 -4
  35. package/cjs/rules/format-rules/is-vat-number.js +4 -4
  36. package/cjs/rules/format-rules/matches.js +2 -3
  37. package/cjs/rules/index.js +14 -13
  38. package/cjs/rules/logical-rules/is-defined.js +2 -17
  39. package/cjs/rules/logical-rules/is-empty.js +4 -5
  40. package/cjs/rules/logical-rules/range.js +57 -26
  41. package/cjs/rules/type-rules/is-any.js +1 -3
  42. package/cjs/rules/type-rules/is-array.js +5 -4
  43. package/cjs/rules/type-rules/is-bigint.js +5 -4
  44. package/cjs/rules/type-rules/is-boolean.js +2 -3
  45. package/cjs/rules/type-rules/is-date.js +13 -8
  46. package/cjs/rules/type-rules/is-enum.js +7 -4
  47. package/cjs/rules/type-rules/is-integer.js +11 -5
  48. package/cjs/rules/type-rules/is-null.js +34 -14
  49. package/cjs/rules/type-rules/is-number.js +7 -4
  50. package/cjs/rules/type-rules/is-object.js +20 -12
  51. package/cjs/rules/type-rules/is-record.js +2 -3
  52. package/cjs/rules/type-rules/is-string.js +11 -11
  53. package/cjs/rules/type-rules/is-tuple.js +7 -5
  54. package/cjs/rules/type-rules/is-undefined.js +17 -0
  55. package/cjs/rules/utility-rules/exists.js +6 -4
  56. package/cjs/rules/utility-rules/get-length.js +10 -5
  57. package/cjs/rules/utility-rules/optional.js +2 -3
  58. package/cjs/rules/utility-rules/pipe.js +5 -6
  59. package/cjs/rules/utility-rules/required.js +2 -3
  60. package/cjs/rules/utility-rules/union.js +2 -3
  61. package/esm/core/context.js +13 -10
  62. package/esm/core/index.js +2 -2
  63. package/esm/core/utilities.js +3 -3
  64. package/esm/core/validator.js +8 -4
  65. package/esm/helpers/string.utils.js +1 -3
  66. package/esm/index.js +3 -1
  67. package/esm/rules/format-rules/is-alpha.js +4 -3
  68. package/esm/rules/format-rules/is-ascii.js +1 -1
  69. package/esm/rules/format-rules/is-base64.js +3 -2
  70. package/esm/rules/format-rules/is-btc-address.js +3 -2
  71. package/esm/rules/format-rules/is-credit-card.js +4 -2
  72. package/esm/rules/format-rules/is-decimal.js +3 -2
  73. package/esm/rules/format-rules/is-ean.js +1 -1
  74. package/esm/rules/format-rules/is-email.js +3 -2
  75. package/esm/rules/format-rules/is-eth-address.js +3 -2
  76. package/esm/rules/format-rules/is-fqdn.js +3 -2
  77. package/esm/rules/format-rules/is-hash.js +3 -2
  78. package/esm/rules/format-rules/is-hex-color.js +3 -2
  79. package/esm/rules/format-rules/is-hex.js +3 -2
  80. package/esm/rules/format-rules/is-iban.js +2 -2
  81. package/esm/rules/format-rules/is-ip.js +10 -4
  82. package/esm/rules/format-rules/is-issn.js +3 -2
  83. package/esm/rules/format-rules/is-jwt.js +1 -1
  84. package/esm/rules/format-rules/is-lowercase.js +6 -4
  85. package/esm/rules/format-rules/is-mac-address.js +5 -2
  86. package/esm/rules/format-rules/is-mobile-phone.js +4 -2
  87. package/esm/rules/format-rules/is-object-id.js +7 -4
  88. package/esm/rules/format-rules/is-passport-number.js +4 -2
  89. package/esm/rules/format-rules/is-port.js +5 -2
  90. package/esm/rules/format-rules/is-url.js +5 -2
  91. package/esm/rules/format-rules/is-uuid.js +5 -2
  92. package/esm/rules/format-rules/is-vat-number.js +3 -2
  93. package/esm/rules/format-rules/matches.js +2 -2
  94. package/esm/rules/index.js +14 -13
  95. package/esm/rules/logical-rules/is-defined.js +1 -14
  96. package/esm/rules/logical-rules/is-empty.js +2 -2
  97. package/esm/rules/logical-rules/range.js +52 -21
  98. package/esm/rules/type-rules/is-any.js +1 -3
  99. package/esm/rules/type-rules/is-array.js +5 -3
  100. package/esm/rules/type-rules/is-bigint.js +4 -2
  101. package/esm/rules/type-rules/is-boolean.js +2 -2
  102. package/esm/rules/type-rules/is-date.js +11 -5
  103. package/esm/rules/type-rules/is-enum.js +7 -3
  104. package/esm/rules/type-rules/is-integer.js +11 -4
  105. package/esm/rules/type-rules/is-null.js +30 -11
  106. package/esm/rules/type-rules/is-number.js +6 -2
  107. package/esm/rules/type-rules/is-object.js +19 -10
  108. package/esm/rules/type-rules/is-record.js +2 -2
  109. package/esm/rules/type-rules/is-string.js +7 -7
  110. package/esm/rules/type-rules/is-tuple.js +7 -4
  111. package/esm/rules/type-rules/is-undefined.js +14 -0
  112. package/esm/rules/utility-rules/exists.js +5 -2
  113. package/esm/rules/utility-rules/get-length.js +9 -3
  114. package/esm/rules/utility-rules/optional.js +2 -2
  115. package/esm/rules/utility-rules/pipe.js +4 -4
  116. package/esm/rules/utility-rules/required.js +2 -2
  117. package/esm/rules/utility-rules/union.js +1 -1
  118. package/package.json +2 -2
  119. package/typings/core/index.d.ts +3 -3
  120. package/typings/core/types.d.ts +1 -1
  121. package/typings/index.d.ts +3 -1
  122. package/typings/rules/index.d.ts +14 -13
  123. package/typings/rules/logical-rules/is-defined.d.ts +0 -5
  124. package/typings/rules/type-rules/is-null.d.ts +11 -1
  125. package/typings/rules/type-rules/is-tuple.d.ts +36 -4
  126. package/typings/rules/type-rules/is-undefined.d.ts +6 -0
@@ -1,16 +1,3 @@
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
1
  export * from './format-rules/is-alpha.js';
15
2
  export * from './format-rules/is-ascii.js';
16
3
  export * from './format-rules/is-base64.js';
@@ -41,6 +28,20 @@ export * from './format-rules/matches.js';
41
28
  export * from './logical-rules/is-defined.js';
42
29
  export * from './logical-rules/is-empty.js';
43
30
  export * from './logical-rules/range.js';
31
+ export * from './type-rules/is-any.js';
32
+ export * from './type-rules/is-array.js';
33
+ export * from './type-rules/is-bigint.js';
34
+ export * from './type-rules/is-boolean.js';
35
+ export * from './type-rules/is-date.js';
36
+ export * from './type-rules/is-enum.js';
37
+ export * from './type-rules/is-integer.js';
38
+ export * from './type-rules/is-null.js';
39
+ export * from './type-rules/is-number.js';
40
+ export * from './type-rules/is-object.js';
41
+ export * from './type-rules/is-record.js';
42
+ export * from './type-rules/is-string.js';
43
+ export * from './type-rules/is-tuple.js';
44
+ export * from './type-rules/is-undefined.js';
44
45
  export * from './utility-rules/exists.js';
45
46
  export * from './utility-rules/get-length.js';
46
47
  export * from './utility-rules/optional.js';
@@ -4,22 +4,9 @@ import { validator } from '../../core/index.js';
4
4
  * @validator isDefined
5
5
  */
6
6
  export function isDefined(options) {
7
- return validator('is-defined', function (input, context, _this) {
7
+ return validator('is-defined', (input, context, _this) => {
8
8
  if (input !== undefined)
9
9
  return input;
10
10
  context.fail(_this, `Is not defined`, input);
11
11
  }, options);
12
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
- }
@@ -4,7 +4,7 @@ import { validator } from '../../core/index.js';
4
4
  * @validator isEmpty
5
5
  */
6
6
  export function isEmpty(options) {
7
- return validator('isEmpty', function (input, context, _this) {
7
+ return validator('isEmpty', (input, context, _this) => {
8
8
  if (input == null)
9
9
  return input;
10
10
  if (typeof input === 'string') {
@@ -40,7 +40,7 @@ export function isEmpty(options) {
40
40
  * @validator isNotEmpty
41
41
  */
42
42
  export function isNotEmpty(options) {
43
- return validator('isNotEmpty', function (input, context, _this) {
43
+ return validator('isNotEmpty', (input, context, _this) => {
44
44
  if (input != null) {
45
45
  if (typeof input === 'string') {
46
46
  if (input)
@@ -1,4 +1,4 @@
1
- import { validator } from '../../core/index.js';
1
+ import { validator, } from '../../core/index.js';
2
2
  import { getLength } from '../utility-rules/get-length.js';
3
3
  import { allOf, pipe } from '../utility-rules/pipe.js';
4
4
  /**
@@ -6,85 +6,116 @@ import { allOf, pipe } from '../utility-rules/pipe.js';
6
6
  * @validator range
7
7
  */
8
8
  export function range(minValue, maxValue, options) {
9
- return validator('range', function (input, context, _this) {
9
+ return validator('range', (input, context, _this) => {
10
10
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
11
11
  (typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
12
12
  (typeof input === 'number' || typeof input === 'bigint') &&
13
13
  input >= minValue &&
14
- input <= maxValue)
14
+ input <= maxValue) {
15
15
  return input;
16
+ }
16
17
  if (minValue instanceof Date &&
17
18
  maxValue instanceof Date &&
18
19
  input instanceof Date &&
19
20
  input >= minValue &&
20
- input <= maxValue)
21
+ input <= maxValue) {
21
22
  return input;
23
+ }
22
24
  if (typeof minValue === 'string' &&
23
25
  typeof maxValue === 'string' &&
24
26
  typeof input === 'string' &&
25
27
  input >= minValue &&
26
- input <= maxValue)
28
+ input <= maxValue) {
27
29
  return input;
30
+ }
28
31
  context.fail(_this, `Value must be between ${minValue} and ${maxValue}`, input);
29
32
  }, options);
30
33
  }
31
34
  export function isGt(minValue, options) {
32
- return validator('isGt', function (input, context, _this) {
35
+ return validator('isGt', (input, context, _this) => {
33
36
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
34
37
  (typeof input === 'number' || typeof input === 'bigint') &&
35
- input > minValue)
38
+ input > minValue) {
36
39
  return input;
37
- if (minValue instanceof Date && input instanceof Date && input > minValue)
40
+ }
41
+ if (minValue instanceof Date &&
42
+ input instanceof Date &&
43
+ input > minValue) {
38
44
  return input;
45
+ }
39
46
  if (typeof minValue === 'string' &&
40
47
  typeof input === 'string' &&
41
- (input > minValue || (options?.caseInsensitive && input.toLowerCase() > minValue.toLowerCase())))
48
+ (input > minValue ||
49
+ (options?.caseInsensitive &&
50
+ input.toLowerCase() > minValue.toLowerCase()))) {
42
51
  return input;
52
+ }
43
53
  context.fail(_this, `{{label}} must be greater than ${typeof minValue === 'string' ? `"${minValue}"` : minValue}`, input);
44
54
  }, options);
45
55
  }
46
56
  export function isGte(minValue, options) {
47
- return validator('isGte', function (input, context, _this) {
57
+ return validator('isGte', (input, context, _this) => {
48
58
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
49
59
  (typeof input === 'number' || typeof input === 'bigint') &&
50
- input >= minValue)
60
+ input >= minValue) {
51
61
  return input;
52
- if (minValue instanceof Date && input instanceof Date && input >= minValue)
62
+ }
63
+ if (minValue instanceof Date &&
64
+ input instanceof Date &&
65
+ input >= minValue) {
53
66
  return input;
67
+ }
54
68
  if (typeof minValue === 'string' &&
55
69
  typeof input === 'string' &&
56
- (input >= minValue || (options?.caseInsensitive && input.toLowerCase() >= minValue.toLowerCase())))
70
+ (input >= minValue ||
71
+ (options?.caseInsensitive &&
72
+ input.toLowerCase() >= minValue.toLowerCase()))) {
57
73
  return input;
74
+ }
58
75
  context.fail(_this, `{{label}} must be greater than or equal to ${typeof minValue === 'string' ? `"${minValue}"` : minValue}`, input);
59
76
  }, options);
60
77
  }
61
78
  export function isLt(maxValue, options) {
62
- return validator('isLt', function (input, context, _this) {
79
+ return validator('isLt', (input, context, _this) => {
63
80
  if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
64
81
  (typeof input === 'number' || typeof input === 'bigint') &&
65
- input < maxValue)
82
+ input < maxValue) {
66
83
  return input;
67
- if (maxValue instanceof Date && input instanceof Date && input < maxValue)
84
+ }
85
+ if (maxValue instanceof Date &&
86
+ input instanceof Date &&
87
+ input < maxValue) {
68
88
  return input;
89
+ }
69
90
  if (typeof maxValue === 'string' &&
70
91
  typeof input === 'string' &&
71
- (input < maxValue || (options?.caseInsensitive && input.toLowerCase() < maxValue.toLowerCase())))
92
+ (input < maxValue ||
93
+ (options?.caseInsensitive &&
94
+ input.toLowerCase() < maxValue.toLowerCase()))) {
72
95
  return input;
96
+ }
73
97
  context.fail(_this, `{{label}} must be lover than ${typeof maxValue === 'string' ? `"${maxValue}"` : maxValue}`, input);
74
98
  }, options);
75
99
  }
76
100
  export function isLte(maxValue, options) {
77
- return validator('isLte', function (input, context, _this) {
101
+ return validator('isLte', (input, context, _this) => {
78
102
  if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
79
103
  (typeof input === 'number' || typeof input === 'bigint') &&
80
- input <= maxValue)
104
+ input <= maxValue) {
81
105
  return input;
82
- if (maxValue instanceof Date && input instanceof Date && input <= maxValue)
106
+ }
107
+ if (maxValue instanceof Date &&
108
+ input instanceof Date &&
109
+ input <= maxValue) {
83
110
  return input;
111
+ }
84
112
  if (typeof maxValue === 'string' &&
85
113
  typeof input === 'string' &&
86
- (input <= maxValue || (options?.caseInsensitive && input.toLowerCase() <= maxValue.toLowerCase())))
114
+ (input <= maxValue ||
115
+ (options?.caseInsensitive &&
116
+ input.toLowerCase() <= maxValue.toLowerCase()))) {
87
117
  return input;
118
+ }
88
119
  context.fail(_this, `{{label}} must be lover than or equal to ${typeof maxValue === 'string' ? `"${maxValue}"` : maxValue}`, input);
89
120
  }, options);
90
121
  }
@@ -3,6 +3,4 @@ import { validator } from '../../core/index.js';
3
3
  * Does nothing, just returns original input value.
4
4
  * @validator isAny
5
5
  */
6
- export const isAny = () => validator('is-any', function (input) {
7
- return input;
8
- });
6
+ export const isAny = () => validator('is-any', (input) => input);
@@ -1,11 +1,11 @@
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'.
5
5
  * @validator isArray
6
6
  */
7
7
  export function isArray(itemValidator, options) {
8
- return validator('isArray', function (input, context, _this) {
8
+ return validator('isArray', (input, context, _this) => {
9
9
  const coerce = options?.coerce || context.coerce;
10
10
  let output = input;
11
11
  if (output != null && coerce && !Array.isArray(output))
@@ -26,7 +26,9 @@ export function isArray(itemValidator, options) {
26
26
  v = output[i];
27
27
  itemContext.scope = output;
28
28
  // itemContext.location = location + '[' + i + ']';
29
- itemContext.location = context.location ? context.location + `[${i}]` : `<Array>[${i}]`;
29
+ itemContext.location = context.location
30
+ ? context.location + `[${i}]`
31
+ : `<Array>[${i}]`;
30
32
  itemContext.index = i;
31
33
  v = itemValidator(v, itemContext);
32
34
  out.push(v);
@@ -5,12 +5,14 @@ import { validator } from '../../core/index.js';
5
5
  * @validator isNumber
6
6
  */
7
7
  export function isBigint(options) {
8
- return validator('isBigint', function (input, context, _this) {
8
+ return validator('isBigint', (input, context, _this) => {
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)) || (typeof input === 'string' && coerce))
12
+ if ((typeof input === 'number' && !isNaN(input)) ||
13
+ (typeof input === 'string' && coerce)) {
13
14
  return BigInt(input);
15
+ }
14
16
  const t = typeof input === 'string' ? 'String ' : '';
15
17
  context.fail(_this, `${t}"{{value}}" is not a valid BigInt value`, input);
16
18
  }, options);
@@ -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
  /**
@@ -7,7 +7,7 @@ const FALSE_PATTERN = /^false|f|0|no|n$/i;
7
7
  * @validator isBoolean
8
8
  */
9
9
  export function isBoolean(options) {
10
- return validator('isBoolean', function (input, context, _this) {
10
+ return validator('isBoolean', (input, context, _this) => {
11
11
  const coerce = options?.coerce || context.coerce;
12
12
  let output = input;
13
13
  if (output != null && typeof output !== 'boolean' && coerce) {
@@ -11,7 +11,7 @@ const DATE_PATTERN =
11
11
  */
12
12
  export function isDate(options) {
13
13
  const precision = options?.precision;
14
- return validator('isDate', function (input, context, _this) {
14
+ return validator('isDate', (input, context, _this) => {
15
15
  const coerce = options?.coerce || context.coerce;
16
16
  let d;
17
17
  if (input instanceof Date)
@@ -36,7 +36,9 @@ export function isDate(options) {
36
36
  d.setHours(0, 0, 0, 0);
37
37
  return d;
38
38
  }
39
- context.fail(_this, `"{{value}}" is not a valid date value`, input, { ...options });
39
+ context.fail(_this, `"{{value}}" is not a valid date value`, input, {
40
+ ...options,
41
+ });
40
42
  }, options);
41
43
  }
42
44
  /**
@@ -47,7 +49,7 @@ export function isDate(options) {
47
49
  export function isDateString(options) {
48
50
  const precision = options?.precision;
49
51
  const trim = options?.trim;
50
- return validator('isDateString', function (input, context, _this) {
52
+ return validator('isDateString', (input, context, _this) => {
51
53
  const coerce = options?.coerce || context.coerce;
52
54
  if (typeof input === 'string') {
53
55
  const m = DATE_PATTERN.exec(input);
@@ -83,8 +85,12 @@ export function isDateString(options) {
83
85
  }
84
86
  }
85
87
  else if (input instanceof Date) {
86
- return trim === 'date' ? formatISO(input, { representation: 'date' }) : formatISO(input).substring(0, 19);
88
+ return trim === 'date'
89
+ ? formatISO(input, { representation: 'date' })
90
+ : formatISO(input).substring(0, 19);
87
91
  }
88
- context.fail(_this, `"{{value}}" is not a valid date string`, input, { ...options });
92
+ context.fail(_this, `"{{value}}" is not a valid date string`, input, {
93
+ ...options,
94
+ });
89
95
  }, options);
90
96
  }
@@ -1,4 +1,4 @@
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
@@ -14,9 +14,13 @@ export function isEnum(values, options) {
14
14
  a[v] = true;
15
15
  return a;
16
16
  }, {});
17
- return validator('isEnum', function (input, context, _this) {
18
- if (input != null && valObj[typeof input === 'string' && caseInSensitive ? input.toUpperCase() : input])
17
+ return validator('isEnum', (input, context, _this) => {
18
+ if (input != null &&
19
+ valObj[typeof input === 'string' && caseInSensitive
20
+ ? input.toUpperCase()
21
+ : input]) {
19
22
  return input;
23
+ }
20
24
  context.fail(_this, `Value must be one of enumeration member${enumName ? ` (${enumName})` : ''}`, input, {
21
25
  enum: enumName,
22
26
  });
@@ -1,11 +1,11 @@
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'.
5
5
  * @validator isInteger
6
6
  */
7
7
  export function isInteger(options) {
8
- return validator('isInteger', function (input, context, _this) {
8
+ return validator('isInteger', (input, context, _this) => {
9
9
  const coerce = options?.coerce || context.coerce;
10
10
  let output = input;
11
11
  if (output != null && typeof output !== 'number' && coerce) {
@@ -17,9 +17,16 @@ export function isInteger(options) {
17
17
  input = output;
18
18
  }
19
19
  }
20
- if (typeof output === 'number' && !isNaN(output) && Number.isInteger(output))
20
+ if (typeof output === 'number' &&
21
+ !isNaN(output) &&
22
+ Number.isInteger(output)) {
21
23
  return output;
22
- const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
24
+ }
25
+ const t = typeof input === 'bigint'
26
+ ? 'BigInt '
27
+ : typeof input === 'string'
28
+ ? 'String '
29
+ : '';
23
30
  context.fail(_this, `${t}"{{value}}" is not a valid integer value`, input);
24
31
  }, options);
25
32
  }
@@ -4,26 +4,45 @@ import { validator } from '../../core/index.js';
4
4
  * @validator isNull
5
5
  */
6
6
  export function isNull(options) {
7
- return validator('isNull', function (input, context, _this) {
7
+ return validator('isNull', (input, context, _this) => {
8
8
  if (input === null)
9
9
  return input;
10
- const coerce = options?.coerce || context.coerce;
11
- if (coerce)
12
- return null;
13
- context.fail(_this, `"{{value}}" is not null`, input);
10
+ context.fail(_this, `{{label}} is not null`, input);
11
+ }, options);
12
+ }
13
+ /**
14
+ * Validates if value is not "null".
15
+ * @validator isNotNull
16
+ */
17
+ export function isNotNull(options) {
18
+ return validator('isNotNull', (input, context, _this) => {
19
+ if (input !== null)
20
+ return input;
21
+ context.fail(_this, `{{label}} is null`, input);
14
22
  }, options);
15
23
  }
16
24
  /**
17
25
  * Validates if value is "null" or "undefined".
18
- * @validator isNull
26
+ * @validator isNullish
19
27
  */
20
28
  export function isNullish(options) {
21
- return validator('isNullish', function (input, context, _this) {
29
+ return validator('isNullish', (input, context, _this) => {
22
30
  if (input == null)
23
31
  return input;
24
- const coerce = options?.coerce || context.coerce;
25
- if (coerce)
26
- return null;
27
- context.fail(_this, `"{{value}}" is not nullish`, input);
32
+ context.fail(_this, `{{label}} is not nullish`, input);
33
+ }, options);
34
+ }
35
+ /**
36
+ * Validates if value is not "null" nor "undefined".
37
+ * @validator isNotNullish
38
+ */
39
+ export function isNotNullish(options) {
40
+ return validator('isNotNullish', (input, context, _this) => {
41
+ if (input != null)
42
+ return input;
43
+ if (input === null)
44
+ context.fail(_this, `{{label}} is null`, input);
45
+ else
46
+ context.fail(_this, `{{label}} is undefined`, input);
28
47
  }, options);
29
48
  }
@@ -5,7 +5,7 @@ import { validator } from '../../core/index.js';
5
5
  * @validator isNumber
6
6
  */
7
7
  export function isNumber(options) {
8
- return validator('isNumber', function (input, context, _this) {
8
+ return validator('isNumber', (input, context, _this) => {
9
9
  const coerce = options?.coerce || context.coerce;
10
10
  let output = input;
11
11
  if (output != null && typeof output !== 'number' && coerce) {
@@ -19,7 +19,11 @@ export function isNumber(options) {
19
19
  }
20
20
  if (typeof output === 'number' && !isNaN(output))
21
21
  return output;
22
- const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
22
+ const t = typeof input === 'bigint'
23
+ ? 'BigInt '
24
+ : typeof input === 'string'
25
+ ? 'String '
26
+ : '';
23
27
  context.fail(_this, `${t}"{{value}}" is not a valid number value`, input);
24
28
  }, options);
25
29
  }
@@ -19,8 +19,9 @@ export function isObject(schema, options) {
19
19
  const n = schema[k];
20
20
  const key = caseInSensitive ? k.toLowerCase() : k;
21
21
  if (Array.isArray(n)) {
22
- if (!isValidator(n[0]))
22
+ if (!isValidator(n[0])) {
23
23
  throw new TypeError(`Invalid tuple definition in validation schema (${k})`);
24
+ }
24
25
  propertyRules[key] = n[0];
25
26
  propertyOptions[key] = { as: k, ...n[1] };
26
27
  }
@@ -28,18 +29,20 @@ export function isObject(schema, options) {
28
29
  propertyRules[key] = n;
29
30
  propertyOptions[key] = { as: k };
30
31
  }
31
- else
32
+ else {
32
33
  throw new TypeError(`Invalid definition in validation schema (${k})`);
34
+ }
33
35
  }
34
- const _rule = validator('isObject', function (input, context, _this) {
36
+ const _rule = validator('isObject', (input, context, _this) => {
35
37
  let output = input;
36
- if (ctor && ctor[preValidation])
38
+ if (ctor && ctor[preValidation]) {
37
39
  output = ctor[preValidation](output, context, _this);
40
+ }
38
41
  const coerce = options?.coerce || context.coerce;
39
42
  if (typeof output === 'string' && coerce)
40
43
  output = JSON.parse(output);
41
44
  if (!(output && typeof output === 'object')) {
42
- context.fail(_this, `{{label}} must be an object`, input);
45
+ context.fail(_this, `"{{value}}" is not an object`, input);
43
46
  return;
44
47
  }
45
48
  const keys = [...Object.keys(output), ...schemaKeys];
@@ -54,8 +57,9 @@ export function isObject(schema, options) {
54
57
  Object.setPrototypeOf(out, ctor.prototype);
55
58
  if (detectCircular) {
56
59
  context.circMap = context.circMap || new Map();
57
- if (context.circMap.has(output))
60
+ if (context.circMap.has(output)) {
58
61
  return context.circMap.get(output);
62
+ }
59
63
  context.circMap.set(output, out);
60
64
  }
61
65
  if (context.root == null)
@@ -67,7 +71,9 @@ export function isObject(schema, options) {
67
71
  inputKey = keys[i];
68
72
  schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
69
73
  v = output[inputKey];
70
- _propRule = propertyRules[schemaKey] || (isValidator(additionalFields) ? additionalFields : undefined);
74
+ _propRule =
75
+ propertyRules[schemaKey] ||
76
+ (isValidator(additionalFields) ? additionalFields : undefined);
71
77
  if (_propRule) {
72
78
  if (processedSchemaKeys[schemaKey])
73
79
  continue;
@@ -77,18 +83,21 @@ export function isObject(schema, options) {
77
83
  subCtx.context = ctorName;
78
84
  subCtx.location = location + (location ? '.' : '') + schemaKey;
79
85
  subCtx.property = schemaKey;
80
- if (propertyOptions[schemaKey]?.label)
86
+ if (propertyOptions[schemaKey]?.label) {
81
87
  subCtx.label = propertyOptions[schemaKey]?.label;
88
+ }
82
89
  v = _propRule(v, subCtx);
83
90
  }
84
91
  else if (v !== undefined) {
85
92
  if (!additionalFields)
86
93
  continue;
87
- if (additionalFields === 'error')
94
+ if (additionalFields === 'error') {
88
95
  context.fail(_propRule, `${ctorName || 'Object'} has no field '${inputKey}' and does not accept additional fields`, v);
96
+ }
89
97
  }
90
- if (v !== undefined)
98
+ if (v !== undefined) {
91
99
  out[propertyOptions[schemaKey]?.as || schemaKey] = v;
100
+ }
92
101
  }
93
102
  if (context.errors.length)
94
103
  return undefined;
@@ -1,11 +1,11 @@
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'.
5
5
  * @validator isRecord
6
6
  */
7
7
  export function isRecord(keyRule, valueRule, options) {
8
- return validator('isRecord', function (input, context, _this) {
8
+ return validator('isRecord', (input, context, _this) => {
9
9
  if (!(input && typeof input === 'object')) {
10
10
  context.fail(_this, `{{label}} must be an object`, input);
11
11
  return;
@@ -1,11 +1,11 @@
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'.
5
5
  * @validator isString
6
6
  */
7
7
  export function isString(options) {
8
- return validator('isString', function (input, context, _this) {
8
+ return validator('isString', (input, context, _this) => {
9
9
  const coerce = options?.coerce || context.coerce;
10
10
  let output = input;
11
11
  if (output != null && typeof output !== 'string' && coerce) {
@@ -24,14 +24,14 @@ export function isString(options) {
24
24
  }, options);
25
25
  }
26
26
  export function stringReplace(searchValue, replacer) {
27
- return validator('stringReplace', function (input) {
27
+ return validator('stringReplace', (input) => {
28
28
  if (input == null)
29
29
  return input;
30
30
  return String(input).replace(searchValue, replacer);
31
31
  });
32
32
  }
33
33
  export function stringSplit(splitter, limit) {
34
- return validator('stringSplit', function (input) {
34
+ return validator('stringSplit', (input) => {
35
35
  if (input == null)
36
36
  return input;
37
37
  return String(input).split(splitter, limit);
@@ -42,7 +42,7 @@ export function stringSplit(splitter, limit) {
42
42
  * @validator trim
43
43
  */
44
44
  export function trim() {
45
- return validator('trim', function (input) {
45
+ return validator('trim', (input) => {
46
46
  if (input == null)
47
47
  return input;
48
48
  return String(input).trim();
@@ -54,7 +54,7 @@ export function trim() {
54
54
  * @validator trimEnd
55
55
  */
56
56
  export const trimEnd = () => trimEndRule;
57
- const trimEndRule = validator('trimEnd', function (input) {
57
+ const trimEndRule = validator('trimEnd', (input) => {
58
58
  if (input == null)
59
59
  return input;
60
60
  return String(input).trimEnd();
@@ -65,7 +65,7 @@ const trimEndRule = validator('trimEnd', function (input) {
65
65
  * @validator trimStart
66
66
  */
67
67
  export const trimStart = () => trimStartRule;
68
- const trimStartRule = validator('trimStart', function (input) {
68
+ const trimStartRule = validator('trimStart', (input) => {
69
69
  if (input == null)
70
70
  return input;
71
71
  return String(input).trimStart();
@@ -1,6 +1,6 @@
1
- import { validator } from '../../core/index.js';
1
+ import { validator, } from '../../core/index.js';
2
2
  export function isTuple(items, options) {
3
- return validator('isTuple', function (input, context, _this) {
3
+ return validator('isTuple', (input, context, _this) => {
4
4
  const coerce = options?.coerce || context.coerce;
5
5
  let output = input;
6
6
  if (output != null && coerce && !Array.isArray(output))
@@ -20,10 +20,13 @@ export function isTuple(items, options) {
20
20
  for (i = 0; i < l && i < nl; i++) {
21
21
  itemRule = items[i];
22
22
  itemContext.scope = output;
23
- itemContext.label = context.label ? context.label + `[${i}]` : undefined;
23
+ itemContext.label = context.label
24
+ ? context.label + `[${i}]`
25
+ : undefined;
24
26
  itemContext.index = i;
25
27
  itemContext.location = location + '[' + i + ']';
26
- itemContext.label = (itemContext.label || itemContext.property || 'Value at ') + `[${i}]`;
28
+ itemContext.label =
29
+ (itemContext.label || itemContext.property || 'Value at ') + `[${i}]`;
27
30
  v = itemRule(output[i], itemContext);
28
31
  out.push(v);
29
32
  }