UNPKG

1.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.CustomValidation = void 0;
4class CustomValidation {
5 constructor(validator, negated) {
6 this.validator = validator;
7 this.negated = negated;
8 }
9 async run(context, value, meta) {
10 try {
11 const result = this.validator(value, meta);
12 const actualResult = await result;
13 const isPromise = result && result.then;
14 const failed = this.negated ? actualResult : !actualResult;
15 // A promise that was resolved only adds an error if negated.
16 // Otherwise it always suceeds
17 if ((!isPromise && failed) || (isPromise && this.negated)) {
18 context.addError(this.message, value, meta);
19 }
20 }
21 catch (err) {
22 if (this.negated) {
23 return;
24 }
25 context.addError((err instanceof Error ? err.message : err) || this.message, value, meta);
26 }
27 }
28}
29exports.CustomValidation = CustomValidation;