@croct/sdk 0.17.7 → 0.17.8

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 (409) hide show
  1. package/activeRecord.cjs.map +1 -0
  2. package/activeRecord.d.ts +8 -4
  3. package/activeRecord.js +115 -106
  4. package/activeRecord.js.map +1 -1
  5. package/apiKey.cjs.map +1 -0
  6. package/apiKey.d.ts +4 -2
  7. package/apiKey.js +152 -141
  8. package/apiKey.js.map +1 -1
  9. package/base64Url.cjs.map +1 -0
  10. package/base64Url.d.ts +4 -2
  11. package/base64Url.js +8 -15
  12. package/base64Url.js.map +1 -1
  13. package/cache/cache.cjs.map +1 -0
  14. package/cache/cache.d.ts +6 -4
  15. package/cache/cache.js +9 -10
  16. package/cache/cache.js.map +1 -1
  17. package/cache/cookieCache.cjs.map +1 -0
  18. package/cache/cookieCache.d.ts +6 -3
  19. package/cache/cookieCache.js +52 -54
  20. package/cache/cookieCache.js.map +1 -1
  21. package/cache/fallbackCache.cjs.map +1 -0
  22. package/cache/fallbackCache.d.ts +5 -2
  23. package/cache/fallbackCache.js +20 -21
  24. package/cache/fallbackCache.js.map +1 -1
  25. package/cache/inMemoryCache.cjs.map +1 -0
  26. package/cache/inMemoryCache.d.ts +5 -2
  27. package/cache/inMemoryCache.js +15 -17
  28. package/cache/inMemoryCache.js.map +1 -1
  29. package/cache/index.cjs.map +1 -0
  30. package/cache/index.d.ts +4 -4
  31. package/cache/index.js +9 -11
  32. package/cache/index.js.map +1 -1
  33. package/cache/localStorageCache.cjs.map +1 -0
  34. package/cache/localStorageCache.d.ts +5 -2
  35. package/cache/localStorageCache.js +55 -63
  36. package/cache/localStorageCache.js.map +1 -1
  37. package/channel/channel.cjs.map +1 -0
  38. package/channel/channel.d.ts +8 -6
  39. package/channel/channel.js +21 -22
  40. package/channel/channel.js.map +1 -1
  41. package/channel/encodedChannel.cjs.map +1 -0
  42. package/channel/encodedChannel.d.ts +6 -3
  43. package/channel/encodedChannel.js +13 -14
  44. package/channel/encodedChannel.js.map +1 -1
  45. package/channel/guaranteedChannel.cjs.map +1 -0
  46. package/channel/guaranteedChannel.d.ts +9 -7
  47. package/channel/guaranteedChannel.js +74 -73
  48. package/channel/guaranteedChannel.js.map +1 -1
  49. package/channel/httpBeaconChannel.cjs.map +1 -0
  50. package/channel/httpBeaconChannel.d.ts +9 -6
  51. package/channel/httpBeaconChannel.js +82 -83
  52. package/channel/httpBeaconChannel.js.map +1 -1
  53. package/channel/index.cjs.map +1 -0
  54. package/channel/index.d.ts +12 -7
  55. package/channel/index.js +15 -17
  56. package/channel/index.js.map +1 -1
  57. package/channel/queuedChannel.cjs.map +1 -0
  58. package/channel/queuedChannel.d.ts +7 -4
  59. package/channel/queuedChannel.js +87 -97
  60. package/channel/queuedChannel.js.map +1 -1
  61. package/channel/retryChannel.cjs.map +1 -0
  62. package/channel/retryChannel.d.ts +7 -5
  63. package/channel/retryChannel.js +58 -57
  64. package/channel/retryChannel.js.map +1 -1
  65. package/channel/sandboxChannel.cjs.map +1 -0
  66. package/channel/sandboxChannel.d.ts +5 -2
  67. package/channel/sandboxChannel.js +34 -35
  68. package/channel/sandboxChannel.js.map +1 -1
  69. package/cid/assigner.cjs.map +1 -0
  70. package/cid/assigner.d.ts +3 -1
  71. package/cid/assigner.js +0 -2
  72. package/cid/assigner.js.map +1 -1
  73. package/cid/cachedAssigner.cjs.map +1 -0
  74. package/cid/cachedAssigner.d.ts +7 -5
  75. package/cid/cachedAssigner.js +37 -43
  76. package/cid/cachedAssigner.js.map +1 -1
  77. package/cid/fixedAssigner.cjs.map +1 -0
  78. package/cid/fixedAssigner.d.ts +5 -2
  79. package/cid/fixedAssigner.js +9 -10
  80. package/cid/fixedAssigner.js.map +1 -1
  81. package/cid/index.cjs.map +1 -0
  82. package/cid/index.d.ts +6 -4
  83. package/cid/index.js +9 -11
  84. package/cid/index.js.map +1 -1
  85. package/cid/remoteAssigner.cjs.map +1 -0
  86. package/cid/remoteAssigner.d.ts +6 -3
  87. package/cid/remoteAssigner.js +36 -37
  88. package/cid/remoteAssigner.js.map +1 -1
  89. package/constants.cjs.map +1 -0
  90. package/constants.d.ts +6 -4
  91. package/constants.js +10 -7
  92. package/constants.js.map +1 -1
  93. package/container.cjs.map +1 -0
  94. package/container.d.ts +28 -15
  95. package/container.js +269 -252
  96. package/container.js.map +1 -1
  97. package/contentFetcher.cjs.map +1 -0
  98. package/contentFetcher.d.ts +17 -14
  99. package/contentFetcher.js +156 -163
  100. package/contentFetcher.js.map +1 -1
  101. package/context.cjs.map +1 -0
  102. package/context.d.ts +13 -9
  103. package/context.js +80 -81
  104. package/context.js.map +1 -1
  105. package/error.cjs.map +1 -0
  106. package/error.d.ts +4 -2
  107. package/error.js +21 -21
  108. package/error.js.map +1 -1
  109. package/evaluator.cjs.map +1 -0
  110. package/evaluator.d.ts +18 -16
  111. package/evaluator.js +179 -179
  112. package/evaluator.js.map +1 -1
  113. package/eventManager.cjs.map +1 -0
  114. package/eventManager.d.ts +8 -6
  115. package/eventManager.js +24 -26
  116. package/eventManager.js.map +1 -1
  117. package/eventSubjectProcessor.cjs.map +1 -0
  118. package/eventSubjectProcessor.d.ts +16 -3
  119. package/eventSubjectProcessor.js +53 -59
  120. package/eventSubjectProcessor.js.map +1 -1
  121. package/facade/contentFetcherFacade.cjs.map +1 -0
  122. package/facade/contentFetcherFacade.d.ts +16 -7
  123. package/facade/contentFetcherFacade.js +32 -35
  124. package/facade/contentFetcherFacade.js.map +1 -1
  125. package/facade/evaluatorFacade.cjs.map +1 -0
  126. package/facade/evaluatorFacade.d.ts +17 -10
  127. package/facade/evaluatorFacade.js +56 -60
  128. package/facade/evaluatorFacade.js.map +1 -1
  129. package/facade/index.cjs.map +1 -0
  130. package/facade/index.d.ts +29 -7
  131. package/facade/index.js +19 -19
  132. package/facade/index.js.map +1 -1
  133. package/facade/sdkFacade.cjs.map +1 -0
  134. package/facade/sdkFacade.d.ts +33 -15
  135. package/facade/sdkFacade.js +201 -214
  136. package/facade/sdkFacade.js.map +1 -1
  137. package/facade/sessionFacade.cjs.map +1 -0
  138. package/facade/sessionFacade.d.ts +18 -3
  139. package/facade/sessionFacade.js +10 -11
  140. package/facade/sessionFacade.js.map +1 -1
  141. package/facade/sessionPatch.cjs.map +1 -0
  142. package/facade/sessionPatch.d.ts +17 -4
  143. package/facade/sessionPatch.js +22 -24
  144. package/facade/sessionPatch.js.map +1 -1
  145. package/facade/trackerFacade.cjs.map +1 -0
  146. package/facade/trackerFacade.d.ts +17 -4
  147. package/facade/trackerFacade.js +67 -57
  148. package/facade/trackerFacade.js.map +1 -1
  149. package/facade/userFacade.cjs.map +1 -0
  150. package/facade/userFacade.d.ts +21 -4
  151. package/facade/userFacade.js +17 -18
  152. package/facade/userFacade.js.map +1 -1
  153. package/facade/userPatch.cjs.map +1 -0
  154. package/facade/userPatch.d.ts +17 -4
  155. package/facade/userPatch.js +22 -24
  156. package/facade/userPatch.js.map +1 -1
  157. package/help.cjs.map +1 -0
  158. package/help.d.ts +3 -1
  159. package/help.js +19 -23
  160. package/help.js.map +1 -1
  161. package/index.cjs.map +1 -0
  162. package/index.d.ts +25 -3
  163. package/index.js +7 -7
  164. package/index.js.map +1 -1
  165. package/logging/consoleLogger.cjs.map +1 -0
  166. package/logging/consoleLogger.d.ts +5 -2
  167. package/logging/consoleLogger.js +23 -24
  168. package/logging/consoleLogger.js.map +1 -1
  169. package/logging/filteredLogger.cjs.map +1 -0
  170. package/logging/filteredLogger.d.ts +8 -5
  171. package/logging/filteredLogger.js +36 -37
  172. package/logging/filteredLogger.js.map +1 -1
  173. package/logging/index.cjs.map +1 -0
  174. package/logging/index.d.ts +5 -5
  175. package/logging/index.js +11 -13
  176. package/logging/index.js.map +1 -1
  177. package/logging/logger.cjs.map +1 -0
  178. package/logging/logger.d.ts +4 -2
  179. package/logging/logger.js +0 -2
  180. package/logging/logger.js.map +1 -1
  181. package/logging/namespacedLogger.cjs.map +1 -0
  182. package/logging/namespacedLogger.d.ts +5 -2
  183. package/logging/namespacedLogger.js +22 -23
  184. package/logging/namespacedLogger.js.map +1 -1
  185. package/logging/nullLogger.cjs.map +1 -0
  186. package/logging/nullLogger.d.ts +5 -2
  187. package/logging/nullLogger.js +11 -16
  188. package/logging/nullLogger.js.map +1 -1
  189. package/namespacedStorage.cjs.map +1 -0
  190. package/namespacedStorage.d.ts +3 -1
  191. package/namespacedStorage.js +51 -52
  192. package/namespacedStorage.js.map +1 -1
  193. package/package.json +60 -6
  194. package/patch.cjs.map +1 -0
  195. package/patch.d.ts +6 -4
  196. package/patch.js +0 -2
  197. package/patch.js.map +1 -1
  198. package/queue/capacityRestrictedQueue.cjs.map +1 -0
  199. package/queue/capacityRestrictedQueue.d.ts +5 -2
  200. package/queue/capacityRestrictedQueue.js +31 -32
  201. package/queue/capacityRestrictedQueue.js.map +1 -1
  202. package/queue/inMemoryQueue.cjs.map +1 -0
  203. package/queue/inMemoryQueue.d.ts +5 -2
  204. package/queue/inMemoryQueue.js +32 -34
  205. package/queue/inMemoryQueue.js.map +1 -1
  206. package/queue/index.cjs.map +1 -0
  207. package/queue/index.d.ts +6 -5
  208. package/queue/index.js +11 -13
  209. package/queue/index.js.map +1 -1
  210. package/queue/monitoredQueue.cjs.map +1 -0
  211. package/queue/monitoredQueue.d.ts +8 -5
  212. package/queue/monitoredQueue.js +104 -110
  213. package/queue/monitoredQueue.js.map +1 -1
  214. package/queue/persistentQueue.cjs.map +1 -0
  215. package/queue/persistentQueue.d.ts +5 -2
  216. package/queue/persistentQueue.js +52 -54
  217. package/queue/persistentQueue.js.map +1 -1
  218. package/queue/queue.cjs.map +1 -0
  219. package/queue/queue.d.ts +3 -1
  220. package/queue/queue.js +0 -2
  221. package/queue/queue.js.map +1 -1
  222. package/retry/arbitraryPolicy.cjs.map +1 -0
  223. package/retry/arbitraryPolicy.d.ts +5 -2
  224. package/retry/arbitraryPolicy.js +14 -15
  225. package/retry/arbitraryPolicy.js.map +1 -1
  226. package/retry/backoffPolicy.cjs.map +1 -0
  227. package/retry/backoffPolicy.d.ts +5 -3
  228. package/retry/backoffPolicy.js +52 -50
  229. package/retry/backoffPolicy.js.map +1 -1
  230. package/retry/index.cjs.map +1 -0
  231. package/retry/index.d.ts +5 -5
  232. package/retry/index.js +11 -13
  233. package/retry/index.js.map +1 -1
  234. package/retry/maxAttemptsPolicy.cjs.map +1 -0
  235. package/retry/maxAttemptsPolicy.d.ts +5 -2
  236. package/retry/maxAttemptsPolicy.js +17 -18
  237. package/retry/maxAttemptsPolicy.js.map +1 -1
  238. package/retry/neverPolicy.cjs.map +1 -0
  239. package/retry/neverPolicy.d.ts +5 -2
  240. package/retry/neverPolicy.js +9 -10
  241. package/retry/neverPolicy.js.map +1 -1
  242. package/retry/policy.cjs.map +1 -0
  243. package/retry/policy.d.ts +3 -1
  244. package/retry/policy.js +0 -2
  245. package/retry/policy.js.map +1 -1
  246. package/schema/attributeSchema.cjs.map +1 -0
  247. package/schema/attributeSchema.d.ts +6 -2
  248. package/schema/attributeSchema.js +7 -7
  249. package/schema/attributeSchema.js.map +1 -1
  250. package/schema/contentFetcherSchemas.cjs.map +1 -0
  251. package/schema/contentFetcherSchemas.d.ts +6 -2
  252. package/schema/contentFetcherSchemas.js +24 -21
  253. package/schema/contentFetcherSchemas.js.map +1 -1
  254. package/schema/contentSchemas.cjs.map +1 -0
  255. package/schema/contentSchemas.d.ts +6 -2
  256. package/schema/contentSchemas.js +45 -45
  257. package/schema/contentSchemas.js.map +1 -1
  258. package/schema/contextSchemas.cjs.map +1 -0
  259. package/schema/contextSchemas.d.ts +6 -2
  260. package/schema/contextSchemas.js +6 -6
  261. package/schema/contextSchemas.js.map +1 -1
  262. package/schema/ecommerceSchemas.cjs.map +1 -0
  263. package/schema/ecommerceSchemas.d.ts +10 -6
  264. package/schema/ecommerceSchemas.js +176 -172
  265. package/schema/ecommerceSchemas.js.map +1 -1
  266. package/schema/evaluatorSchemas.cjs.map +1 -0
  267. package/schema/evaluatorSchemas.d.ts +6 -2
  268. package/schema/evaluatorSchemas.js +46 -30
  269. package/schema/evaluatorSchemas.js.map +1 -1
  270. package/schema/eventSchemas.cjs.map +1 -0
  271. package/schema/eventSchemas.d.ts +16 -12
  272. package/schema/eventSchemas.js +125 -102
  273. package/schema/eventSchemas.js.map +1 -1
  274. package/schema/index.cjs.map +1 -0
  275. package/schema/index.d.ts +14 -11
  276. package/schema/index.js +11 -14
  277. package/schema/index.js.map +1 -1
  278. package/schema/loggerSchema.cjs.map +1 -0
  279. package/schema/loggerSchema.d.ts +6 -2
  280. package/schema/loggerSchema.js +13 -13
  281. package/schema/loggerSchema.js.map +1 -1
  282. package/schema/operationSchemas.cjs.map +1 -0
  283. package/schema/operationSchemas.d.ts +14 -10
  284. package/schema/operationSchemas.js +85 -69
  285. package/schema/operationSchemas.js.map +1 -1
  286. package/schema/sdkFacadeSchemas.cjs.map +1 -0
  287. package/schema/sdkFacadeSchemas.d.ts +6 -2
  288. package/schema/sdkFacadeSchemas.js +65 -51
  289. package/schema/sdkFacadeSchemas.js.map +1 -1
  290. package/schema/sdkSchemas.cjs.map +1 -0
  291. package/schema/sdkSchemas.d.ts +8 -4
  292. package/schema/sdkSchemas.js +80 -78
  293. package/schema/sdkSchemas.js.map +1 -1
  294. package/schema/tokenSchema.cjs.map +1 -0
  295. package/schema/tokenSchema.d.ts +6 -2
  296. package/schema/tokenSchema.js +41 -38
  297. package/schema/tokenSchema.js.map +1 -1
  298. package/schema/userSchema.cjs.map +1 -0
  299. package/schema/userSchema.d.ts +6 -2
  300. package/schema/userSchema.js +183 -134
  301. package/schema/userSchema.js.map +1 -1
  302. package/sdk.cjs.map +1 -0
  303. package/sdk.d.ts +28 -14
  304. package/sdk.js +106 -108
  305. package/sdk.js.map +1 -1
  306. package/sdkEvents.cjs.map +1 -0
  307. package/sdkEvents.d.ts +10 -5
  308. package/sdkEvents.js +0 -2
  309. package/sdkEvents.js.map +1 -1
  310. package/sourceLocation.cjs.map +1 -0
  311. package/sourceLocation.d.ts +7 -5
  312. package/sourceLocation.js +56 -57
  313. package/sourceLocation.js.map +1 -1
  314. package/tab.cjs.map +1 -0
  315. package/tab.d.ts +9 -7
  316. package/tab.js +93 -90
  317. package/tab.js.map +1 -1
  318. package/token/cachedTokenStore.cjs.map +1 -0
  319. package/token/cachedTokenStore.d.ts +8 -3
  320. package/token/cachedTokenStore.js +22 -24
  321. package/token/cachedTokenStore.js.map +1 -1
  322. package/token/inMemoryTokenStore.cjs.map +1 -0
  323. package/token/inMemoryTokenStore.d.ts +7 -2
  324. package/token/inMemoryTokenStore.js +12 -13
  325. package/token/inMemoryTokenStore.js.map +1 -1
  326. package/token/index.cjs.map +1 -0
  327. package/token/index.d.ts +7 -4
  328. package/token/index.js +9 -11
  329. package/token/index.js.map +1 -1
  330. package/token/replicatedTokenStore.cjs.map +1 -0
  331. package/token/replicatedTokenStore.d.ts +7 -2
  332. package/token/replicatedTokenStore.js +14 -15
  333. package/token/replicatedTokenStore.js.map +1 -1
  334. package/token/token.cjs.map +1 -0
  335. package/token/token.d.ts +11 -8
  336. package/token/token.js +212 -195
  337. package/token/token.js.map +1 -1
  338. package/tracker.cjs.map +1 -0
  339. package/tracker.d.ts +20 -13
  340. package/tracker.js +307 -307
  341. package/tracker.js.map +1 -1
  342. package/trackingEvents.cjs.map +1 -0
  343. package/trackingEvents.d.ts +65 -62
  344. package/trackingEvents.js +57 -56
  345. package/trackingEvents.js.map +1 -1
  346. package/transformer.cjs.map +1 -0
  347. package/transformer.d.ts +4 -2
  348. package/transformer.js +5 -6
  349. package/transformer.js.map +1 -1
  350. package/utilityTypes.cjs.map +1 -0
  351. package/utilityTypes.d.ts +4 -2
  352. package/utilityTypes.js +0 -2
  353. package/utilityTypes.js.map +1 -1
  354. package/uuid.cjs.map +1 -0
  355. package/uuid.d.ts +3 -1
  356. package/uuid.js +27 -32
  357. package/uuid.js.map +1 -1
  358. package/validation/arrayType.cjs.map +1 -0
  359. package/validation/arrayType.d.ts +5 -3
  360. package/validation/arrayType.js +45 -41
  361. package/validation/arrayType.js.map +1 -1
  362. package/validation/booleanType.cjs.map +1 -0
  363. package/validation/booleanType.d.ts +5 -2
  364. package/validation/booleanType.js +19 -16
  365. package/validation/booleanType.js.map +1 -1
  366. package/validation/functionType.cjs.map +1 -0
  367. package/validation/functionType.d.ts +5 -2
  368. package/validation/functionType.js +19 -16
  369. package/validation/functionType.js.map +1 -1
  370. package/validation/index.cjs.map +1 -0
  371. package/validation/index.d.ts +12 -12
  372. package/validation/index.js +27 -29
  373. package/validation/index.js.map +1 -1
  374. package/validation/jsonType.cjs.map +1 -0
  375. package/validation/jsonType.d.ts +8 -6
  376. package/validation/jsonType.js +89 -82
  377. package/validation/jsonType.js.map +1 -1
  378. package/validation/mixedSchema.cjs.map +1 -0
  379. package/validation/mixedSchema.d.ts +5 -2
  380. package/validation/mixedSchema.js +5 -7
  381. package/validation/mixedSchema.js.map +1 -1
  382. package/validation/nullType.cjs.map +1 -0
  383. package/validation/nullType.d.ts +5 -2
  384. package/validation/nullType.js +19 -16
  385. package/validation/nullType.js.map +1 -1
  386. package/validation/numberType.cjs.map +1 -0
  387. package/validation/numberType.d.ts +5 -3
  388. package/validation/numberType.js +40 -32
  389. package/validation/numberType.js.map +1 -1
  390. package/validation/objectType.cjs.map +1 -0
  391. package/validation/objectType.d.ts +5 -3
  392. package/validation/objectType.js +84 -85
  393. package/validation/objectType.js.map +1 -1
  394. package/validation/schema.cjs.map +1 -0
  395. package/validation/schema.d.ts +5 -3
  396. package/validation/schema.js +8 -9
  397. package/validation/schema.js.map +1 -1
  398. package/validation/stringType.cjs.map +1 -0
  399. package/validation/stringType.d.ts +5 -3
  400. package/validation/stringType.js +74 -70
  401. package/validation/stringType.js.map +1 -1
  402. package/validation/unionType.cjs.map +1 -0
  403. package/validation/unionType.d.ts +5 -2
  404. package/validation/unionType.js +37 -35
  405. package/validation/unionType.js.map +1 -1
  406. package/validation/violation.cjs.map +1 -0
  407. package/validation/violation.d.ts +4 -2
  408. package/validation/violation.js +18 -18
  409. package/validation/violation.js.map +1 -1
@@ -1,92 +1,91 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ObjectType = void 0;
4
- const schema_1 = require("./schema");
5
- const mixedSchema_1 = require("./mixedSchema");
6
- const violation_1 = require("./violation");
1
+ import { Violation } from "./schema";
2
+ import { MixedSchema } from "./mixedSchema";
3
+ import { describe, formatPath } from "./violation";
7
4
  class ObjectType {
8
- constructor(schema = {}) {
9
- var _a, _b, _c, _d, _e, _f;
10
- this.definition = {
11
- ...schema,
12
- properties: (_a = schema.properties) !== null && _a !== void 0 ? _a : {},
13
- required: (_b = schema.required) !== null && _b !== void 0 ? _b : [],
14
- additionalProperties: (_c = schema.additionalProperties) !== null && _c !== void 0 ? _c : false,
15
- propertyNames: (_d = schema.propertyNames) !== null && _d !== void 0 ? _d : new mixedSchema_1.MixedSchema(),
16
- minProperties: (_e = schema.minProperties) !== null && _e !== void 0 ? _e : -1,
17
- maxProperties: (_f = schema.maxProperties) !== null && _f !== void 0 ? _f : -1,
18
- };
5
+ constructor(schema = {}) {
6
+ this.definition = {
7
+ ...schema,
8
+ properties: schema.properties ?? {},
9
+ required: schema.required ?? [],
10
+ additionalProperties: schema.additionalProperties ?? false,
11
+ propertyNames: schema.propertyNames ?? new MixedSchema(),
12
+ minProperties: schema.minProperties ?? -1,
13
+ maxProperties: schema.maxProperties ?? -1
14
+ };
15
+ }
16
+ getTypes() {
17
+ if (this.definition.type !== void 0) {
18
+ return [this.definition.type.name];
19
19
  }
20
- getTypes() {
21
- if (this.definition.type !== undefined) {
22
- return [this.definition.type.name];
23
- }
24
- return ['object'];
20
+ return ["object"];
21
+ }
22
+ isValidType(value) {
23
+ if (this.definition.type !== void 0) {
24
+ return value instanceof this.definition.type;
25
25
  }
26
- isValidType(value) {
27
- if (this.definition.type !== undefined) {
28
- return value instanceof this.definition.type;
29
- }
30
- return Object.prototype
31
- .toString
32
- .call(value) === '[object Object]';
26
+ return Object.prototype.toString.call(value) === "[object Object]";
27
+ }
28
+ validate(value, path = []) {
29
+ if (!this.isValidType(value)) {
30
+ const [type] = this.getTypes();
31
+ throw new Violation(
32
+ `Expected value of type ${type} at path '${formatPath(path)}', actual ${describe(value)}.`,
33
+ path,
34
+ { type }
35
+ );
33
36
  }
34
- validate(value, path = []) {
35
- if (!this.isValidType(value)) {
36
- const [type] = this.getTypes();
37
- throw new schema_1.Violation(`Expected value of type ${type} at path '${(0, violation_1.formatPath)(path)}', `
38
- + `actual ${(0, violation_1.describe)(value)}.`, path, { type: type });
39
- }
40
- const entries = Object.entries(value);
41
- const { minProperties, maxProperties } = this.definition;
42
- if (minProperties >= 0 && minProperties > entries.length) {
43
- throw new schema_1.Violation(`Expected ${minProperties === maxProperties ? 'exactly' : 'at least'} `
44
- + `${minProperties} ${minProperties === 1 ? 'entry' : 'entries'} `
45
- + `at path '${(0, violation_1.formatPath)(path)}', actual ${entries.length}.`, path, { limit: minProperties });
46
- }
47
- if (maxProperties >= 0 && maxProperties < entries.length) {
48
- throw new schema_1.Violation(`Expected ${minProperties === maxProperties ? 'exactly' : 'at most'} `
49
- + `${maxProperties} ${maxProperties === 1 ? 'entry' : 'entries'} `
50
- + `at path '${(0, violation_1.formatPath)(path)}', actual ${entries.length}.`, path, { limit: maxProperties });
51
- }
52
- const additionalProperties = { ...value };
53
- for (const property of this.definition.required) {
54
- if (!(property in value)) {
55
- throw new schema_1.Violation(`Missing property '${(0, violation_1.formatPath)(path.concat([property]))}'.`, path, {
56
- required: property,
57
- });
58
- }
59
- }
60
- for (const [entryName, entryValue] of entries) {
61
- const propertyPath = path.concat([entryName]);
62
- this.definition
63
- .propertyNames
64
- .validate(entryName, propertyPath);
65
- const propertyRule = this.definition.properties[entryName];
66
- if (propertyRule !== undefined) {
67
- propertyRule.validate(entryValue, propertyPath);
68
- delete additionalProperties[entryName];
69
- continue;
70
- }
71
- if (this.definition.additionalProperties === false) {
72
- throw new schema_1.Violation(`Unknown property '${(0, violation_1.formatPath)(propertyPath)}'.`, propertyPath, {
73
- additionalProperty: entryName,
74
- });
75
- }
76
- if (this.definition.additionalProperties !== true) {
77
- this.definition
78
- .additionalProperties
79
- .validate(entryValue, propertyPath);
80
- }
81
- }
82
- const { subtypes } = this.definition;
83
- if (subtypes !== undefined) {
84
- const type = value[subtypes.discriminator];
85
- if (type !== undefined && subtypes.schemas[type] !== undefined) {
86
- subtypes.schemas[type].validate(additionalProperties, path);
87
- }
88
- }
37
+ const entries = Object.entries(value);
38
+ const { minProperties, maxProperties } = this.definition;
39
+ if (minProperties >= 0 && minProperties > entries.length) {
40
+ throw new Violation(
41
+ `Expected ${minProperties === maxProperties ? "exactly" : "at least"} ${minProperties} ${minProperties === 1 ? "entry" : "entries"} at path '${formatPath(path)}', actual ${entries.length}.`,
42
+ path,
43
+ { limit: minProperties }
44
+ );
89
45
  }
46
+ if (maxProperties >= 0 && maxProperties < entries.length) {
47
+ throw new Violation(
48
+ `Expected ${minProperties === maxProperties ? "exactly" : "at most"} ${maxProperties} ${maxProperties === 1 ? "entry" : "entries"} at path '${formatPath(path)}', actual ${entries.length}.`,
49
+ path,
50
+ { limit: maxProperties }
51
+ );
52
+ }
53
+ const additionalProperties = { ...value };
54
+ for (const property of this.definition.required) {
55
+ if (!(property in value)) {
56
+ throw new Violation(`Missing property '${formatPath(path.concat([property]))}'.`, path, {
57
+ required: property
58
+ });
59
+ }
60
+ }
61
+ for (const [entryName, entryValue] of entries) {
62
+ const propertyPath = path.concat([entryName]);
63
+ this.definition.propertyNames.validate(entryName, propertyPath);
64
+ const propertyRule = this.definition.properties[entryName];
65
+ if (propertyRule !== void 0) {
66
+ propertyRule.validate(entryValue, propertyPath);
67
+ delete additionalProperties[entryName];
68
+ continue;
69
+ }
70
+ if (this.definition.additionalProperties === false) {
71
+ throw new Violation(`Unknown property '${formatPath(propertyPath)}'.`, propertyPath, {
72
+ additionalProperty: entryName
73
+ });
74
+ }
75
+ if (this.definition.additionalProperties !== true) {
76
+ this.definition.additionalProperties.validate(entryValue, propertyPath);
77
+ }
78
+ }
79
+ const { subtypes } = this.definition;
80
+ if (subtypes !== void 0) {
81
+ const type = value[subtypes.discriminator];
82
+ if (type !== void 0 && subtypes.schemas[type] !== void 0) {
83
+ subtypes.schemas[type].validate(additionalProperties, path);
84
+ }
85
+ }
86
+ }
90
87
  }
91
- exports.ObjectType = ObjectType;
88
+ export {
89
+ ObjectType
90
+ };
92
91
  //# sourceMappingURL=objectType.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"objectType.js","sourceRoot":"","sources":["../src/validation/objectType.ts"],"names":[],"mappings":";;;AAAA,qCAAuD;AACvD,+CAA0C;AAC1C,2CAAiD;AAgBjD,MAAa,UAAU;IAGnB,YAAmB,SAAoC,EAAE;;QACrD,IAAI,CAAC,UAAU,GAAG;YACd,GAAG,MAAM;YACT,UAAU,EAAE,MAAA,MAAM,CAAC,UAAU,mCAAI,EAAE;YACnC,QAAQ,EAAE,MAAA,MAAM,CAAC,QAAQ,mCAAI,EAAE;YAC/B,oBAAoB,EAAE,MAAA,MAAM,CAAC,oBAAoB,mCAAI,KAAK;YAC1D,aAAa,EAAE,MAAA,MAAM,CAAC,aAAa,mCAAI,IAAI,yBAAW,EAAE;YACxD,aAAa,EAAE,MAAA,MAAM,CAAC,aAAa,mCAAI,CAAC,CAAC;YACzC,aAAa,EAAE,MAAA,MAAM,CAAC,aAAa,mCAAI,CAAC,CAAC;SAC5C,CAAC;IACN,CAAC;IAEM,QAAQ;QACX,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YACrC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACvC,CAAC;QAED,OAAO,CAAC,QAAQ,CAAC,CAAC;IACtB,CAAC;IAEM,WAAW,CAAC,KAAc;QAC7B,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YACrC,OAAO,KAAK,YAA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CAAC,UAAU,CAAC,oBAAoB,KAAK,KAAK,EAAE,CAAC;gBACjD,MAAM,IAAI,kBAAS,CAAC,qBAAqB,IAAA,sBAAU,EAAC,YAAY,CAAC,IAAI,EAAE,YAAY,EAAE;oBACjF,kBAAkB,EAAE,SAAS;iBAChC,CAAC,CAAC;YACP,CAAC;YAED,IAAI,IAAI,CAAC,UAAU,CAAC,oBAAoB,KAAK,IAAI,EAAE,CAAC;gBAChD,IAAI,CAAC,UAAU;qBACV,oBAAoB;qBACpB,QAAQ,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;YAC5C,CAAC;QACL,CAAC;QAED,MAAM,EAAC,QAAQ,EAAC,GAAG,IAAI,CAAC,UAAU,CAAC;QAEnC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YACzB,MAAM,IAAI,GAAI,KAAa,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC;YAEpD,IAAI,IAAI,KAAK,SAAS,IAAI,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;gBAC7D,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,oBAAoB,EAAE,IAAI,CAAC,CAAC;YAChE,CAAC;QACL,CAAC;IACL,CAAC;CACJ;AAvHD,gCAuHC"}
1
+ {"version":3,"sources":["../src/validation/objectType.ts"],"sourcesContent":["import {Schema, TypeSchema, Violation} from './schema';\nimport {MixedSchema} from './mixedSchema';\nimport {describe, formatPath} from './violation';\n\ntype ObjectDefinition = {\n type?: {new (...args: any): any},\n properties: {[key: string]: Schema},\n additionalProperties: boolean | Schema,\n subtypes?: {\n discriminator: string,\n schemas: {[key: string]: ObjectType},\n },\n propertyNames: Schema,\n required: string[],\n minProperties: number,\n maxProperties: number,\n};\n\nexport class ObjectType implements TypeSchema {\n private readonly definition: ObjectDefinition;\n\n public constructor(schema: Partial<ObjectDefinition> = {}) {\n this.definition = {\n ...schema,\n properties: schema.properties ?? {},\n required: schema.required ?? [],\n additionalProperties: schema.additionalProperties ?? false,\n propertyNames: schema.propertyNames ?? new MixedSchema(),\n minProperties: schema.minProperties ?? -1,\n maxProperties: schema.maxProperties ?? -1,\n };\n }\n\n public getTypes(): string[] {\n if (this.definition.type !== undefined) {\n return [this.definition.type.name];\n }\n\n return ['object'];\n }\n\n public isValidType(value: unknown): value is object {\n if (this.definition.type !== undefined) {\n return value instanceof this.definition.type;\n }\n\n return Object.prototype\n .toString\n .call(value) === '[object Object]';\n }\n\n public validate(value: unknown, path: string[] = []): void {\n if (!this.isValidType(value)) {\n const [type] = this.getTypes();\n\n throw new Violation(\n `Expected value of type ${type} at path '${formatPath(path)}', `\n + `actual ${describe(value)}.`,\n path,\n {type: type},\n );\n }\n\n const entries = Object.entries(value);\n\n const {minProperties, maxProperties} = this.definition;\n\n if (minProperties >= 0 && minProperties > entries.length) {\n throw new Violation(\n `Expected ${minProperties === maxProperties ? 'exactly' : 'at least'} `\n + `${minProperties} ${minProperties === 1 ? 'entry' : 'entries'} `\n + `at path '${formatPath(path)}', actual ${entries.length}.`,\n path,\n {limit: minProperties},\n );\n }\n\n if (maxProperties >= 0 && maxProperties < entries.length) {\n throw new Violation(\n `Expected ${minProperties === maxProperties ? 'exactly' : 'at most'} `\n + `${maxProperties} ${maxProperties === 1 ? 'entry' : 'entries'} `\n + `at path '${formatPath(path)}', actual ${entries.length}.`,\n path,\n {limit: maxProperties},\n );\n }\n\n const additionalProperties: {[key: string]: any} = {...value};\n\n for (const property of this.definition.required) {\n if (!(property in value)) {\n throw new Violation(`Missing property '${formatPath(path.concat([property]))}'.`, path, {\n required: property,\n });\n }\n }\n\n for (const [entryName, entryValue] of entries) {\n const propertyPath = path.concat([entryName]);\n\n this.definition\n .propertyNames\n .validate(entryName, propertyPath);\n\n const propertyRule = this.definition.properties[entryName];\n\n if (propertyRule !== undefined) {\n propertyRule.validate(entryValue, propertyPath);\n\n delete additionalProperties[entryName];\n\n continue;\n }\n\n if (this.definition.additionalProperties === false) {\n throw new Violation(`Unknown property '${formatPath(propertyPath)}'.`, propertyPath, {\n additionalProperty: entryName,\n });\n }\n\n if (this.definition.additionalProperties !== true) {\n this.definition\n .additionalProperties\n .validate(entryValue, propertyPath);\n }\n }\n\n const {subtypes} = this.definition;\n\n if (subtypes !== undefined) {\n const type = (value as any)[subtypes.discriminator];\n\n if (type !== undefined && subtypes.schemas[type] !== undefined) {\n subtypes.schemas[type].validate(additionalProperties, path);\n }\n }\n }\n}\n"],"mappings":"AAAA,SAA4B,iBAAgB;AAC5C,SAAQ,mBAAkB;AAC1B,SAAQ,UAAU,kBAAiB;AAgB5B,MAAM,WAAiC;AAAA,EAGnC,YAAY,SAAoC,CAAC,GAAG;AACvD,SAAK,aAAa;AAAA,MACd,GAAG;AAAA,MACH,YAAY,OAAO,cAAc,CAAC;AAAA,MAClC,UAAU,OAAO,YAAY,CAAC;AAAA,MAC9B,sBAAsB,OAAO,wBAAwB;AAAA,MACrD,eAAe,OAAO,iBAAiB,IAAI,YAAY;AAAA,MACvD,eAAe,OAAO,iBAAiB;AAAA,MACvC,eAAe,OAAO,iBAAiB;AAAA,IAC3C;AAAA,EACJ;AAAA,EAEO,WAAqB;AACxB,QAAI,KAAK,WAAW,SAAS,QAAW;AACpC,aAAO,CAAC,KAAK,WAAW,KAAK,IAAI;AAAA,IACrC;AAEA,WAAO,CAAC,QAAQ;AAAA,EACpB;AAAA,EAEO,YAAY,OAAiC;AAChD,QAAI,KAAK,WAAW,SAAS,QAAW;AACpC,aAAO,iBAAiB,KAAK,WAAW;AAAA,IAC5C;AAEA,WAAO,OAAO,UACT,SACA,KAAK,KAAK,MAAM;AAAA,EACzB;AAAA,EAEO,SAAS,OAAgB,OAAiB,CAAC,GAAS;AACvD,QAAI,CAAC,KAAK,YAAY,KAAK,GAAG;AAC1B,YAAM,CAAC,IAAI,IAAI,KAAK,SAAS;AAE7B,YAAM,IAAI;AAAA,QACN,0BAA0B,IAAI,aAAa,WAAW,IAAI,CAAC,aAC/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J;AAEA,UAAI,KAAK,WAAW,yBAAyB,OAAO;AAChD,cAAM,IAAI,UAAU,qBAAqB,WAAW,YAAY,CAAC,MAAM,cAAc;AAAA,UACjF,oBAAoB;AAAA,QACxB,CAAC;AAAA,MACL;AAEA,UAAI,KAAK,WAAW,yBAAyB,MAAM;AAC/C,aAAK,WACA,qBACA,SAAS,YAAY,YAAY;AAAA,MAC1C;AAAA,IACJ;AAEA,UAAM,EAAC,SAAQ,IAAI,KAAK;AAExB,QAAI,aAAa,QAAW;AACxB,YAAM,OAAQ,MAAc,SAAS,aAAa;AAElD,UAAI,SAAS,UAAa,SAAS,QAAQ,IAAI,MAAM,QAAW;AAC5D,iBAAS,QAAQ,IAAI,EAAE,SAAS,sBAAsB,IAAI;AAAA,MAC9D;AAAA,IACJ;AAAA,EACJ;AACJ;","names":[]}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/validation/schema.ts"],"sourcesContent":["export interface Schema {\n validate(value: unknown, path?: string[]): void;\n}\n\nexport interface TypeSchema extends Schema {\n getTypes(): string[];\n\n isValidType(value: unknown): boolean;\n}\n\nexport class Violation extends Error {\n public readonly path: string[];\n\n public readonly params: {[key: string]: unknown};\n\n public constructor(message: string, path: string[], params: {[p: string]: unknown}) {\n super(message);\n this.path = path;\n this.params = params;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,MAAM,kBAAkB,MAAM;AAAA,EAK1B,YAAY,SAAiB,MAAgB,QAAgC;AAChF,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAClB;AACJ;","names":[]}
@@ -1,11 +1,11 @@
1
- export interface Schema {
1
+ interface Schema {
2
2
  validate(value: unknown, path?: string[]): void;
3
3
  }
4
- export interface TypeSchema extends Schema {
4
+ interface TypeSchema extends Schema {
5
5
  getTypes(): string[];
6
6
  isValidType(value: unknown): boolean;
7
7
  }
8
- export declare class Violation extends Error {
8
+ declare class Violation extends Error {
9
9
  readonly path: string[];
10
10
  readonly params: {
11
11
  [key: string]: unknown;
@@ -14,3 +14,5 @@ export declare class Violation extends Error {
14
14
  [p: string]: unknown;
15
15
  });
16
16
  }
17
+
18
+ export { type Schema, type TypeSchema, Violation };
@@ -1,12 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Violation = void 0;
4
1
  class Violation extends Error {
5
- constructor(message, path, params) {
6
- super(message);
7
- this.path = path;
8
- this.params = params;
9
- }
2
+ constructor(message, path, params) {
3
+ super(message);
4
+ this.path = path;
5
+ this.params = params;
6
+ }
10
7
  }
11
- exports.Violation = Violation;
8
+ export {
9
+ Violation
10
+ };
12
11
  //# sourceMappingURL=schema.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/validation/schema.ts"],"names":[],"mappings":";;;AAUA,MAAa,SAAU,SAAQ,KAAK;IAKhC,YAAmB,OAAe,EAAE,IAAc,EAAE,MAA8B;QAC9E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;CACJ;AAVD,8BAUC"}
1
+ {"version":3,"sources":["../src/validation/schema.ts"],"sourcesContent":["export interface Schema {\n validate(value: unknown, path?: string[]): void;\n}\n\nexport interface TypeSchema extends Schema {\n getTypes(): string[];\n\n isValidType(value: unknown): boolean;\n}\n\nexport class Violation extends Error {\n public readonly path: string[];\n\n public readonly params: {[key: string]: unknown};\n\n public constructor(message: string, path: string[], params: {[p: string]: unknown}) {\n super(message);\n this.path = path;\n this.params = params;\n }\n}\n"],"mappings":"AAUO,MAAM,kBAAkB,MAAM;AAAA,EAK1B,YAAY,SAAiB,MAAgB,QAAgC;AAChF,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAClB;AACJ;","names":[]}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/validation/stringType.ts"],"sourcesContent":["import {TypeSchema, Violation} from './schema';\nimport {describe, formatPath} from './violation';\n\ninterface Format {\n (value: string): boolean;\n}\n\nconst FORMAT: {[key: string]: Format} = {\n pointer: function pointer(value: string): boolean {\n return /^(\\.|([a-zA-Z_][a-zA-Z0-9_]*|\\[[0-9]+])(\\.[a-zA-Z_][a-zA-Z0-9_]*|\\[[0-9]+])*)$/.test(value);\n },\n identifier: function identifier(value: string): boolean {\n return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);\n },\n uuid: function uuid(value: string): boolean {\n return /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(value);\n },\n date: function date(value: string): boolean {\n return /^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)$/.test(value);\n },\n url: function url(value: string): boolean {\n try {\n // eslint-disable-next-line no-new -- Easier way to validate a URL\n new URL(value);\n } catch {\n return false;\n }\n\n return true;\n },\n};\n\ntype StringDefinition = {\n minLength: number,\n maxLength: number,\n enumeration: string[],\n pattern?: RegExp,\n format?: keyof typeof FORMAT,\n};\n\nexport class StringType implements TypeSchema {\n private readonly definition: StringDefinition;\n\n public constructor(definition: Partial<StringDefinition> = {}) {\n this.definition = {\n ...definition,\n minLength: definition.minLength ?? -1,\n maxLength: definition.maxLength ?? -1,\n enumeration: definition.enumeration ?? [],\n };\n }\n\n public getTypes(): string[] {\n return ['string'];\n }\n\n public isValidType(value: unknown): value is string {\n return typeof value === 'string';\n }\n\n public validate(value: unknown, path: string[] = []): void {\n if (!this.isValidType(value)) {\n throw new Violation(\n `Expected value of type string at path '${formatPath(path)}', actual ${describe(value)}.`,\n path,\n {type: 'string'},\n );\n }\n\n const {minLength, maxLength} = this.definition;\n\n if (minLength >= 0 && minLength > value.length) {\n throw new Violation(\n `Expected ${minLength === maxLength ? 'exactly' : 'at least'} `\n + `${minLength} ${minLength === 1 ? 'character' : 'characters'} `\n + `at path '${formatPath(path)}', actual ${value.length}.`,\n path,\n {limit: minLength},\n );\n }\n\n if (maxLength >= 0 && maxLength < value.length) {\n throw new Violation(\n `Expected ${minLength === maxLength ? 'exactly' : 'at most'} `\n + `${maxLength} ${maxLength === 1 ? 'character' : 'characters'} `\n + `at path '${formatPath(path)}', actual ${value.length}.`,\n path,\n {limit: maxLength},\n );\n }\n\n const {enumeration} = this.definition;\n\n if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {\n throw new Violation(\n `Unexpected value at path '${formatPath(path)}', expecting `\n + `'${\n enumeration.length === 1\n ? enumeration[0]\n : `${enumeration.slice(0, -1).join('\\', \\'')}' or '${enumeration.slice(-1)}`\n }', `\n + `found '${value}'.`,\n path,\n {enumeration: enumeration},\n );\n }\n\n const {format, pattern} = this.definition;\n\n if (format !== undefined && !FORMAT[format](value)) {\n throw new Violation(`Invalid ${format} format at path '${formatPath(path)}'.`, path, {format: format});\n }\n\n if (pattern !== undefined && !pattern.test(value)) {\n throw new Violation(`Invalid format at path '${formatPath(path)}'.`, path, {pattern: pattern});\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoC;AACpC,uBAAmC;AAMnC,MAAM,SAAkC;AAAA,EACpC,SAAS,SAAS,QAAQ,OAAwB;AAC9C,WAAO,iFAAiF,KAAK,KAAK;AAAA,EACtG;AAAA,EACA,YAAY,SAAS,WAAW,OAAwB;AACpD,WAAO,2BAA2B,KAAK,KAAK;AAAA,EAChD;AAAA,EACA,MAAM,SAAS,KAAK,OAAwB;AACxC,WAAO,gDAAgD,KAAK,KAAK;AAAA,EACrE;AAAA,EACA,MAAM,SAAS,KAAK,OAAwB;AACxC,WAAO,6BAA6B,KAAK,KAAK;AAAA,EAClD;AAAA,EACA,KAAK,SAAS,IAAI,OAAwB;AACtC,QAAI;AAEA,UAAI,IAAI,KAAK;AAAA,IACjB,QAAQ;AACJ,aAAO;AAAA,IACX;AAEA,WAAO;AAAA,EACX;AAC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,YAAM,IAAI;AAAA,QACN,iCAA6B,6BAAW,IAAI,CAAC,iBAEzC,YAAY,WAAW,IACjB,YAAY,CAAC,IACb,GAAG,YAAY,MAAM,GAAG,EAAE,EAAE,KAAK,MAAQ,CAAC,SAAS,YAAY,MAAM,EAAE,CAAC,EAClF,aACY,KAAK;AAAA,QACjB;AAAA,QACA,EAAC,YAAwB;AAAA,MAC7B;AAAA,IACJ;AAEA,UAAM,EAAC,QAAQ,QAAO,IAAI,KAAK;AAE/B,QAAI,WAAW,UAAa,CAAC,OAAO,MAAM,EAAE,KAAK,GAAG;AAChD,YAAM,IAAI,wBAAU,WAAW,MAAM,wBAAoB,6BAAW,IAAI,CAAC,MAAM,MAAM,EAAC,OAAc,CAAC;AAAA,IACzG;AAEA,QAAI,YAAY,UAAa,CAAC,QAAQ,KAAK,KAAK,GAAG;AAC/C,YAAM,IAAI,wBAAU,+BAA2B,6BAAW,IAAI,CAAC,MAAM,MAAM,EAAC,QAAgB,CAAC;AAAA,IACjG;AAAA,EACJ;AACJ;","names":[]}
@@ -1,4 +1,5 @@
1
- import { TypeSchema } from './schema';
1
+ import { TypeSchema } from './schema.js';
2
+
2
3
  interface Format {
3
4
  (value: string): boolean;
4
5
  }
@@ -12,11 +13,12 @@ type StringDefinition = {
12
13
  pattern?: RegExp;
13
14
  format?: keyof typeof FORMAT;
14
15
  };
15
- export declare class StringType implements TypeSchema {
16
+ declare class StringType implements TypeSchema {
16
17
  private readonly definition;
17
18
  constructor(definition?: Partial<StringDefinition>);
18
19
  getTypes(): string[];
19
20
  isValidType(value: unknown): value is string;
20
21
  validate(value: unknown, path?: string[]): void;
21
22
  }
22
- export {};
23
+
24
+ export { StringType };
@@ -1,79 +1,83 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StringType = void 0;
4
- const schema_1 = require("./schema");
5
- const violation_1 = require("./violation");
1
+ import { Violation } from "./schema";
2
+ import { describe, formatPath } from "./violation";
6
3
  const FORMAT = {
7
- pointer: function pointer(value) {
8
- return /^(\.|([a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])(\.[a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])*)$/.test(value);
9
- },
10
- identifier: function identifier(value) {
11
- return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);
12
- },
13
- uuid: function uuid(value) {
14
- return /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(value);
15
- },
16
- date: function date(value) {
17
- return /^(\d\d\d\d)-(\d\d)-(\d\d)$/.test(value);
18
- },
19
- url: function url(value) {
20
- try {
21
- // eslint-disable-next-line no-new -- Easier way to validate a URL
22
- new URL(value);
23
- }
24
- catch {
25
- return false;
26
- }
27
- return true;
28
- },
4
+ pointer: function pointer(value) {
5
+ return /^(\.|([a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])(\.[a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])*)$/.test(value);
6
+ },
7
+ identifier: function identifier(value) {
8
+ return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);
9
+ },
10
+ uuid: function uuid(value) {
11
+ return /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(value);
12
+ },
13
+ date: function date(value) {
14
+ return /^(\d\d\d\d)-(\d\d)-(\d\d)$/.test(value);
15
+ },
16
+ url: function url(value) {
17
+ try {
18
+ new URL(value);
19
+ } catch {
20
+ return false;
21
+ }
22
+ return true;
23
+ }
29
24
  };
30
25
  class StringType {
31
- constructor(definition = {}) {
32
- var _a, _b, _c;
33
- this.definition = {
34
- ...definition,
35
- minLength: (_a = definition.minLength) !== null && _a !== void 0 ? _a : -1,
36
- maxLength: (_b = definition.maxLength) !== null && _b !== void 0 ? _b : -1,
37
- enumeration: (_c = definition.enumeration) !== null && _c !== void 0 ? _c : [],
38
- };
26
+ constructor(definition = {}) {
27
+ this.definition = {
28
+ ...definition,
29
+ minLength: definition.minLength ?? -1,
30
+ maxLength: definition.maxLength ?? -1,
31
+ enumeration: definition.enumeration ?? []
32
+ };
33
+ }
34
+ getTypes() {
35
+ return ["string"];
36
+ }
37
+ isValidType(value) {
38
+ return typeof value === "string";
39
+ }
40
+ validate(value, path = []) {
41
+ if (!this.isValidType(value)) {
42
+ throw new Violation(
43
+ `Expected value of type string at path '${formatPath(path)}', actual ${describe(value)}.`,
44
+ path,
45
+ { type: "string" }
46
+ );
47
+ }
48
+ const { minLength, maxLength } = this.definition;
49
+ if (minLength >= 0 && minLength > value.length) {
50
+ throw new Violation(
51
+ `Expected ${minLength === maxLength ? "exactly" : "at least"} ${minLength} ${minLength === 1 ? "character" : "characters"} at path '${formatPath(path)}', actual ${value.length}.`,
52
+ path,
53
+ { limit: minLength }
54
+ );
39
55
  }
40
- getTypes() {
41
- return ['string'];
56
+ if (maxLength >= 0 && maxLength < value.length) {
57
+ throw new Violation(
58
+ `Expected ${minLength === maxLength ? "exactly" : "at most"} ${maxLength} ${maxLength === 1 ? "character" : "characters"} at path '${formatPath(path)}', actual ${value.length}.`,
59
+ path,
60
+ { limit: maxLength }
61
+ );
42
62
  }
43
- isValidType(value) {
44
- return typeof value === 'string';
63
+ const { enumeration } = this.definition;
64
+ if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {
65
+ throw new Violation(
66
+ `Unexpected value at path '${formatPath(path)}', expecting '${enumeration.length === 1 ? enumeration[0] : `${enumeration.slice(0, -1).join("', '")}' or '${enumeration.slice(-1)}`}', found '${value}'.`,
67
+ path,
68
+ { enumeration }
69
+ );
45
70
  }
46
- validate(value, path = []) {
47
- if (!this.isValidType(value)) {
48
- throw new schema_1.Violation(`Expected value of type string at path '${(0, violation_1.formatPath)(path)}', actual ${(0, violation_1.describe)(value)}.`, path, { type: 'string' });
49
- }
50
- const { minLength, maxLength } = this.definition;
51
- if (minLength >= 0 && minLength > value.length) {
52
- throw new schema_1.Violation(`Expected ${minLength === maxLength ? 'exactly' : 'at least'} `
53
- + `${minLength} ${minLength === 1 ? 'character' : 'characters'} `
54
- + `at path '${(0, violation_1.formatPath)(path)}', actual ${value.length}.`, path, { limit: minLength });
55
- }
56
- if (maxLength >= 0 && maxLength < value.length) {
57
- throw new schema_1.Violation(`Expected ${minLength === maxLength ? 'exactly' : 'at most'} `
58
- + `${maxLength} ${maxLength === 1 ? 'character' : 'characters'} `
59
- + `at path '${(0, violation_1.formatPath)(path)}', actual ${value.length}.`, path, { limit: maxLength });
60
- }
61
- const { enumeration } = this.definition;
62
- if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {
63
- throw new schema_1.Violation(`Unexpected value at path '${(0, violation_1.formatPath)(path)}', expecting `
64
- + `'${enumeration.length === 1
65
- ? enumeration[0]
66
- : `${enumeration.slice(0, -1).join('\', \'')}' or '${enumeration.slice(-1)}`}', `
67
- + `found '${value}'.`, path, { enumeration: enumeration });
68
- }
69
- const { format, pattern } = this.definition;
70
- if (format !== undefined && !FORMAT[format](value)) {
71
- throw new schema_1.Violation(`Invalid ${format} format at path '${(0, violation_1.formatPath)(path)}'.`, path, { format: format });
72
- }
73
- if (pattern !== undefined && !pattern.test(value)) {
74
- throw new schema_1.Violation(`Invalid format at path '${(0, violation_1.formatPath)(path)}'.`, path, { pattern: pattern });
75
- }
71
+ const { format, pattern } = this.definition;
72
+ if (format !== void 0 && !FORMAT[format](value)) {
73
+ throw new Violation(`Invalid ${format} format at path '${formatPath(path)}'.`, path, { format });
76
74
  }
75
+ if (pattern !== void 0 && !pattern.test(value)) {
76
+ throw new Violation(`Invalid format at path '${formatPath(path)}'.`, path, { pattern });
77
+ }
78
+ }
77
79
  }
78
- exports.StringType = StringType;
80
+ export {
81
+ StringType
82
+ };
79
83
  //# sourceMappingURL=stringType.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"stringType.js","sourceRoot":"","sources":["../src/validation/stringType.ts"],"names":[],"mappings":";;;AAAA,qCAA+C;AAC/C,2CAAiD;AAMjD,MAAM,MAAM,GAA4B;IACpC,OAAO,EAAE,SAAS,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,GAAG,CAAC,EAAE,CAAC;YAC3D,MAAM,IAAI,kBAAS,CACf,6BAA6B,IAAA,sBAAU,EAAC,IAAI,CAAC,eAAe;kBAC1D,IACE,WAAW,CAAC,MAAM,KAAK,CAAC;oBACpB,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC;oBAChB,CAAC,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAClF,KAAK;kBACH,UAAU,KAAK,IAAI,EACrB,IAAI,EACJ,EAAC,WAAW,EAAE,WAAW,EAAC,CAC7B,CAAC;QACN,CAAC;QAED,MAAM,EAAC,MAAM,EAAE,OAAO,EAAC,GAAG,IAAI,CAAC,UAAU,CAAC;QAE1C,IAAI,MAAM,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;YACjD,MAAM,IAAI,kBAAS,CAAC,WAAW,MAAM,oBAAoB,IAAA,sBAAU,EAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,EAAC,MAAM,EAAE,MAAM,EAAC,CAAC,CAAC;QAC3G,CAAC;QAED,IAAI,OAAO,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAChD,MAAM,IAAI,kBAAS,CAAC,2BAA2B,IAAA,sBAAU,EAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,EAAC,OAAO,EAAE,OAAO,EAAC,CAAC,CAAC;QACnG,CAAC;IACL,CAAC;CACJ;AA7ED,gCA6EC"}
1
+ {"version":3,"sources":["../src/validation/stringType.ts"],"sourcesContent":["import {TypeSchema, Violation} from './schema';\nimport {describe, formatPath} from './violation';\n\ninterface Format {\n (value: string): boolean;\n}\n\nconst FORMAT: {[key: string]: Format} = {\n pointer: function pointer(value: string): boolean {\n return /^(\\.|([a-zA-Z_][a-zA-Z0-9_]*|\\[[0-9]+])(\\.[a-zA-Z_][a-zA-Z0-9_]*|\\[[0-9]+])*)$/.test(value);\n },\n identifier: function identifier(value: string): boolean {\n return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);\n },\n uuid: function uuid(value: string): boolean {\n return /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(value);\n },\n date: function date(value: string): boolean {\n return /^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)$/.test(value);\n },\n url: function url(value: string): boolean {\n try {\n // eslint-disable-next-line no-new -- Easier way to validate a URL\n new URL(value);\n } catch {\n return false;\n }\n\n return true;\n },\n};\n\ntype StringDefinition = {\n minLength: number,\n maxLength: number,\n enumeration: string[],\n pattern?: RegExp,\n format?: keyof typeof FORMAT,\n};\n\nexport class StringType implements TypeSchema {\n private readonly definition: StringDefinition;\n\n public constructor(definition: Partial<StringDefinition> = {}) {\n this.definition = {\n ...definition,\n minLength: definition.minLength ?? -1,\n maxLength: definition.maxLength ?? -1,\n enumeration: definition.enumeration ?? [],\n };\n }\n\n public getTypes(): string[] {\n return ['string'];\n }\n\n public isValidType(value: unknown): value is string {\n return typeof value === 'string';\n }\n\n public validate(value: unknown, path: string[] = []): void {\n if (!this.isValidType(value)) {\n throw new Violation(\n `Expected value of type string at path '${formatPath(path)}', actual ${describe(value)}.`,\n path,\n {type: 'string'},\n );\n }\n\n const {minLength, maxLength} = this.definition;\n\n if (minLength >= 0 && minLength > value.length) {\n throw new Violation(\n `Expected ${minLength === maxLength ? 'exactly' : 'at least'} `\n + `${minLength} ${minLength === 1 ? 'character' : 'characters'} `\n + `at path '${formatPath(path)}', actual ${value.length}.`,\n path,\n {limit: minLength},\n );\n }\n\n if (maxLength >= 0 && maxLength < value.length) {\n throw new Violation(\n `Expected ${minLength === maxLength ? 'exactly' : 'at most'} `\n + `${maxLength} ${maxLength === 1 ? 'character' : 'characters'} `\n + `at path '${formatPath(path)}', actual ${value.length}.`,\n path,\n {limit: maxLength},\n );\n }\n\n const {enumeration} = this.definition;\n\n if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {\n throw new Violation(\n `Unexpected value at path '${formatPath(path)}', expecting `\n + `'${\n enumeration.length === 1\n ? enumeration[0]\n : `${enumeration.slice(0, -1).join('\\', \\'')}' or '${enumeration.slice(-1)}`\n }', `\n + `found '${value}'.`,\n path,\n {enumeration: enumeration},\n );\n }\n\n const {format, pattern} = this.definition;\n\n if (format !== undefined && !FORMAT[format](value)) {\n throw new Violation(`Invalid ${format} format at path '${formatPath(path)}'.`, path, {format: format});\n }\n\n if (pattern !== undefined && !pattern.test(value)) {\n throw new Violation(`Invalid format at path '${formatPath(path)}'.`, path, {pattern: pattern});\n }\n }\n}\n"],"mappings":"AAAA,SAAoB,iBAAgB;AACpC,SAAQ,UAAU,kBAAiB;AAMnC,MAAM,SAAkC;AAAA,EACpC,SAAS,SAAS,QAAQ,OAAwB;AAC9C,WAAO,iFAAiF,KAAK,KAAK;AAAA,EACtG;AAAA,EACA,YAAY,SAAS,WAAW,OAAwB;AACpD,WAAO,2BAA2B,KAAK,KAAK;AAAA,EAChD;AAAA,EACA,MAAM,SAAS,KAAK,OAAwB;AACxC,WAAO,gDAAgD,KAAK,KAAK;AAAA,EACrE;AAAA,EACA,MAAM,SAAS,KAAK,OAAwB;AACxC,WAAO,6BAA6B,KAAK,KAAK;AAAA,EAClD;AAAA,EACA,KAAK,SAAS,IAAI,OAAwB;AACtC,QAAI;AAEA,UAAI,IAAI,KAAK;AAAA,IACjB,QAAQ;AACJ,aAAO;AAAA,IACX;AAEA,WAAO;AAAA,EACX;AACJ;AAUO,MAAM,WAAiC;AAAA,EAGnC,YAAY,aAAwC,CAAC,GAAG;AAC3D,SAAK,aAAa;AAAA,MACd,GAAG;AAAA,MACH,WAAW,WAAW,aAAa;AAAA,MACnC,WAAW,WAAW,aAAa;AAAA,MACnC,aAAa,WAAW,eAAe,CAAC;AAA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jB;AAAA,QACA,EAAC,YAAwB;AAAA,MAC7B;AAAA,IACJ;AAEA,UAAM,EAAC,QAAQ,QAAO,IAAI,KAAK;AAE/B,QAAI,WAAW,UAAa,CAAC,OAAO,MAAM,EAAE,KAAK,GAAG;AAChD,YAAM,IAAI,UAAU,WAAW,MAAM,oBAAoB,WAAW,IAAI,CAAC,MAAM,MAAM,EAAC,OAAc,CAAC;AAAA,IACzG;AAEA,QAAI,YAAY,UAAa,CAAC,QAAQ,KAAK,KAAK,GAAG;AAC/C,YAAM,IAAI,UAAU,2BAA2B,WAAW,IAAI,CAAC,MAAM,MAAM,EAAC,QAAgB,CAAC;AAAA,IACjG;AAAA,EACJ;AACJ;","names":[]}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/validation/unionType.ts"],"sourcesContent":["import {TypeSchema, Violation} from './schema';\nimport {describe, formatPath} from './violation';\n\nexport class UnionType implements TypeSchema {\n private readonly schemas: TypeSchema[];\n\n public constructor(first: TypeSchema, second: TypeSchema, ...others: TypeSchema[]) {\n this.schemas = [first, second, ...others];\n }\n\n public getTypes(): string[] {\n const types = [];\n\n for (const schema of this.schemas) {\n for (const type of schema.getTypes()) {\n if (types.indexOf(type) < 0) {\n types.push(type);\n }\n }\n }\n\n return types;\n }\n\n public isValidType(value: unknown): boolean {\n for (const schema of this.schemas) {\n if (schema.isValidType(value)) {\n return true;\n }\n }\n\n return false;\n }\n\n public validate(value: unknown, path: string[] = []): void {\n for (const schema of this.schemas) {\n if (schema.isValidType(value)) {\n schema.validate(value, path);\n\n return;\n }\n }\n\n const types = this.getTypes();\n\n throw new Violation(\n `Expected value of type ${types.slice(0, -1).join(', ')} or ${types[types.length - 1]} `\n + `at path '${formatPath(path)}', actual ${describe(value)}.`,\n path,\n {type: types.join('|')},\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoC;AACpC,uBAAmC;AAE5B,MAAM,UAAgC;AAAA,EAGlC,YAAY,OAAmB,WAAuB,QAAsB;AAC/E,SAAK,UAAU,CAAC,OAAO,QAAQ,GAAG,MAAM;AAAA,EAC5C;AAAA,EAEO,WAAqB;AACxB,UAAM,QAAQ,CAAC;AAEf,eAAW,UAAU,KAAK,SAAS;AAC/B,iBAAW,QAAQ,OAAO,SAAS,GAAG;AAClC,YAAI,MAAM,QAAQ,IAAI,IAAI,GAAG;AACzB,gBAAM,KAAK,IAAI;AAAA,QACnB;AAAA,MACJ;AAAA,IACJ;AAEA,WAAO;AAAA,EACX;AAAA,EAEO,YAAY,OAAyB;AACxC,eAAW,UAAU,KAAK,SAAS;AAC/B,UAAI,OAAO,YAAY,KAAK,GAAG;AAC3B,eAAO;AAAA,MACX;AAAA,IACJ;AAEA,WAAO;AAAA,EACX;AAAA,EAEO,SAAS,OAAgB,OAAiB,CAAC,GAAS;AACvD,eAAW,UAAU,KAAK,SAAS;AAC/B,UAAI,OAAO,YAAY,KAAK,GAAG;AAC3B,eAAO,SAAS,OAAO,IAAI;AAE3B;AAAA,MACJ;AAAA,IACJ;AAEA,UAAM,QAAQ,KAAK,SAAS;AAE5B,UAAM,IAAI;AAAA,MACN,0BAA0B,MAAM,MAAM,GAAG,EAAE,EAAE,KAAK,IAAI,CAAC,OAAO,MAAM,MAAM,SAAS,CAAC,CAAC,iBACvE,6BAAW,IAAI,CAAC,iBAAa,2BAAS,KAAK,CAAC;AAAA,MAC1D;AAAA,MACA,EAAC,MAAM,MAAM,KAAK,GAAG,EAAC;AAAA,IAC1B;AAAA,EACJ;AACJ;","names":[]}
@@ -1,8 +1,11 @@
1
- import { TypeSchema } from './schema';
2
- export declare class UnionType implements TypeSchema {
1
+ import { TypeSchema } from './schema.js';
2
+
3
+ declare class UnionType implements TypeSchema {
3
4
  private readonly schemas;
4
5
  constructor(first: TypeSchema, second: TypeSchema, ...others: TypeSchema[]);
5
6
  getTypes(): string[];
6
7
  isValidType(value: unknown): boolean;
7
8
  validate(value: unknown, path?: string[]): void;
8
9
  }
10
+
11
+ export { UnionType };
@@ -1,42 +1,44 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.UnionType = void 0;
4
- const schema_1 = require("./schema");
5
- const violation_1 = require("./violation");
1
+ import { Violation } from "./schema";
2
+ import { describe, formatPath } from "./violation";
6
3
  class UnionType {
7
- constructor(first, second, ...others) {
8
- this.schemas = [first, second, ...others];
9
- }
10
- getTypes() {
11
- const types = [];
12
- for (const schema of this.schemas) {
13
- for (const type of schema.getTypes()) {
14
- if (types.indexOf(type) < 0) {
15
- types.push(type);
16
- }
17
- }
4
+ constructor(first, second, ...others) {
5
+ this.schemas = [first, second, ...others];
6
+ }
7
+ getTypes() {
8
+ const types = [];
9
+ for (const schema of this.schemas) {
10
+ for (const type of schema.getTypes()) {
11
+ if (types.indexOf(type) < 0) {
12
+ types.push(type);
18
13
  }
19
- return types;
14
+ }
20
15
  }
21
- isValidType(value) {
22
- for (const schema of this.schemas) {
23
- if (schema.isValidType(value)) {
24
- return true;
25
- }
26
- }
27
- return false;
16
+ return types;
17
+ }
18
+ isValidType(value) {
19
+ for (const schema of this.schemas) {
20
+ if (schema.isValidType(value)) {
21
+ return true;
22
+ }
28
23
  }
29
- validate(value, path = []) {
30
- for (const schema of this.schemas) {
31
- if (schema.isValidType(value)) {
32
- schema.validate(value, path);
33
- return;
34
- }
35
- }
36
- const types = this.getTypes();
37
- throw new schema_1.Violation(`Expected value of type ${types.slice(0, -1).join(', ')} or ${types[types.length - 1]} `
38
- + `at path '${(0, violation_1.formatPath)(path)}', actual ${(0, violation_1.describe)(value)}.`, path, { type: types.join('|') });
24
+ return false;
25
+ }
26
+ validate(value, path = []) {
27
+ for (const schema of this.schemas) {
28
+ if (schema.isValidType(value)) {
29
+ schema.validate(value, path);
30
+ return;
31
+ }
39
32
  }
33
+ const types = this.getTypes();
34
+ throw new Violation(
35
+ `Expected value of type ${types.slice(0, -1).join(", ")} or ${types[types.length - 1]} at path '${formatPath(path)}', actual ${describe(value)}.`,
36
+ path,
37
+ { type: types.join("|") }
38
+ );
39
+ }
40
40
  }
41
- exports.UnionType = UnionType;
41
+ export {
42
+ UnionType
43
+ };
42
44
  //# sourceMappingURL=unionType.js.map