Skip to content

Commit 59eac09

Browse files
andrewwa-kainosvlapo
authored andcommitted
feat: replace instanceof Promise and support Promise/A+ (#310)
1 parent 35ec04d commit 59eac09

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎src/utils.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// https://github.com/TylorS/typed-is-promise/blob/abf1514e1b6961adfc75765476b0debb96b2c3ae/src/index.ts
2+
3+
export function isPromise<T = any>(p: any): p is Promise<T> {
4+
return p !== null && typeof p === "object" && typeof p.then === "function";
5+
}

‎src/validation/ValidationExecutor.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {ValidationTypes} from "./ValidationTypes";
88
import {ConstraintMetadata} from "../metadata/ConstraintMetadata";
99
import {ValidationArguments} from "./ValidationArguments";
1010
import {ValidationUtils} from "./ValidationUtils";
11+
import {isPromise} from "../utils";
1112

1213
/**
1314
* Executes validation over given object.
@@ -43,7 +44,7 @@ export class ValidationExecutor {
4344
/**
4445
* If there is no metadata registered it means possibly the dependencies are not flatterned and
4546
* more than one instance is used.
46-
*
47+
*
4748
* TODO: This needs proper handling, forcing to use the same container or some other proper solution.
4849
*/
4950
if (!this.metadataStorage.hasValidationMetaData) {
@@ -250,7 +251,7 @@ export class ValidationExecutor {
250251
constraints: metadata.constraints
251252
};
252253
const validatedValue = customConstraintMetadata.instance.validate(value, validationArguments);
253-
if (validatedValue instanceof Promise) {
254+
if (isPromise(validatedValue)) {
254255
const promise = validatedValue.then(isValid => {
255256
if (!isValid) {
256257
const [type, message] = this.createValidationError(object, value, metadata, customConstraintMetadata);

0 commit comments

Comments
 (0)