UNPKG

2.47 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AnyPredicate = void 0;
4const argument_error_1 = require("../argument-error");
5const base_predicate_1 = require("./base-predicate");
6const generate_argument_error_message_1 = require("../utils/generate-argument-error-message");
7/**
8@hidden
9*/
10class AnyPredicate {
11 constructor(predicates, options = {}) {
12 Object.defineProperty(this, "predicates", {
13 enumerable: true,
14 configurable: true,
15 writable: true,
16 value: predicates
17 });
18 Object.defineProperty(this, "options", {
19 enumerable: true,
20 configurable: true,
21 writable: true,
22 value: options
23 });
24 }
25 [base_predicate_1.testSymbol](value, main, label, stack) {
26 const errors = new Map();
27 for (const predicate of this.predicates) {
28 try {
29 main(value, label, predicate, stack);
30 return;
31 }
32 catch (error) {
33 if (value === undefined && this.options.optional === true) {
34 return;
35 }
36 // If we received an ArgumentError, then..
37 if (error instanceof argument_error_1.ArgumentError) {
38 // Iterate through every error reported.
39 for (const [key, value] of error.validationErrors.entries()) {
40 // Get the current errors set, if any.
41 const alreadyPresent = errors.get(key);
42 // If they are present already, create a unique set with both current and new values.
43 if (alreadyPresent) {
44 errors.set(key, [...new Set([...alreadyPresent, ...value])]);
45 }
46 else {
47 // Add the errors found as is to the map.
48 errors.set(key, value);
49 }
50 }
51 }
52 }
53 }
54 if (errors.size > 0) {
55 // Generate the `error.message` property.
56 const message = generate_argument_error_message_1.generateArgumentErrorMessage(errors, true);
57 throw new argument_error_1.ArgumentError(`Any predicate failed with the following errors:\n${message}`, main, stack, errors);
58 }
59 }
60}
61exports.AnyPredicate = AnyPredicate;