diff --git a/src/index.ts b/src/index.ts index 813607597d..60af8c55d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,6 +49,29 @@ export function validate(schemaNameOrObject: Object|string, } } +/** + * Validates given object and reject on error. + */ +export function validateOrReject(object: Object, validatorOptions?: ValidatorOptions): Promise; + +/** + * Validates given object by a given validation schema and reject on error. + */ +export function validateOrReject(schemaName: string, object: Object, validatorOptions?: ValidatorOptions): Promise; + +/** + * Validates given object by object's decorators or given validation schema and reject on error. + */ +export function validateOrReject(schemaNameOrObject: Object|string, + objectOrValidationOptions?: Object|ValidatorOptions, + maybeValidatorOptions?: ValidatorOptions): Promise { + if (typeof schemaNameOrObject === "string") { + return getFromContainer(Validator).validateOrReject(schemaNameOrObject as string, objectOrValidationOptions as Object, maybeValidatorOptions); + } else { + return getFromContainer(Validator).validateOrReject(schemaNameOrObject as Object, objectOrValidationOptions as ValidatorOptions); + } +} + /** * Performs sync validation of the given object. * Note that this method completely ignores async validations. diff --git a/src/validation/Validator.ts b/src/validation/Validator.ts index e8de957cf5..fc5f691f7c 100644 --- a/src/validation/Validator.ts +++ b/src/validation/Validator.ts @@ -17,6 +17,24 @@ export class Validator { private validatorJs = require("validator"); + /** + * Performs validation of the given object based on decorators or validation schema. + * Common method for `validateOrReject` and `validate` methods. + */ + private coreValidate(objectOrSchemaName: Object|string, objectOrValidationOptions: Object|ValidationOptions, maybeValidatorOptions?: ValidatorOptions): Promise { + const object = typeof objectOrSchemaName === "string" ? objectOrValidationOptions as Object : objectOrSchemaName as Object; + const options = typeof objectOrSchemaName === "string" ? maybeValidatorOptions : objectOrValidationOptions as ValidationOptions; + const schema = typeof objectOrSchemaName === "string" ? objectOrSchemaName as string : undefined; + + const executor = new ValidationExecutor(this, options); + const validationErrors: ValidationError[] = []; + executor.execute(object, schema, validationErrors); + + return Promise.all(executor.awaitingPromises).then(() => { + return executor.stripEmptyErrors(validationErrors); + }); + } + // ------------------------------------------------------------------------- // Public Methods // ------------------------------------------------------------------------- @@ -35,17 +53,25 @@ export class Validator { * Performs validation of the given object based on decorators or validation schema. */ validate(objectOrSchemaName: Object|string, objectOrValidationOptions: Object|ValidationOptions, maybeValidatorOptions?: ValidatorOptions): Promise { - const object = typeof objectOrSchemaName === "string" ? objectOrValidationOptions as Object : objectOrSchemaName as Object; - const options = typeof objectOrSchemaName === "string" ? maybeValidatorOptions : objectOrValidationOptions as ValidationOptions; - const schema = typeof objectOrSchemaName === "string" ? objectOrSchemaName as string : undefined; + return this.coreValidate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions); + } - const executor = new ValidationExecutor(this, options); - const validationErrors: ValidationError[] = []; - executor.execute(object, schema, validationErrors); + /** + * Performs validation of the given object based on decorators used in given object class and reject on error. + */ + validateOrReject(object: Object, options?: ValidatorOptions): Promise; - return Promise.all(executor.awaitingPromises).then(() => { - return executor.stripEmptyErrors(validationErrors); - }); + /** + * Performs validation of the given object based on validation schema and reject on error. + */ + validateOrReject(schemaName: string, object: Object, options?: ValidatorOptions): Promise; + + /** + * Performs validation of the given object based on decorators or validation schema and reject on error. + */ + validateOrReject(objectOrSchemaName: Object|string, objectOrValidationOptions: Object|ValidationOptions, maybeValidatorOptions?: ValidatorOptions): Promise { + return this.coreValidate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) + .then((validationErorrs: ValidationError[]) => (validationErorrs.length > 0) ? Promise.reject(validationErorrs) : Promise.resolve()); } /** diff --git a/test/functional/reject-validation.spec.ts b/test/functional/reject-validation.spec.ts new file mode 100644 index 0000000000..71dbb1ad90 --- /dev/null +++ b/test/functional/reject-validation.spec.ts @@ -0,0 +1,48 @@ +import "es6-shim"; + +import { ValidationError } from "./../../src/validation/ValidationError"; +import { Contains, MinLength } from "../../src/decorator/decorators"; +import { Validator } from "../../src/validation/Validator"; +import { expect } from "chai"; + +class MyClass { + @Contains("hello", { + message: "$value is not valid. Your string must contain a hello word" + }) + someProperty: string; +} + +describe("validateOrReject()", () => { + let validator: Validator; + let model: MyClass; + + beforeEach(() => { + validator = new Validator(); + model = new MyClass(); + }); + + it("should resolve promise when no error", (done) => { + model.someProperty = "hello world"; + validator.validateOrReject(model) + .then((args) => { + expect(args).to.not.exist; + done(); + }) + .catch((errors) => { + done("should resolve promise"); + }); + }); + + it("should reject promise on error", (done) => { + model.someProperty = "hell no world"; + validator.validateOrReject(model) + .then(() => { + done("should reject promise"); + }) + .catch((errors: ValidationError[]) => { + expect(errors).to.have.lengthOf(1); + expect(errors[0].constraints).to.deep.equal({ contains: "hell no world is not valid. Your string must contain a hello word" }); + done(); + }); + }); +});