Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;

/**
* Validates given object by a given validation schema and reject on error.
*/
export function validateOrReject(schemaName: string, object: Object, validatorOptions?: ValidatorOptions): Promise<void>;

/**
* 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<void> {
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.
Expand Down
44 changes: 35 additions & 9 deletions src/validation/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationError[]> {
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
// -------------------------------------------------------------------------
Expand All @@ -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<ValidationError[]> {
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<void>;

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<void>;

/**
* 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<void> {
return this.coreValidate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions)
.then((validationErorrs: ValidationError[]) => (validationErorrs.length > 0) ? Promise.reject(validationErorrs) : Promise.resolve());
}

/**
Expand Down
48 changes: 48 additions & 0 deletions test/functional/reject-validation.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});