Skip to content
Closed
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
28 changes: 28 additions & 0 deletions src/validation/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad name, we need to came up with better name, need to start name with validate*, and something like validateOrThrow() or better.


/**
* Performs validation of the given object based on validation schema.
*/
restrictValidate(schemaName: string, object: Object, options?: ValidatorOptions): Promise<void>;

/**
* Performs validation of the given object based on decorators or validation schema.
*/
restrictValidate(objectOrSchemaName: Object|string, objectOrValidationOptions: Object|ValidationOptions, maybeValidatorOptions?: ValidatorOptions): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to do it DRY. I think this should be easy as:

async restrictValidate(....) {
     const validationErorrs = await this.validate(...);
     if (validationErorrs.length > 0)
            return Promise.reject(validationErorrs);

     return validationErorrs;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not that simple - when you have overload signatures, you can use only them, not the 3rd function validate(objectOrSchemaName: Object|string, objectOrValidationOptions: Object|ValidationOptions, maybeValidatorOptions?: ValidatorOptions). I will move the implementation to the private core method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah I hate this problem

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