@twin.org/core 0.0.2-next.9 → 0.0.3-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/dist/es/encoding/base32.js +75 -0
  2. package/dist/es/encoding/base32.js.map +1 -0
  3. package/dist/es/encoding/base58.js +124 -0
  4. package/dist/es/encoding/base58.js.map +1 -0
  5. package/dist/es/encoding/base64.js +231 -0
  6. package/dist/es/encoding/base64.js.map +1 -0
  7. package/dist/es/encoding/base64Url.js +42 -0
  8. package/dist/es/encoding/base64Url.js.map +1 -0
  9. package/dist/es/errors/alreadyExistsError.js +22 -0
  10. package/dist/es/errors/alreadyExistsError.js.map +1 -0
  11. package/dist/es/errors/baseError.js +257 -0
  12. package/dist/es/errors/baseError.js.map +1 -0
  13. package/dist/es/errors/conflictError.js +23 -0
  14. package/dist/es/errors/conflictError.js.map +1 -0
  15. package/dist/es/errors/generalError.js +21 -0
  16. package/dist/es/errors/generalError.js.map +1 -0
  17. package/dist/es/errors/guardError.js +27 -0
  18. package/dist/es/errors/guardError.js.map +1 -0
  19. package/dist/es/errors/notFoundError.js +22 -0
  20. package/dist/es/errors/notFoundError.js.map +1 -0
  21. package/dist/es/errors/notImplementedError.js +24 -0
  22. package/dist/es/errors/notImplementedError.js.map +1 -0
  23. package/dist/es/errors/notSupportedError.js +21 -0
  24. package/dist/es/errors/notSupportedError.js.map +1 -0
  25. package/dist/es/errors/unauthorizedError.js +21 -0
  26. package/dist/es/errors/unauthorizedError.js.map +1 -0
  27. package/dist/es/errors/unprocessableError.js +21 -0
  28. package/dist/es/errors/unprocessableError.js.map +1 -0
  29. package/dist/es/errors/validationError.js +23 -0
  30. package/dist/es/errors/validationError.js.map +1 -0
  31. package/dist/es/factories/componentFactory.js +9 -0
  32. package/dist/es/factories/componentFactory.js.map +1 -0
  33. package/dist/es/factories/factory.js +256 -0
  34. package/dist/es/factories/factory.js.map +1 -0
  35. package/dist/es/helpers/arrayHelper.js +44 -0
  36. package/dist/es/helpers/arrayHelper.js.map +1 -0
  37. package/dist/es/helpers/envHelper.js +39 -0
  38. package/dist/es/helpers/envHelper.js.map +1 -0
  39. package/dist/es/helpers/errorHelper.js +100 -0
  40. package/dist/es/helpers/errorHelper.js.map +1 -0
  41. package/dist/es/helpers/filenameHelper.js +32 -0
  42. package/dist/es/helpers/filenameHelper.js.map +1 -0
  43. package/dist/es/helpers/hexHelper.js +45 -0
  44. package/dist/es/helpers/hexHelper.js.map +1 -0
  45. package/dist/es/helpers/jsonHelper.js +163 -0
  46. package/dist/es/helpers/jsonHelper.js.map +1 -0
  47. package/dist/es/helpers/numberHelper.js +30 -0
  48. package/dist/es/helpers/numberHelper.js.map +1 -0
  49. package/dist/es/helpers/objectHelper.js +333 -0
  50. package/dist/es/helpers/objectHelper.js.map +1 -0
  51. package/dist/es/helpers/randomHelper.js +18 -0
  52. package/dist/es/helpers/randomHelper.js.map +1 -0
  53. package/dist/es/helpers/stringHelper.js +241 -0
  54. package/dist/es/helpers/stringHelper.js.map +1 -0
  55. package/dist/es/helpers/uint8ArrayHelper.js +26 -0
  56. package/dist/es/helpers/uint8ArrayHelper.js.map +1 -0
  57. package/dist/es/index.js +57 -0
  58. package/dist/es/index.js.map +1 -0
  59. package/dist/es/models/IComponent.js +4 -0
  60. package/dist/es/models/IComponent.js.map +1 -0
  61. package/dist/es/models/IError.js +4 -0
  62. package/dist/es/models/IError.js.map +1 -0
  63. package/dist/es/models/II18nShared.js +2 -0
  64. package/dist/es/models/II18nShared.js.map +1 -0
  65. package/dist/es/models/IKeyValue.js +2 -0
  66. package/dist/es/models/IKeyValue.js.map +1 -0
  67. package/dist/es/models/ILabelledValue.js +2 -0
  68. package/dist/es/models/ILabelledValue.js.map +1 -0
  69. package/dist/es/models/ILocale.js +2 -0
  70. package/dist/es/models/ILocale.js.map +1 -0
  71. package/dist/es/models/ILocaleDictionary.js +4 -0
  72. package/dist/es/models/ILocaleDictionary.js.map +1 -0
  73. package/dist/es/models/ILocalesIndex.js +2 -0
  74. package/dist/es/models/ILocalesIndex.js.map +1 -0
  75. package/dist/es/models/IPatchOperation.js +4 -0
  76. package/dist/es/models/IPatchOperation.js.map +1 -0
  77. package/dist/es/models/IUrlParts.js +4 -0
  78. package/dist/es/models/IUrlParts.js.map +1 -0
  79. package/dist/es/models/IValidationFailure.js +2 -0
  80. package/dist/es/models/IValidationFailure.js.map +1 -0
  81. package/dist/es/models/coerceType.js +49 -0
  82. package/dist/es/models/coerceType.js.map +1 -0
  83. package/dist/es/models/compressionType.js +17 -0
  84. package/dist/es/models/compressionType.js.map +1 -0
  85. package/dist/es/models/objectOrArray.js +2 -0
  86. package/dist/es/models/objectOrArray.js.map +1 -0
  87. package/dist/es/types/bitString.js +98 -0
  88. package/dist/es/types/bitString.js.map +1 -0
  89. package/dist/es/types/url.js +144 -0
  90. package/dist/es/types/url.js.map +1 -0
  91. package/dist/es/types/urn.js +212 -0
  92. package/dist/es/types/urn.js.map +1 -0
  93. package/dist/es/utils/asyncCache.js +179 -0
  94. package/dist/es/utils/asyncCache.js.map +1 -0
  95. package/dist/es/utils/coerce.js +269 -0
  96. package/dist/es/utils/coerce.js.map +1 -0
  97. package/dist/es/utils/compression.js +49 -0
  98. package/dist/es/utils/compression.js.map +1 -0
  99. package/dist/es/utils/converter.js +256 -0
  100. package/dist/es/utils/converter.js.map +1 -0
  101. package/dist/es/utils/guards.js +369 -0
  102. package/dist/es/utils/guards.js.map +1 -0
  103. package/dist/es/utils/i18n.js +243 -0
  104. package/dist/es/utils/i18n.js.map +1 -0
  105. package/dist/es/utils/is.js +351 -0
  106. package/dist/es/utils/is.js.map +1 -0
  107. package/dist/es/utils/sharedStore.js +48 -0
  108. package/dist/es/utils/sharedStore.js.map +1 -0
  109. package/dist/es/utils/validation.js +774 -0
  110. package/dist/es/utils/validation.js.map +1 -0
  111. package/dist/types/encoding/base32.d.ts +4 -0
  112. package/dist/types/encoding/base58.d.ts +4 -0
  113. package/dist/types/encoding/base64.d.ts +4 -0
  114. package/dist/types/encoding/base64Url.d.ts +4 -0
  115. package/dist/types/errors/alreadyExistsError.d.ts +5 -2
  116. package/dist/types/errors/baseError.d.ts +1 -1
  117. package/dist/types/errors/conflictError.d.ts +5 -2
  118. package/dist/types/errors/generalError.d.ts +1 -1
  119. package/dist/types/errors/guardError.d.ts +1 -1
  120. package/dist/types/errors/notFoundError.d.ts +5 -2
  121. package/dist/types/errors/notImplementedError.d.ts +6 -2
  122. package/dist/types/errors/notSupportedError.d.ts +5 -2
  123. package/dist/types/errors/unauthorizedError.d.ts +5 -2
  124. package/dist/types/errors/unprocessableError.d.ts +1 -1
  125. package/dist/types/errors/validationError.d.ts +2 -2
  126. package/dist/types/factories/componentFactory.d.ts +2 -2
  127. package/dist/types/factories/factory.d.ts +4 -0
  128. package/dist/types/helpers/arrayHelper.d.ts +1 -1
  129. package/dist/types/helpers/errorHelper.d.ts +1 -1
  130. package/dist/types/helpers/jsonHelper.d.ts +5 -1
  131. package/dist/types/helpers/numberHelper.d.ts +17 -0
  132. package/dist/types/helpers/objectHelper.d.ts +14 -0
  133. package/dist/types/helpers/stringHelper.d.ts +6 -6
  134. package/dist/types/index.d.ts +54 -53
  135. package/dist/types/models/IComponent.d.ts +6 -7
  136. package/dist/types/models/ILocalesIndex.d.ts +1 -1
  137. package/dist/types/models/IValidationFailure.d.ts +0 -4
  138. package/dist/types/types/bitString.d.ts +4 -0
  139. package/dist/types/types/url.d.ts +8 -3
  140. package/dist/types/types/urn.d.ts +7 -2
  141. package/dist/types/utils/coerce.d.ts +1 -1
  142. package/dist/types/utils/compression.d.ts +5 -1
  143. package/dist/types/utils/guards.d.ts +2 -3
  144. package/dist/types/utils/i18n.d.ts +16 -1
  145. package/dist/types/utils/is.d.ts +1 -1
  146. package/dist/types/utils/validation.d.ts +1 -1
  147. package/docs/changelog.md +283 -0
  148. package/docs/reference/classes/AlreadyExistsError.md +8 -4
  149. package/docs/reference/classes/AsyncCache.md +5 -5
  150. package/docs/reference/classes/Base32.md +8 -0
  151. package/docs/reference/classes/Base58.md +8 -0
  152. package/docs/reference/classes/Base64.md +8 -0
  153. package/docs/reference/classes/Base64Url.md +8 -0
  154. package/docs/reference/classes/BaseError.md +3 -3
  155. package/docs/reference/classes/BitString.md +8 -0
  156. package/docs/reference/classes/Coerce.md +20 -20
  157. package/docs/reference/classes/Compression.md +8 -0
  158. package/docs/reference/classes/ConflictError.md +8 -4
  159. package/docs/reference/classes/ErrorHelper.md +2 -2
  160. package/docs/reference/classes/Factory.md +11 -3
  161. package/docs/reference/classes/GeneralError.md +3 -3
  162. package/docs/reference/classes/GuardError.md +3 -3
  163. package/docs/reference/classes/Guards.md +8 -4
  164. package/docs/reference/classes/I18n.md +54 -2
  165. package/docs/reference/classes/Is.md +10 -4
  166. package/docs/reference/classes/JsonHelper.md +8 -0
  167. package/docs/reference/classes/NotFoundError.md +8 -4
  168. package/docs/reference/classes/NotImplementedError.md +14 -4
  169. package/docs/reference/classes/NotSupportedError.md +8 -4
  170. package/docs/reference/classes/NumberHelper.md +55 -0
  171. package/docs/reference/classes/ObjectHelper.md +58 -8
  172. package/docs/reference/classes/SharedStore.md +2 -2
  173. package/docs/reference/classes/StringHelper.md +8 -8
  174. package/docs/reference/classes/UnauthorizedError.md +8 -4
  175. package/docs/reference/classes/UnprocessableError.md +3 -3
  176. package/docs/reference/classes/Url.md +17 -3
  177. package/docs/reference/classes/Urn.md +19 -5
  178. package/docs/reference/classes/Validation.md +2 -2
  179. package/docs/reference/classes/ValidationError.md +3 -3
  180. package/docs/reference/index.md +1 -0
  181. package/docs/reference/interfaces/IComponent.md +19 -25
  182. package/docs/reference/interfaces/IValidationFailure.md +0 -8
  183. package/locales/.validate-ignore +1 -0
  184. package/locales/en.json +4 -5
  185. package/package.json +22 -10
  186. package/dist/cjs/index.cjs +0 -5175
  187. package/dist/esm/index.mjs +0 -5133
@@ -0,0 +1,774 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { Is } from "./is.js";
4
+ import { ValidationError } from "../errors/validationError.js";
5
+ /**
6
+ * Class to handle validation operations.
7
+ */
8
+ export class Validation {
9
+ /**
10
+ * Is the property null or undefined.
11
+ * @param property The name of the property.
12
+ * @param value The value to test.
13
+ * @param failures The list of failures to add to.
14
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
15
+ * @returns True if the value is a empty.
16
+ */
17
+ static empty(property, value, failures, fieldNameResource) {
18
+ const is = Is.empty(value);
19
+ if (!is) {
20
+ failures.push({
21
+ property,
22
+ reason: "validation.beEmpty",
23
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
24
+ });
25
+ }
26
+ return is;
27
+ }
28
+ /**
29
+ * Is the property is not null or undefined.
30
+ * @param property The name of the property.
31
+ * @param value The value to test.
32
+ * @param failures The list of failures to add to.
33
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
34
+ * @returns True if the value is a not empty.
35
+ */
36
+ static notEmpty(property, value, failures, fieldNameResource) {
37
+ const is = Is.notEmpty(value);
38
+ if (!is) {
39
+ failures.push({
40
+ property,
41
+ reason: "validation.beNotEmpty",
42
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
43
+ });
44
+ }
45
+ return is;
46
+ }
47
+ /**
48
+ * Is the property a string.
49
+ * @param property The name of the property.
50
+ * @param value The value to test.
51
+ * @param failures The list of failures to add to.
52
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
53
+ * @param options Additional options for the validation.
54
+ * @param options.minLength The minimum length of the string.
55
+ * @param options.maxLength The maximum length of the string.
56
+ * @param options.format Specific format to check.
57
+ * @returns True if the value is a valid string.
58
+ */
59
+ static string(property, value, failures, fieldNameResource, options) {
60
+ const is = Is.string(value);
61
+ if (!is) {
62
+ failures.push({
63
+ property,
64
+ reason: "validation.beText",
65
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
66
+ });
67
+ }
68
+ else {
69
+ const minLength = options?.minLength;
70
+ const maxLength = options?.maxLength;
71
+ const minLimitDefined = Is.integer(minLength);
72
+ const maxLimitDefined = Is.integer(maxLength);
73
+ const belowMin = minLimitDefined && value.length < minLength;
74
+ const aboveMax = maxLimitDefined && value.length > maxLength;
75
+ if (options?.format === "base58" && !Is.stringBase58(value)) {
76
+ failures.push({
77
+ property,
78
+ reason: "validation.beTextBase58",
79
+ properties: {
80
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
81
+ value
82
+ }
83
+ });
84
+ }
85
+ else if (options?.format === "base64" && !Is.stringBase64(value)) {
86
+ failures.push({
87
+ property,
88
+ reason: "validation.beTextBase64",
89
+ properties: {
90
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
91
+ value
92
+ }
93
+ });
94
+ }
95
+ else if (options?.format === "hex" && !Is.stringHex(value)) {
96
+ failures.push({
97
+ property,
98
+ reason: "validation.beTextHex",
99
+ properties: {
100
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
101
+ value
102
+ }
103
+ });
104
+ }
105
+ else if (Is.regexp(options?.format) && !options.format.test(value)) {
106
+ failures.push({
107
+ property,
108
+ reason: "validation.beTextRegExp",
109
+ properties: {
110
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
111
+ value,
112
+ format: options?.format
113
+ }
114
+ });
115
+ }
116
+ if (minLimitDefined && maxLimitDefined && (belowMin || aboveMax)) {
117
+ failures.push({
118
+ property,
119
+ reason: "validation.beTextMinMax",
120
+ properties: {
121
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
122
+ value,
123
+ minLength,
124
+ maxLength
125
+ }
126
+ });
127
+ }
128
+ else if (minLimitDefined && belowMin) {
129
+ failures.push({
130
+ property,
131
+ reason: "validation.beTextMin",
132
+ properties: {
133
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
134
+ value,
135
+ minLength
136
+ }
137
+ });
138
+ }
139
+ else if (maxLimitDefined && aboveMax) {
140
+ failures.push({
141
+ property,
142
+ reason: "validation.beTextMax",
143
+ properties: {
144
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
145
+ value,
146
+ maxLength
147
+ }
148
+ });
149
+ }
150
+ }
151
+ return failures.length === 0;
152
+ }
153
+ /**
154
+ * Is the property a string with a value.
155
+ * @param property The name of the property.
156
+ * @param value The value to test.
157
+ * @param failures The list of failures to add to.
158
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
159
+ * @param options Additional options for the validation.
160
+ * @param options.minLength The minimum length of the string.
161
+ * @param options.maxLength The maximum length of the string.
162
+ * @returns True if the value is a valid string.
163
+ */
164
+ static stringValue(property, value, failures, fieldNameResource, options) {
165
+ const is = Is.stringValue(value);
166
+ if (!is) {
167
+ failures.push({
168
+ property,
169
+ reason: "validation.beTextValue",
170
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
171
+ });
172
+ }
173
+ else {
174
+ const minLength = options?.minLength;
175
+ const maxLength = options?.maxLength;
176
+ const minLimitDefined = Is.integer(minLength);
177
+ const maxLimitDefined = Is.integer(maxLength);
178
+ const belowMin = minLimitDefined && value.length < minLength;
179
+ const aboveMax = maxLimitDefined && value.length > maxLength;
180
+ if (minLimitDefined && maxLimitDefined && (belowMin || aboveMax)) {
181
+ failures.push({
182
+ property,
183
+ reason: "validation.beTextMinMax",
184
+ properties: {
185
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
186
+ value,
187
+ minLength,
188
+ maxLength
189
+ }
190
+ });
191
+ }
192
+ else if (minLimitDefined && belowMin) {
193
+ failures.push({
194
+ property,
195
+ reason: "validation.beTextMin",
196
+ properties: {
197
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
198
+ value,
199
+ minLength
200
+ }
201
+ });
202
+ }
203
+ else if (maxLimitDefined && aboveMax) {
204
+ failures.push({
205
+ property,
206
+ reason: "validation.beTextMax",
207
+ properties: {
208
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
209
+ value,
210
+ maxLength
211
+ }
212
+ });
213
+ }
214
+ }
215
+ return failures.length === 0;
216
+ }
217
+ /**
218
+ * Is the property a number.
219
+ * @param property The name of the property.
220
+ * @param value The value to test.
221
+ * @param failures The list of failures to add to.
222
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
223
+ * @param options Additional options for the validation.
224
+ * @param options.minValue The minimum value of the number.
225
+ * @param options.maxValue The maximum value of the number.
226
+ * @returns True if the value is a valid number.
227
+ */
228
+ static number(property, value, failures, fieldNameResource, options) {
229
+ const is = Is.number(value);
230
+ if (!is) {
231
+ failures.push({
232
+ property,
233
+ reason: "validation.beNumber",
234
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
235
+ });
236
+ }
237
+ else {
238
+ const minValue = options?.minValue;
239
+ const maxValue = options?.maxValue;
240
+ const minLimitDefined = Is.number(minValue);
241
+ const maxLimitDefined = Is.number(maxValue);
242
+ const belowMin = minLimitDefined && value < minValue;
243
+ const aboveMax = maxLimitDefined && value > maxValue;
244
+ if (minLimitDefined && maxLimitDefined && (belowMin || aboveMax)) {
245
+ failures.push({
246
+ property,
247
+ reason: "validation.beNumberMinMax",
248
+ properties: {
249
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
250
+ value,
251
+ minValue,
252
+ maxValue
253
+ }
254
+ });
255
+ }
256
+ else if (minLimitDefined && belowMin) {
257
+ failures.push({
258
+ property,
259
+ reason: "validation.beNumberMin",
260
+ properties: {
261
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
262
+ value,
263
+ minValue
264
+ }
265
+ });
266
+ }
267
+ else if (maxLimitDefined && aboveMax) {
268
+ failures.push({
269
+ property,
270
+ reason: "validation.beNumberMax",
271
+ properties: {
272
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
273
+ value,
274
+ maxValue
275
+ }
276
+ });
277
+ }
278
+ }
279
+ return failures.length === 0;
280
+ }
281
+ /**
282
+ * Is the property an integer.
283
+ * @param property The name of the property.
284
+ * @param value The value to test.
285
+ * @param failures The list of failures to add to.
286
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
287
+ * @param options Additional options for the validation.
288
+ * @param options.minValue The minimum value of the integer.
289
+ * @param options.maxValue The maximum value of the integer.
290
+ * @returns True if the value is a valid integer.
291
+ */
292
+ static integer(property, value, failures, fieldNameResource, options) {
293
+ const is = Is.integer(value);
294
+ if (!is) {
295
+ failures.push({
296
+ property,
297
+ reason: "validation.beWholeNumber",
298
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
299
+ });
300
+ }
301
+ else {
302
+ const minValue = options?.minValue;
303
+ const maxValue = options?.maxValue;
304
+ const minLimitDefined = Is.integer(minValue);
305
+ const maxLimitDefined = Is.integer(maxValue);
306
+ const belowMin = minLimitDefined && value < minValue;
307
+ const aboveMax = maxLimitDefined && value > maxValue;
308
+ if (minLimitDefined && maxLimitDefined && (belowMin || aboveMax)) {
309
+ failures.push({
310
+ property,
311
+ reason: "validation.beWholeNumberMinMax",
312
+ properties: {
313
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
314
+ value,
315
+ minValue,
316
+ maxValue
317
+ }
318
+ });
319
+ }
320
+ else if (minLimitDefined && belowMin) {
321
+ failures.push({
322
+ property,
323
+ reason: "validation.beWholeNumberMin",
324
+ properties: {
325
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
326
+ value,
327
+ minValue
328
+ }
329
+ });
330
+ }
331
+ else if (maxLimitDefined && aboveMax) {
332
+ failures.push({
333
+ property,
334
+ reason: "validation.beWholeNumberMax",
335
+ properties: {
336
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
337
+ value,
338
+ maxValue
339
+ }
340
+ });
341
+ }
342
+ }
343
+ return failures.length === 0;
344
+ }
345
+ /**
346
+ * Is the property a bigint.
347
+ * @param property The name of the property.
348
+ * @param value The value to test.
349
+ * @param failures The list of failures to add to.
350
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
351
+ * @param options Additional options for the validation.
352
+ * @param options.minValue The minimum value of the bigint.
353
+ * @param options.maxValue The maximum value of the bigint.
354
+ * @returns True if the value is a valid bigint.
355
+ */
356
+ static bigint(property, value, failures, fieldNameResource, options) {
357
+ const is = Is.bigint(value);
358
+ if (!is) {
359
+ failures.push({
360
+ property,
361
+ reason: "validation.beBigInteger",
362
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
363
+ });
364
+ }
365
+ else {
366
+ const minValue = options?.minValue;
367
+ const maxValue = options?.maxValue;
368
+ const minLimitDefined = Is.bigint(minValue);
369
+ const maxLimitDefined = Is.bigint(maxValue);
370
+ const belowMin = minLimitDefined && value < minValue;
371
+ const aboveMax = maxLimitDefined && value > maxValue;
372
+ if (minLimitDefined && maxLimitDefined && (belowMin || aboveMax)) {
373
+ failures.push({
374
+ property,
375
+ reason: "validation.beBigIntegerMinMax",
376
+ properties: {
377
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
378
+ value,
379
+ minValue,
380
+ maxValue
381
+ }
382
+ });
383
+ }
384
+ else if (minLimitDefined && belowMin) {
385
+ failures.push({
386
+ property,
387
+ reason: "validation.beBigIntegerMin",
388
+ properties: {
389
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
390
+ value,
391
+ minValue
392
+ }
393
+ });
394
+ }
395
+ else if (maxLimitDefined && aboveMax) {
396
+ failures.push({
397
+ property,
398
+ reason: "validation.beBigIntegerMax",
399
+ properties: {
400
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
401
+ value,
402
+ maxValue
403
+ }
404
+ });
405
+ }
406
+ }
407
+ return failures.length === 0;
408
+ }
409
+ /**
410
+ * Is the property a boolean.
411
+ * @param property The name of the property.
412
+ * @param value The value to test.
413
+ * @param failures The list of failures to add to.
414
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
415
+ * @returns True if the value is a boolean.
416
+ */
417
+ static boolean(property, value, failures, fieldNameResource) {
418
+ const is = Is.boolean(value);
419
+ if (!is) {
420
+ failures.push({
421
+ property,
422
+ reason: "validation.beBoolean",
423
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
424
+ });
425
+ }
426
+ return is;
427
+ }
428
+ /**
429
+ * Is the property a date.
430
+ * @param property The name of the property.
431
+ * @param value The value to test.
432
+ * @param failures The list of failures to add to.
433
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
434
+ * @returns True if the value is a date.
435
+ */
436
+ static date(property, value, failures, fieldNameResource) {
437
+ if (Is.dateEmpty(value)) {
438
+ failures.push({
439
+ property,
440
+ reason: "validation.beDate",
441
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
442
+ });
443
+ return false;
444
+ }
445
+ const is = Is.date(value);
446
+ if (!is) {
447
+ failures.push({
448
+ property,
449
+ reason: "validation.beDate",
450
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
451
+ });
452
+ }
453
+ return is;
454
+ }
455
+ /**
456
+ * Is the property a date in ISO 8601 format.
457
+ * @param property The name of the property.
458
+ * @param value The value to test.
459
+ * @param failures The list of failures to add to.
460
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
461
+ * @returns True if the value is a date.
462
+ */
463
+ static dateString(property, value, failures, fieldNameResource) {
464
+ if (!Is.stringValue(value)) {
465
+ failures.push({
466
+ property,
467
+ reason: "validation.beDate",
468
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
469
+ });
470
+ return false;
471
+ }
472
+ const is = Is.dateString(value);
473
+ if (!is) {
474
+ failures.push({
475
+ property,
476
+ reason: "validation.beDate",
477
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
478
+ });
479
+ }
480
+ return is;
481
+ }
482
+ /**
483
+ * Is the property a date/time in ISO 8601 format.
484
+ * @param property The name of the property.
485
+ * @param value The value to test.
486
+ * @param failures The list of failures to add to.
487
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
488
+ * @returns True if the value is a date/time.
489
+ */
490
+ static dateTimeString(property, value, failures, fieldNameResource) {
491
+ if (!Is.stringValue(value)) {
492
+ failures.push({
493
+ property,
494
+ reason: "validation.beDateTime",
495
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
496
+ });
497
+ return false;
498
+ }
499
+ const is = Is.dateTimeString(value);
500
+ if (!is) {
501
+ failures.push({
502
+ property,
503
+ reason: "validation.beDateTime",
504
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
505
+ });
506
+ }
507
+ return is;
508
+ }
509
+ /**
510
+ * Is the property a time in ISO 8601 format.
511
+ * @param property The name of the property.
512
+ * @param value The value to test.
513
+ * @param failures The list of failures to add to.
514
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
515
+ * @returns True if the value is a time.
516
+ */
517
+ static timeString(property, value, failures, fieldNameResource) {
518
+ if (!Is.stringValue(value)) {
519
+ failures.push({
520
+ property,
521
+ reason: "validation.beTime",
522
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
523
+ });
524
+ return false;
525
+ }
526
+ const is = Is.timeString(value);
527
+ if (!is) {
528
+ failures.push({
529
+ property,
530
+ reason: "validation.beTime",
531
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
532
+ });
533
+ }
534
+ return is;
535
+ }
536
+ /**
537
+ * Is the property a timestamp in milliseconds.
538
+ * @param property The name of the property.
539
+ * @param value The value to test.
540
+ * @param failures The list of failures to add to.
541
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
542
+ * @returns True if the value is a timestamp in milliseconds.
543
+ */
544
+ static timestampMilliseconds(property, value, failures, fieldNameResource) {
545
+ if (!Is.integer(value)) {
546
+ failures.push({
547
+ property,
548
+ reason: "validation.beTimestampMilliseconds",
549
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
550
+ });
551
+ return false;
552
+ }
553
+ const is = Is.timestampMilliseconds(value);
554
+ if (!is) {
555
+ failures.push({
556
+ property,
557
+ reason: "validation.beTimestampMilliseconds",
558
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
559
+ });
560
+ }
561
+ return is;
562
+ }
563
+ /**
564
+ * Is the property a timestamp in seconds.
565
+ * @param property The name of the property.
566
+ * @param value The value to test.
567
+ * @param failures The list of failures to add to.
568
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
569
+ * @returns True if the value is a timestamp in seconds.
570
+ */
571
+ static timestampSeconds(property, value, failures, fieldNameResource) {
572
+ if (!Is.integer(value)) {
573
+ failures.push({
574
+ property,
575
+ reason: "validation.beTimestampSeconds",
576
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
577
+ });
578
+ return false;
579
+ }
580
+ const is = Is.timestampSeconds(value);
581
+ if (!is) {
582
+ failures.push({
583
+ property,
584
+ reason: "validation.beTimestampSeconds",
585
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
586
+ });
587
+ }
588
+ return is;
589
+ }
590
+ /**
591
+ * Is the property an object.
592
+ * @param property The name of the property.
593
+ * @param value The value to test.
594
+ * @param failures The list of failures to add to.
595
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
596
+ * @returns True if the value is a object.
597
+ */
598
+ static object(property, value, failures, fieldNameResource) {
599
+ const is = Is.object(value);
600
+ if (!is) {
601
+ failures.push({
602
+ property,
603
+ reason: "validation.beObject",
604
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
605
+ });
606
+ }
607
+ return is;
608
+ }
609
+ /**
610
+ * Is the property an array.
611
+ * @param property The name of the property.
612
+ * @param value The value to test.
613
+ * @param failures The list of failures to add to.
614
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
615
+ * @returns True if the value is an array.
616
+ */
617
+ static array(property, value, failures, fieldNameResource) {
618
+ const is = Is.array(value);
619
+ if (!is) {
620
+ failures.push({
621
+ property,
622
+ reason: "validation.beArray",
623
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
624
+ });
625
+ }
626
+ return is;
627
+ }
628
+ /**
629
+ * Is the property an array with at least one item.
630
+ * @param property The name of the property.
631
+ * @param value The value to test.
632
+ * @param failures The list of failures to add to.
633
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
634
+ * @returns True if the value is an array with at least one element.
635
+ */
636
+ static arrayValue(property, value, failures, fieldNameResource) {
637
+ const is = Is.array(value) && value.length > 0;
638
+ if (!is) {
639
+ failures.push({
640
+ property,
641
+ reason: "validation.beArrayValue",
642
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
643
+ });
644
+ }
645
+ return is;
646
+ }
647
+ /**
648
+ * Is the property one of a list of items.
649
+ * @param property The name of the property.
650
+ * @param value The value to test.
651
+ * @param options The options the value must be one of.
652
+ * @param failures The list of failures to add to.
653
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
654
+ * @returns True if the value is one of the items in the options.
655
+ */
656
+ static arrayOneOf(property, value, options, failures, fieldNameResource) {
657
+ if (Is.empty(value)) {
658
+ failures.push({
659
+ property,
660
+ reason: "validation.beIncluded",
661
+ properties: {
662
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
663
+ value,
664
+ options
665
+ }
666
+ });
667
+ return false;
668
+ }
669
+ const is = Is.arrayOneOf(value, options);
670
+ if (!is) {
671
+ failures.push({
672
+ property,
673
+ reason: "validation.beIncluded",
674
+ properties: {
675
+ fieldName: fieldNameResource ?? "validation.defaultFieldName",
676
+ value,
677
+ options
678
+ }
679
+ });
680
+ }
681
+ return is;
682
+ }
683
+ /**
684
+ * Is the property a Uint8Array.
685
+ * @param property The name of the property.
686
+ * @param value The value to test.
687
+ * @param failures The list of failures to add to.
688
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
689
+ * @returns True if the value is a Uint8Array.
690
+ */
691
+ static uint8Array(property, value, failures, fieldNameResource) {
692
+ const is = Is.uint8Array(value);
693
+ if (!is) {
694
+ failures.push({
695
+ property,
696
+ reason: "validation.beByteArray",
697
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
698
+ });
699
+ }
700
+ return is;
701
+ }
702
+ /**
703
+ * Is the property valid JSON.
704
+ * @param property The name of the property.
705
+ * @param value The value to test.
706
+ * @param failures The list of failures to add to.
707
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
708
+ * @returns True if the value is valid JSON.
709
+ */
710
+ static json(property, value, failures, fieldNameResource) {
711
+ const is = Is.json(value);
712
+ if (!is) {
713
+ failures.push({
714
+ property,
715
+ reason: "validation.beJSON",
716
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
717
+ });
718
+ }
719
+ return is;
720
+ }
721
+ /**
722
+ * Is the property a string in e-mail format.
723
+ * @param property The name of the property.
724
+ * @param value The value to test.
725
+ * @param failures The list of failures to add to.
726
+ * @param fieldNameResource Optional i18n resource of the field name to display in the message.
727
+ * @returns True if the value is a valid looking e-mail.
728
+ */
729
+ static email(property, value, failures, fieldNameResource) {
730
+ const is = Is.email(value);
731
+ if (!is) {
732
+ failures.push({
733
+ property,
734
+ reason: "validation.beEmail",
735
+ properties: { fieldName: fieldNameResource ?? "validation.defaultFieldName", value }
736
+ });
737
+ }
738
+ return is;
739
+ }
740
+ /**
741
+ * Throw the validation failures as a ValidationError.
742
+ * @param source The source of the error.
743
+ * @param objectName The object that was being validated.
744
+ * @param failures The validation failures.
745
+ * @throws ValidationError From the converted failures.
746
+ */
747
+ static asValidationError(source, objectName, failures) {
748
+ if (Is.arrayValue(failures)) {
749
+ throw new ValidationError(source, objectName, failures);
750
+ }
751
+ }
752
+ /**
753
+ * Map a list of failures to their properties in a map.
754
+ * @param failures The validation failures to combine into the map for the properties.
755
+ * @param propertyMap The map to add the failures to.
756
+ * @param clearMap Should the map be cleared before adding the failures.
757
+ */
758
+ static toPropertyMap(failures, propertyMap, clearMap = true) {
759
+ if (clearMap) {
760
+ for (const prop in propertyMap) {
761
+ delete propertyMap[prop];
762
+ }
763
+ }
764
+ for (const validationFailure of failures) {
765
+ if (Is.array(propertyMap[validationFailure.property])) {
766
+ propertyMap[validationFailure.property].push(validationFailure);
767
+ }
768
+ else {
769
+ propertyMap[validationFailure.property] = [validationFailure];
770
+ }
771
+ }
772
+ }
773
+ }
774
+ //# sourceMappingURL=validation.js.map