From e55d8ed344f2c3d29691fd121e50483df98ced5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=5BDesktop=5D?= Date: Sun, 16 Oct 2016 14:34:29 +0200 Subject: [PATCH] Added rejecting validation. --- src/validation/Validator.ts | 28 ++++++++++++ test/functional/rejecting-validation.spec.ts | 48 ++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 test/functional/rejecting-validation.spec.ts diff --git a/src/validation/Validator.ts b/src/validation/Validator.ts index e8de957cf5..173f8d5795 100644 --- a/src/validation/Validator.ts +++ b/src/validation/Validator.ts @@ -47,6 +47,34 @@ export class Validator { return executor.stripEmptyErrors(validationErrors); }); } + + /** + * Performs validation of the given object based on decorators used in given object class. + */ + restrictValidate(object: Object, options?: ValidatorOptions): Promise; + + /** + * Performs validation of the given object based on validation schema. + */ + restrictValidate(schemaName: string, object: Object, options?: ValidatorOptions): Promise; + + /** + * Performs validation of the given object based on decorators or validation schema. + */ + restrictValidate(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(() => { + const errors = executor.stripEmptyErrors(validationErrors); + return errors.length ? Promise.reject(errors) : Promise.resolve(); + }); + } /** * Performs validation of the given object based on decorators used in given object class. diff --git a/test/functional/rejecting-validation.spec.ts b/test/functional/rejecting-validation.spec.ts new file mode 100644 index 0000000000..3004e2f3d2 --- /dev/null +++ b/test/functional/rejecting-validation.spec.ts @@ -0,0 +1,48 @@ +import "es6-shim"; +import { Contains, MinLength } from "../../src/decorator/decorators"; +import { Validator } from "../../src/validation/Validator"; +import { expect } from "chai"; + +const validator = new Validator(); + +class MyClass { + @Contains("hello", { + message: "$value is not valid. Your string must contain a hello word" + }) + someProperty: string; +} + +describe("restrictValidate()", () => { + 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.restrictValidate(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.restrictValidate(model) + .then(() => { + done("should reject promise"); + }) + .catch((errors) => { + 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(); + }); + }); +});