valgen 5.18.2 → 5.19.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 (208) hide show
  1. package/{types/constants.d.ts → constants.d.ts} +1 -0
  2. package/{esm/constants.js → constants.js} +1 -0
  3. package/{esm/core → core}/context.js +12 -2
  4. package/{esm/core → core}/validation-error.js +1 -1
  5. package/{types/index.d.ts → index.d.ts} +63 -59
  6. package/{esm/index.js → index.js} +10 -1
  7. package/package.json +9 -25
  8. package/{types/rules → rules}/type-rules/is-date.d.ts +6 -6
  9. package/{esm/rules → rules}/type-rules/is-date.js +21 -10
  10. package/CHANGELOG.md +0 -384
  11. package/cjs/constants.js +0 -7
  12. package/cjs/core/constants.js +0 -5
  13. package/cjs/core/context.js +0 -74
  14. package/cjs/core/index.js +0 -9
  15. package/cjs/core/types.js +0 -2
  16. package/cjs/core/utilities.js +0 -30
  17. package/cjs/core/validation-error.js +0 -12
  18. package/cjs/core/validator.js +0 -71
  19. package/cjs/factories.js +0 -4
  20. package/cjs/helpers/object.utils.js +0 -18
  21. package/cjs/helpers/omit-undefined.js +0 -14
  22. package/cjs/helpers/string.utils.js +0 -6
  23. package/cjs/index.js +0 -129
  24. package/cjs/package.json +0 -3
  25. package/cjs/rules/format-rules/is-alpha.js +0 -30
  26. package/cjs/rules/format-rules/is-ascii.js +0 -17
  27. package/cjs/rules/format-rules/is-base64.js +0 -18
  28. package/cjs/rules/format-rules/is-btc-address.js +0 -18
  29. package/cjs/rules/format-rules/is-credit-card.js +0 -19
  30. package/cjs/rules/format-rules/is-decimal.js +0 -19
  31. package/cjs/rules/format-rules/is-ean.js +0 -17
  32. package/cjs/rules/format-rules/is-email.js +0 -66
  33. package/cjs/rules/format-rules/is-eth-address.js +0 -18
  34. package/cjs/rules/format-rules/is-fqdn.js +0 -21
  35. package/cjs/rules/format-rules/is-hash.js +0 -18
  36. package/cjs/rules/format-rules/is-hex-color.js +0 -18
  37. package/cjs/rules/format-rules/is-hex.js +0 -18
  38. package/cjs/rules/format-rules/is-iban.js +0 -29
  39. package/cjs/rules/format-rules/is-ip.js +0 -35
  40. package/cjs/rules/format-rules/is-issn.js +0 -21
  41. package/cjs/rules/format-rules/is-jwt.js +0 -17
  42. package/cjs/rules/format-rules/is-lowercase.js +0 -31
  43. package/cjs/rules/format-rules/is-mac-address.js +0 -24
  44. package/cjs/rules/format-rules/is-mobile-phone.js +0 -22
  45. package/cjs/rules/format-rules/is-object-id.js +0 -28
  46. package/cjs/rules/format-rules/is-passport-number.js +0 -19
  47. package/cjs/rules/format-rules/is-port.js +0 -22
  48. package/cjs/rules/format-rules/is-time.js +0 -51
  49. package/cjs/rules/format-rules/is-url.js +0 -20
  50. package/cjs/rules/format-rules/is-uuid.js +0 -20
  51. package/cjs/rules/format-rules/is-vat-number.js +0 -18
  52. package/cjs/rules/format-rules/matches.js +0 -22
  53. package/cjs/rules/index.js +0 -56
  54. package/cjs/rules/logical-rules/is-defined.js +0 -15
  55. package/cjs/rules/logical-rules/is-empty.js +0 -110
  56. package/cjs/rules/logical-rules/is-equal.js +0 -32
  57. package/cjs/rules/logical-rules/range.js +0 -175
  58. package/cjs/rules/type-rules/is-any.js +0 -10
  59. package/cjs/rules/type-rules/is-array.js +0 -45
  60. package/cjs/rules/type-rules/is-bigint.js +0 -21
  61. package/cjs/rules/type-rules/is-boolean.js +0 -31
  62. package/cjs/rules/type-rules/is-date.js +0 -180
  63. package/cjs/rules/type-rules/is-enum.js +0 -31
  64. package/cjs/rules/type-rules/is-integer.js +0 -30
  65. package/cjs/rules/type-rules/is-null.js +0 -54
  66. package/cjs/rules/type-rules/is-number.js +0 -27
  67. package/cjs/rules/type-rules/is-object.js +0 -113
  68. package/cjs/rules/type-rules/is-record.js +0 -42
  69. package/cjs/rules/type-rules/is-string.js +0 -81
  70. package/cjs/rules/type-rules/is-tuple.js +0 -38
  71. package/cjs/rules/type-rules/is-undefined.js +0 -17
  72. package/cjs/rules/utility-rules/all-of.js +0 -20
  73. package/cjs/rules/utility-rules/exists.js +0 -18
  74. package/cjs/rules/utility-rules/get-length.js +0 -29
  75. package/cjs/rules/utility-rules/one-of.js +0 -59
  76. package/cjs/rules/utility-rules/optional.js +0 -15
  77. package/cjs/rules/utility-rules/pipe.js +0 -28
  78. package/cjs/rules/utility-rules/required.js +0 -19
  79. package/cjs/tsconfig-build-cjs.tsbuildinfo +0 -1
  80. package/esm/package.json +0 -3
  81. package/esm/tsconfig-build-esm.tsbuildinfo +0 -1
  82. package/types/index.d.cts +0 -65
  83. /package/{types/core → core}/constants.d.ts +0 -0
  84. /package/{esm/core → core}/constants.js +0 -0
  85. /package/{types/core → core}/context.d.ts +0 -0
  86. /package/{types/core → core}/index.d.ts +0 -0
  87. /package/{esm/core → core}/index.js +0 -0
  88. /package/{types/core → core}/types.d.ts +0 -0
  89. /package/{esm/core → core}/types.js +0 -0
  90. /package/{types/core → core}/utilities.d.ts +0 -0
  91. /package/{esm/core → core}/utilities.js +0 -0
  92. /package/{types/core → core}/validation-error.d.ts +0 -0
  93. /package/{types/core → core}/validator.d.ts +0 -0
  94. /package/{esm/core → core}/validator.js +0 -0
  95. /package/{types/factories.d.ts → factories.d.ts} +0 -0
  96. /package/{esm/factories.js → factories.js} +0 -0
  97. /package/{types/helpers → helpers}/object.utils.d.ts +0 -0
  98. /package/{esm/helpers → helpers}/object.utils.js +0 -0
  99. /package/{types/helpers → helpers}/omit-undefined.d.ts +0 -0
  100. /package/{esm/helpers → helpers}/omit-undefined.js +0 -0
  101. /package/{types/helpers → helpers}/string.utils.d.ts +0 -0
  102. /package/{esm/helpers → helpers}/string.utils.js +0 -0
  103. /package/{types/rules → rules}/format-rules/is-alpha.d.ts +0 -0
  104. /package/{esm/rules → rules}/format-rules/is-alpha.js +0 -0
  105. /package/{types/rules → rules}/format-rules/is-ascii.d.ts +0 -0
  106. /package/{esm/rules → rules}/format-rules/is-ascii.js +0 -0
  107. /package/{types/rules → rules}/format-rules/is-base64.d.ts +0 -0
  108. /package/{esm/rules → rules}/format-rules/is-base64.js +0 -0
  109. /package/{types/rules → rules}/format-rules/is-btc-address.d.ts +0 -0
  110. /package/{esm/rules → rules}/format-rules/is-btc-address.js +0 -0
  111. /package/{types/rules → rules}/format-rules/is-credit-card.d.ts +0 -0
  112. /package/{esm/rules → rules}/format-rules/is-credit-card.js +0 -0
  113. /package/{types/rules → rules}/format-rules/is-decimal.d.ts +0 -0
  114. /package/{esm/rules → rules}/format-rules/is-decimal.js +0 -0
  115. /package/{types/rules → rules}/format-rules/is-ean.d.ts +0 -0
  116. /package/{esm/rules → rules}/format-rules/is-ean.js +0 -0
  117. /package/{types/rules → rules}/format-rules/is-email.d.ts +0 -0
  118. /package/{esm/rules → rules}/format-rules/is-email.js +0 -0
  119. /package/{types/rules → rules}/format-rules/is-eth-address.d.ts +0 -0
  120. /package/{esm/rules → rules}/format-rules/is-eth-address.js +0 -0
  121. /package/{types/rules → rules}/format-rules/is-fqdn.d.ts +0 -0
  122. /package/{esm/rules → rules}/format-rules/is-fqdn.js +0 -0
  123. /package/{types/rules → rules}/format-rules/is-hash.d.ts +0 -0
  124. /package/{esm/rules → rules}/format-rules/is-hash.js +0 -0
  125. /package/{types/rules → rules}/format-rules/is-hex-color.d.ts +0 -0
  126. /package/{esm/rules → rules}/format-rules/is-hex-color.js +0 -0
  127. /package/{types/rules → rules}/format-rules/is-hex.d.ts +0 -0
  128. /package/{esm/rules → rules}/format-rules/is-hex.js +0 -0
  129. /package/{types/rules → rules}/format-rules/is-iban.d.ts +0 -0
  130. /package/{esm/rules → rules}/format-rules/is-iban.js +0 -0
  131. /package/{types/rules → rules}/format-rules/is-ip.d.ts +0 -0
  132. /package/{esm/rules → rules}/format-rules/is-ip.js +0 -0
  133. /package/{types/rules → rules}/format-rules/is-issn.d.ts +0 -0
  134. /package/{esm/rules → rules}/format-rules/is-issn.js +0 -0
  135. /package/{types/rules → rules}/format-rules/is-jwt.d.ts +0 -0
  136. /package/{esm/rules → rules}/format-rules/is-jwt.js +0 -0
  137. /package/{types/rules → rules}/format-rules/is-lowercase.d.ts +0 -0
  138. /package/{esm/rules → rules}/format-rules/is-lowercase.js +0 -0
  139. /package/{types/rules → rules}/format-rules/is-mac-address.d.ts +0 -0
  140. /package/{esm/rules → rules}/format-rules/is-mac-address.js +0 -0
  141. /package/{types/rules → rules}/format-rules/is-mobile-phone.d.ts +0 -0
  142. /package/{esm/rules → rules}/format-rules/is-mobile-phone.js +0 -0
  143. /package/{types/rules → rules}/format-rules/is-object-id.d.ts +0 -0
  144. /package/{esm/rules → rules}/format-rules/is-object-id.js +0 -0
  145. /package/{types/rules → rules}/format-rules/is-passport-number.d.ts +0 -0
  146. /package/{esm/rules → rules}/format-rules/is-passport-number.js +0 -0
  147. /package/{types/rules → rules}/format-rules/is-port.d.ts +0 -0
  148. /package/{esm/rules → rules}/format-rules/is-port.js +0 -0
  149. /package/{types/rules → rules}/format-rules/is-time.d.ts +0 -0
  150. /package/{esm/rules → rules}/format-rules/is-time.js +0 -0
  151. /package/{types/rules → rules}/format-rules/is-url.d.ts +0 -0
  152. /package/{esm/rules → rules}/format-rules/is-url.js +0 -0
  153. /package/{types/rules → rules}/format-rules/is-uuid.d.ts +0 -0
  154. /package/{esm/rules → rules}/format-rules/is-uuid.js +0 -0
  155. /package/{types/rules → rules}/format-rules/is-vat-number.d.ts +0 -0
  156. /package/{esm/rules → rules}/format-rules/is-vat-number.js +0 -0
  157. /package/{types/rules → rules}/format-rules/matches.d.ts +0 -0
  158. /package/{esm/rules → rules}/format-rules/matches.js +0 -0
  159. /package/{types/rules → rules}/index.d.ts +0 -0
  160. /package/{esm/rules → rules}/index.js +0 -0
  161. /package/{types/rules → rules}/logical-rules/is-defined.d.ts +0 -0
  162. /package/{esm/rules → rules}/logical-rules/is-defined.js +0 -0
  163. /package/{types/rules → rules}/logical-rules/is-empty.d.ts +0 -0
  164. /package/{esm/rules → rules}/logical-rules/is-empty.js +0 -0
  165. /package/{types/rules → rules}/logical-rules/is-equal.d.ts +0 -0
  166. /package/{esm/rules → rules}/logical-rules/is-equal.js +0 -0
  167. /package/{types/rules → rules}/logical-rules/range.d.ts +0 -0
  168. /package/{esm/rules → rules}/logical-rules/range.js +0 -0
  169. /package/{types/rules → rules}/type-rules/is-any.d.ts +0 -0
  170. /package/{esm/rules → rules}/type-rules/is-any.js +0 -0
  171. /package/{types/rules → rules}/type-rules/is-array.d.ts +0 -0
  172. /package/{esm/rules → rules}/type-rules/is-array.js +0 -0
  173. /package/{types/rules → rules}/type-rules/is-bigint.d.ts +0 -0
  174. /package/{esm/rules → rules}/type-rules/is-bigint.js +0 -0
  175. /package/{types/rules → rules}/type-rules/is-boolean.d.ts +0 -0
  176. /package/{esm/rules → rules}/type-rules/is-boolean.js +0 -0
  177. /package/{types/rules → rules}/type-rules/is-enum.d.ts +0 -0
  178. /package/{esm/rules → rules}/type-rules/is-enum.js +0 -0
  179. /package/{types/rules → rules}/type-rules/is-integer.d.ts +0 -0
  180. /package/{esm/rules → rules}/type-rules/is-integer.js +0 -0
  181. /package/{types/rules → rules}/type-rules/is-null.d.ts +0 -0
  182. /package/{esm/rules → rules}/type-rules/is-null.js +0 -0
  183. /package/{types/rules → rules}/type-rules/is-number.d.ts +0 -0
  184. /package/{esm/rules → rules}/type-rules/is-number.js +0 -0
  185. /package/{types/rules → rules}/type-rules/is-object.d.ts +0 -0
  186. /package/{esm/rules → rules}/type-rules/is-object.js +0 -0
  187. /package/{types/rules → rules}/type-rules/is-record.d.ts +0 -0
  188. /package/{esm/rules → rules}/type-rules/is-record.js +0 -0
  189. /package/{types/rules → rules}/type-rules/is-string.d.ts +0 -0
  190. /package/{esm/rules → rules}/type-rules/is-string.js +0 -0
  191. /package/{types/rules → rules}/type-rules/is-tuple.d.ts +0 -0
  192. /package/{esm/rules → rules}/type-rules/is-tuple.js +0 -0
  193. /package/{types/rules → rules}/type-rules/is-undefined.d.ts +0 -0
  194. /package/{esm/rules → rules}/type-rules/is-undefined.js +0 -0
  195. /package/{types/rules → rules}/utility-rules/all-of.d.ts +0 -0
  196. /package/{esm/rules → rules}/utility-rules/all-of.js +0 -0
  197. /package/{types/rules → rules}/utility-rules/exists.d.ts +0 -0
  198. /package/{esm/rules → rules}/utility-rules/exists.js +0 -0
  199. /package/{types/rules → rules}/utility-rules/get-length.d.ts +0 -0
  200. /package/{esm/rules → rules}/utility-rules/get-length.js +0 -0
  201. /package/{types/rules → rules}/utility-rules/one-of.d.ts +0 -0
  202. /package/{esm/rules → rules}/utility-rules/one-of.js +0 -0
  203. /package/{types/rules → rules}/utility-rules/optional.d.ts +0 -0
  204. /package/{esm/rules → rules}/utility-rules/optional.js +0 -0
  205. /package/{types/rules → rules}/utility-rules/pipe.d.ts +0 -0
  206. /package/{esm/rules → rules}/utility-rules/pipe.js +0 -0
  207. /package/{types/rules → rules}/utility-rules/required.d.ts +0 -0
  208. /package/{esm/rules → rules}/utility-rules/required.js +0 -0
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isBoolean = isBoolean;
4
- const index_js_1 = require("../../core/index.js");
5
- const TRUE_PATTERN = /^true|t|1|yes|y$/i;
6
- const FALSE_PATTERN = /^false|f|0|no|n$/i;
7
- /**
8
- * Validates if value is "boolean".
9
- * Converts input value to boolean if coerce option is set to 'true'.
10
- * @validator isBoolean
11
- */
12
- function isBoolean(options) {
13
- return (0, index_js_1.validator)('isBoolean', (input, context, _this) => {
14
- const coerce = options?.coerce ?? context.coerce;
15
- let output = input;
16
- if (output != null && typeof output !== 'boolean' && coerce) {
17
- if (typeof input === 'string') {
18
- if (TRUE_PATTERN.test(input))
19
- return true;
20
- if (FALSE_PATTERN.test(input))
21
- return false;
22
- throw new TypeError(`Invalid boolean string`);
23
- }
24
- if (input === 1 || input === 0)
25
- output = !!input;
26
- }
27
- if (typeof output === 'boolean')
28
- return output;
29
- context.fail(_this, `Value must be a boolean`, input);
30
- }, options);
31
- }
@@ -1,180 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isDate = isDate;
4
- exports.isDateString = isDateString;
5
- const tslib_1 = require("tslib");
6
- const datefns = tslib_1.__importStar(require("date-fns"));
7
- const index_js_1 = require("../../core/index.js");
8
- /**
9
- * Validates if value is a "Date" instance or ISO 8601 formatted date string.
10
- * if a `coerce` option is `true`, converts input value to Date instance
11
- * @validator isDate
12
- */
13
- function isDate(options) {
14
- const trim = options?.trim;
15
- return (0, index_js_1.validator)('isDate', (input, context, _this) => {
16
- const coerce = options?.coerce ?? context.coerce;
17
- let d;
18
- if (input instanceof Date)
19
- d = input;
20
- else if (coerce) {
21
- if (typeof input === 'number')
22
- d = new Date(input);
23
- else if (typeof input === 'string') {
24
- const parsed = coerceDateString(input);
25
- if (parsed) {
26
- d = new Date(parsed.value);
27
- }
28
- }
29
- }
30
- if (datefns.isValid(d)) {
31
- setPrecision(d, trim);
32
- return d;
33
- }
34
- context.fail(_this, `Value is not valid date`, input, {
35
- ...options,
36
- });
37
- }, options);
38
- }
39
- /**
40
- * Validates if value is DFS (date formatted string).
41
- * Converts input value to DFS if the "coerce" option is set to 'true'.
42
- * @validator isDateString
43
- */
44
- function isDateString(options) {
45
- const precisionMin = options?.precisionMin || 'minutes';
46
- const precisionMax = options?.precisionMax || 'tz';
47
- const precisionMaxIdx = PRECISION_INDEX[precisionMax] || 9;
48
- const precisionMinIdx = Math.min(precisionMaxIdx, PRECISION_INDEX[precisionMin] || 6);
49
- return (0, index_js_1.validator)('isDateString', (input, context, _this) => {
50
- const coerce = options?.coerce ?? context.coerce;
51
- const parsed = coerceDateString(input, options?.trim);
52
- if (parsed) {
53
- if (parsed.precision >= precisionMinIdx &&
54
- parsed.precision <= precisionMaxIdx) {
55
- return coerce ? parsed.value : input;
56
- }
57
- }
58
- context.fail(_this, `Value is not valid date string` +
59
- (options?.precisionMin || options?.precisionMax
60
- ? ` with required precision`
61
- : ''), input, {
62
- ...options,
63
- });
64
- }, options);
65
- }
66
- // noinspection RegExpUnnecessaryNonCapturingGroup
67
- 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,6}))?)?((?:[+-](0[0-9]|1[0-2])(?::(\d{2}))?)|Z)?)?$/;
68
- const DATE_PATTERN2 = /^(\d{4,14})(?:\.(\d{1,6}))?(?:(?:([+-])(0[0-9]|1[0-2])(\d{2})?)|Z)?$/;
69
- function coerceDateString(input, precision) {
70
- let dateParts;
71
- let outPrecision = 0;
72
- if (input instanceof Date || typeof input === 'number') {
73
- const d = typeof input === 'number' ? new Date(input) : input;
74
- dateParts = [
75
- String(d.getFullYear()).padStart(4, '0'),
76
- String(d.getMonth() + 1).padStart(2, '0'),
77
- String(d.getDate()).padStart(2, '0'),
78
- String(d.getHours()).padStart(2, '0'),
79
- String(d.getMinutes()).padStart(2, '0'),
80
- String(d.getSeconds()).padStart(2, '0'),
81
- ];
82
- if (d.getMilliseconds() > 0)
83
- dateParts.push(String(d.getMilliseconds()).padStart(3, '0'));
84
- }
85
- else if (typeof input === 'string') {
86
- const d = datefns.parseISO(input);
87
- let m = DATE_PATTERN.exec(input);
88
- if (m && datefns.isValid(d)) {
89
- m.shift();
90
- dateParts = m;
91
- }
92
- else {
93
- m = DATE_PATTERN2.exec(input);
94
- if (m) {
95
- dateParts = [
96
- m[1].substring(0, 4),
97
- m[1].substring(4, 6),
98
- m[1].substring(6, 8),
99
- m[1].substring(8, 10),
100
- m[1].substring(10, 12),
101
- m[1].substring(12, 14),
102
- m[2],
103
- m[3] ? m[3] + m[4] + (m[5] ? ':' + m[5] : '') : '',
104
- ];
105
- }
106
- }
107
- }
108
- if (!dateParts)
109
- return;
110
- outPrecision = dateParts.findIndex(v => !v);
111
- if (outPrecision === -1)
112
- outPrecision = dateParts.length;
113
- const precisionIndex = (precision ? PRECISION_INDEX[precision] : 9) || 9;
114
- let value = dateParts[0] || '0000';
115
- if (precisionIndex > 1)
116
- value += '-' + (dateParts[1] || '01');
117
- if (precisionIndex > 2)
118
- value += '-' + (dateParts[2] || '01');
119
- if (precisionIndex > 3)
120
- value += 'T' + (dateParts[3] || '00');
121
- if (precisionIndex > 4)
122
- value += ':' + (dateParts[4] || '00');
123
- if (precisionIndex > 5)
124
- value += ':' + (dateParts[5] || '00');
125
- if (precisionIndex > 5)
126
- value += dateParts[6] ? '.' + dateParts[6] : '';
127
- if (precisionIndex > 7)
128
- value += dateParts[7] || '';
129
- return {
130
- value,
131
- precision: outPrecision || 8,
132
- };
133
- }
134
- const PRECISION_INDEX = {
135
- year: 1,
136
- yr: 1,
137
- month: 2,
138
- mo: 2,
139
- day: 3,
140
- d: 4,
141
- hours: 4,
142
- hr: 4,
143
- minutes: 5,
144
- min: 5,
145
- seconds: 6,
146
- sec: 6,
147
- milliseconds: 7,
148
- ms: 7,
149
- tz: 8,
150
- };
151
- function setPrecision(d, precision) {
152
- switch (precision) {
153
- case 'year': {
154
- d.setMonth(0, 1);
155
- d.setHours(0, 0, 0, 0);
156
- break;
157
- }
158
- case 'month': {
159
- d.setDate(1);
160
- d.setHours(0, 0, 0, 0);
161
- break;
162
- }
163
- case 'day': {
164
- d.setHours(0, 0, 0, 0);
165
- break;
166
- }
167
- case 'hours': {
168
- d.setMinutes(0, 0, 0);
169
- break;
170
- }
171
- case 'minutes': {
172
- d.setSeconds(0, 0);
173
- break;
174
- }
175
- case 'seconds': {
176
- d.setMilliseconds(0);
177
- break;
178
- }
179
- }
180
- }
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isEnum = isEnum;
4
- const index_js_1 = require("../../core/index.js");
5
- /**
6
- * Validates if given value is one of enum values.
7
- * @validator isEnum
8
- */
9
- function isEnum(values, options) {
10
- const caseInSensitive = !!options?.caseInSensitive;
11
- const enumName = options?.enumName;
12
- // Prepare an object for fast lookup
13
- const valObj = (Array.isArray(values) ? values : [values]).reduce((a, v) => {
14
- if (typeof v === 'string' && caseInSensitive)
15
- a[v.toUpperCase()] = true;
16
- else
17
- a[v] = true;
18
- return a;
19
- }, {});
20
- return (0, index_js_1.validator)('isEnum', (input, context, _this) => {
21
- if (input != null &&
22
- valObj[typeof input === 'string' && caseInSensitive
23
- ? input.toUpperCase()
24
- : input]) {
25
- return input;
26
- }
27
- context.fail(_this, `Value must be one of enumeration member${enumName ? ` (${enumName})` : ''}`, input, {
28
- enum: enumName,
29
- });
30
- }, options);
31
- }
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isInteger = isInteger;
4
- const index_js_1 = require("../../core/index.js");
5
- /**
6
- * Validates if value is "integer".
7
- * Converts input value to integer number if coerce option is set to 'true'.
8
- * @validator isInteger
9
- */
10
- function isInteger(options) {
11
- return (0, index_js_1.validator)('isInteger', (input, context, _this) => {
12
- const coerce = options?.coerce ?? context.coerce;
13
- let output = input;
14
- if (output != null && typeof output !== 'number' && coerce) {
15
- if (typeof input === 'string')
16
- output = parseFloat(input);
17
- else if (typeof input === 'bigint') {
18
- output = Number(input);
19
- if (input === BigInt(output))
20
- input = output;
21
- }
22
- }
23
- if (typeof output === 'number' &&
24
- !isNaN(output) &&
25
- Number.isInteger(output)) {
26
- return output;
27
- }
28
- context.fail(_this, `Value must be a valid integer value`, input);
29
- }, options);
30
- }
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isNull = isNull;
4
- exports.isNotNull = isNotNull;
5
- exports.isNullish = isNullish;
6
- exports.isNotNullish = isNotNullish;
7
- const index_js_1 = require("../../core/index.js");
8
- /**
9
- * Validates if value is "null".
10
- * @validator isNull
11
- */
12
- function isNull(options) {
13
- return (0, index_js_1.validator)('isNull', (input, context, _this) => {
14
- if (input === null)
15
- return input;
16
- context.fail(_this, `Value must be null`, input);
17
- }, options);
18
- }
19
- /**
20
- * Validates if value is not "null".
21
- * @validator isNotNull
22
- */
23
- function isNotNull(options) {
24
- return (0, index_js_1.validator)('isNotNull', (input, context, _this) => {
25
- if (input !== null)
26
- return input;
27
- context.fail(_this, `{{label}} is null`, input);
28
- }, options);
29
- }
30
- /**
31
- * Validates if value is "null" or "undefined".
32
- * @validator isNullish
33
- */
34
- function isNullish(options) {
35
- return (0, index_js_1.validator)('isNullish', (input, context, _this) => {
36
- if (input == null)
37
- return input;
38
- context.fail(_this, `{{label}} is not nullish`, input);
39
- }, options);
40
- }
41
- /**
42
- * Validates if value is not "null" nor "undefined".
43
- * @validator isNotNullish
44
- */
45
- function isNotNullish(options) {
46
- return (0, index_js_1.validator)('isNotNullish', (input, context, _this) => {
47
- if (input != null)
48
- return input;
49
- if (input === null)
50
- context.fail(_this, `{{label}} is null`, input);
51
- else
52
- context.fail(_this, `{{label}} is undefined`, input);
53
- }, options);
54
- }
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isNumber = isNumber;
4
- const index_js_1 = require("../../core/index.js");
5
- /**
6
- * Validates if value is "number".
7
- * Converts input value to number if coerce option is set to 'true'.
8
- * @validator isNumber
9
- */
10
- function isNumber(options) {
11
- return (0, index_js_1.validator)('isNumber', (input, context, _this) => {
12
- const coerce = options?.coerce ?? context.coerce;
13
- let output = input;
14
- if (output != null && typeof output !== 'number' && coerce) {
15
- if (typeof input === 'string')
16
- output = parseFloat(input);
17
- else if (typeof input === 'bigint') {
18
- output = Number(input);
19
- if (input === BigInt(output))
20
- return output;
21
- }
22
- }
23
- if (typeof output === 'number' && !isNaN(output))
24
- return output;
25
- context.fail(_this, `Value must be a number`, input);
26
- }, options);
27
- }
@@ -1,113 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isObject = isObject;
4
- const constants_js_1 = require("../../constants.js");
5
- const index_js_1 = require("../../core/index.js");
6
- /**
7
- * Validates object according to schema
8
- * Converts properties according to schema rules if coerce option is set to 'true'.
9
- * @validator isObject
10
- */
11
- function isObject(schema, options) {
12
- const ctor = options?.ctor;
13
- const ctorName = options?.name || ctor?.name;
14
- const additionalFields = options?.additionalFields ?? !schema;
15
- const caseInSensitive = !!options?.caseInSensitive;
16
- const detectCircular = !!options?.detectCircular;
17
- const propertyRules = {};
18
- const propertyOptions = {};
19
- schema = schema || {};
20
- const schemaKeys = Object.keys(schema);
21
- for (const k of schemaKeys) {
22
- const n = schema[k];
23
- const key = caseInSensitive ? k.toLowerCase() : k;
24
- if (Array.isArray(n)) {
25
- if (!(0, index_js_1.isValidator)(n[0])) {
26
- throw new TypeError(`Invalid tuple definition in validation schema (${k})`);
27
- }
28
- propertyRules[key] = n[0];
29
- propertyOptions[key] = { as: k, ...n[1] };
30
- }
31
- else if ((0, index_js_1.isValidator)(n)) {
32
- propertyRules[key] = n;
33
- propertyOptions[key] = { as: k };
34
- }
35
- else {
36
- throw new TypeError(`Invalid definition in validation schema (${k})`);
37
- }
38
- }
39
- const _rule = (0, index_js_1.validator)('isObject', (input, context, _this) => {
40
- let output = input;
41
- if (ctor && ctor[constants_js_1.preValidation]) {
42
- output = ctor[constants_js_1.preValidation](output, context, _this);
43
- }
44
- const coerce = options?.coerce ?? context.coerce;
45
- if (typeof output === 'string' && coerce)
46
- output = JSON.parse(output);
47
- if (!(output && typeof output === 'object')) {
48
- context.fail(_this, `Value must be an object`, input);
49
- return;
50
- }
51
- const keys = Array.from(new Set([...Object.keys(output), ...schemaKeys]));
52
- const l = keys.length;
53
- let i = 0;
54
- let inputKey;
55
- let schemaKey;
56
- let v;
57
- let _propRule;
58
- const out = {};
59
- if (ctor)
60
- Object.setPrototypeOf(out, ctor.prototype);
61
- if (detectCircular) {
62
- context.circMap = context.circMap || new Map();
63
- if (context.circMap.has(output)) {
64
- return context.circMap.get(output);
65
- }
66
- context.circMap.set(output, out);
67
- }
68
- if (context.root == null)
69
- context.root = context.root || ctorName || '';
70
- const location = context.location || '';
71
- const processedSchemaKeys = {};
72
- // Iterate object keys and perform rules
73
- for (i = 0; i < l; i++) {
74
- inputKey = keys[i];
75
- schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
76
- v = output[inputKey];
77
- _propRule =
78
- propertyRules[schemaKey] ||
79
- ((0, index_js_1.isValidator)(additionalFields) ? additionalFields : undefined);
80
- if (_propRule) {
81
- if (processedSchemaKeys[schemaKey])
82
- continue;
83
- processedSchemaKeys[schemaKey] = true;
84
- const subCtx = context.extend();
85
- subCtx.scope = output;
86
- subCtx.context = ctorName;
87
- subCtx.location = location + (location ? '.' : '') + schemaKey;
88
- subCtx.property = schemaKey;
89
- if (propertyOptions[schemaKey]?.label) {
90
- subCtx.label = propertyOptions[schemaKey]?.label;
91
- }
92
- v = _propRule(v, subCtx);
93
- }
94
- else if (v !== undefined) {
95
- if (!additionalFields)
96
- continue;
97
- if (additionalFields === 'error') {
98
- context.fail(_propRule, `${ctorName || 'Object'} has no field '${inputKey}' and does not accept additional fields`, v);
99
- }
100
- }
101
- if (v !== undefined) {
102
- out[propertyOptions[schemaKey]?.as || schemaKey] = v;
103
- }
104
- }
105
- if (ctor && ctor[constants_js_1.postValidation]) {
106
- ctor[constants_js_1.postValidation](out, context, _this);
107
- return out;
108
- }
109
- return out;
110
- }, options);
111
- _rule.schema = schema;
112
- return _rule;
113
- }
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isRecord = isRecord;
4
- const index_js_1 = require("../../core/index.js");
5
- /**
6
- * Validates record object according to given "key" and "value" rules
7
- * Converts properties according to rules if coerce option is set to 'true'.
8
- * @validator isRecord
9
- */
10
- function isRecord(keyRule, valueRule, options) {
11
- return (0, index_js_1.validator)('isRecord', (input, context, _this) => {
12
- if (!(input && typeof input === 'object')) {
13
- context.fail(_this, `Value must be an object`, input);
14
- return;
15
- }
16
- const keyContext = context.extend();
17
- const valueContext = context.extend();
18
- const location = context.location || '';
19
- const keys = Object.keys(input);
20
- const l = keys.length;
21
- let i;
22
- let k;
23
- let v;
24
- const out = {};
25
- for (i = 0; i < l; i++) {
26
- k = keys[i];
27
- v = input[k];
28
- // Validate key
29
- k = keyRule(k, {
30
- onFail(issue) {
31
- return `${k} is not a valid key. ` + issue.message;
32
- },
33
- }, keyContext);
34
- // Validate value
35
- valueContext.property = k;
36
- valueContext.location = location + (location ? '.' : '') + k;
37
- v = valueRule(v, valueContext);
38
- out[k] = v;
39
- }
40
- return context.errors.length ? undefined : out;
41
- }, options);
42
- }
@@ -1,81 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.trimStart = exports.trimEnd = void 0;
4
- exports.isString = isString;
5
- exports.stringReplace = stringReplace;
6
- exports.stringSplit = stringSplit;
7
- exports.trim = trim;
8
- const index_js_1 = require("../../core/index.js");
9
- /**
10
- * Validates if value is "string".
11
- * Converts input value to string if coerce option is set to 'true'.
12
- * @validator isString
13
- */
14
- function isString(options) {
15
- return (0, index_js_1.validator)('isString', (input, context, _this) => {
16
- const coerce = options?.coerce ?? context.coerce;
17
- let output = input;
18
- if (output != null && typeof output !== 'string' && coerce) {
19
- if (typeof output === 'object') {
20
- if (typeof output.toJSON === 'function')
21
- output = output.toJSON();
22
- else
23
- output = JSON.stringify(output);
24
- }
25
- else
26
- output = String(output);
27
- }
28
- if (typeof output === 'string')
29
- return output;
30
- context.fail(_this, `Value must be a string`, input);
31
- }, options);
32
- }
33
- function stringReplace(searchValue, replacer) {
34
- return (0, index_js_1.validator)('stringReplace', (input) => {
35
- if (input == null)
36
- return input;
37
- return String(input).replace(searchValue, replacer);
38
- });
39
- }
40
- function stringSplit(splitter, limit) {
41
- return (0, index_js_1.validator)('stringSplit', (input) => {
42
- if (input == null)
43
- return input;
44
- return String(input).split(splitter, limit);
45
- });
46
- }
47
- /**
48
- * Removes whitespace from both ends of a string
49
- * @validator trim
50
- */
51
- function trim() {
52
- return (0, index_js_1.validator)('trim', (input) => {
53
- if (input == null)
54
- return input;
55
- return String(input).trim();
56
- });
57
- }
58
- // *************************************************************
59
- /**
60
- * Removes whitespace from the end of a string
61
- * @validator trimEnd
62
- */
63
- const trimEnd = () => trimEndRule;
64
- exports.trimEnd = trimEnd;
65
- const trimEndRule = (0, index_js_1.validator)('trimEnd', (input) => {
66
- if (input == null)
67
- return input;
68
- return String(input).trimEnd();
69
- });
70
- // *************************************************************
71
- /**
72
- * Removes whitespace from the beginning of a string
73
- * @validator trimStart
74
- */
75
- const trimStart = () => trimStartRule;
76
- exports.trimStart = trimStart;
77
- const trimStartRule = (0, index_js_1.validator)('trimStart', (input) => {
78
- if (input == null)
79
- return input;
80
- return String(input).trimStart();
81
- });
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isTuple = isTuple;
4
- const index_js_1 = require("../../core/index.js");
5
- function isTuple(items, options) {
6
- return (0, index_js_1.validator)('isTuple', (input, context, _this) => {
7
- const coerce = options?.coerce ?? context.coerce;
8
- let output = input;
9
- if (output != null && coerce && !Array.isArray(output))
10
- output = [output];
11
- if (!Array.isArray(input)) {
12
- context.fail(_this, `Value must be a tuple`, input);
13
- return;
14
- }
15
- const location = context.location || '';
16
- let itemRule;
17
- let i;
18
- let v;
19
- const l = output.length;
20
- const out = [];
21
- const nl = items.length;
22
- const itemContext = context.extend();
23
- for (i = 0; i < l && i < nl; i++) {
24
- itemRule = items[i];
25
- itemContext.scope = output;
26
- itemContext.label = context.label
27
- ? context.label + `[${i}]`
28
- : undefined;
29
- itemContext.index = i;
30
- itemContext.location = location + '[' + i + ']';
31
- itemContext.label =
32
- (itemContext.label || itemContext.property || 'Value at ') + `[${i}]`;
33
- v = itemRule(output[i], itemContext);
34
- out.push(v);
35
- }
36
- return context.errors.length ? undefined : out;
37
- }, options);
38
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isUndefined = isUndefined;
4
- const index_js_1 = require("../../core/index.js");
5
- /**
6
- * Validates if value is "undefined"
7
- * @validator isUndefined
8
- */
9
- function isUndefined(options) {
10
- return (0, index_js_1.validator)('isUndefined', (input, context, _this) => {
11
- if (options?.coerce ?? context.coerce)
12
- return undefined;
13
- if (input === undefined)
14
- return;
15
- context.fail(_this, `Value must be undefined`, input);
16
- }, options);
17
- }
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.allOf = allOf;
4
- const index_js_1 = require("../../core/index.js");
5
- /**
6
- * Test given value against to all codecs and returns original input
7
- * @validator allOf
8
- */
9
- function allOf(rules) {
10
- return (0, index_js_1.validator)('allOf', (input, context) => {
11
- let i;
12
- let c;
13
- const l = rules.length;
14
- for (i = 0; i < l; i++) {
15
- c = rules[i];
16
- c(input, context);
17
- }
18
- return input;
19
- });
20
- }