Skip to content

Commit

Permalink
feat: replace instanceof Promise and support Promise/A+ (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewwa-kainos authored and vlapo committed Jul 25, 2019
1 parent 35ec04d commit 59eac09
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://github.com/TylorS/typed-is-promise/blob/abf1514e1b6961adfc75765476b0debb96b2c3ae/src/index.ts

export function isPromise<T = any>(p: any): p is Promise<T> {
return p !== null && typeof p === "object" && typeof p.then === "function";
}
5 changes: 3 additions & 2 deletions src/validation/ValidationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {ValidationTypes} from "./ValidationTypes";
import {ConstraintMetadata} from "../metadata/ConstraintMetadata";
import {ValidationArguments} from "./ValidationArguments";
import {ValidationUtils} from "./ValidationUtils";
import {isPromise} from "../utils";

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

0 comments on commit 59eac09

Please sign in to comment.