Skip to content

Commit

Permalink
Merge pull request #1 from Cognate/master
Browse files Browse the repository at this point in the history
fix: local option doesn't override all gloabl options
  • Loading branch information
nolazybits committed Dec 6, 2020
2 parents 2455688 + 3cc5c3f commit 22ff659
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ActionParameterHandler.ts
Expand Up @@ -232,7 +232,7 @@ export class ActionParameterHandler<T extends BaseDriver> {
&& (value instanceof paramMetadata.targetType);

if (isValidationEnabled && shouldValidate) {
const options = paramMetadata.validate instanceof Object ? paramMetadata.validate : this.driver.validationOptions;
const options = Object.assign({}, this.driver.validationOptions, paramMetadata.validate);
return validate(value, options)
.then(() => value)
.catch((validationErrors: ValidationError[]) => {
Expand Down
46 changes: 46 additions & 0 deletions test/functional/class-validator-options.spec.ts
Expand Up @@ -158,6 +158,52 @@ describe("parameters auto-validation", () => {
});
});

describe("should merge local validation options with global validation options prioritizing local", () => {

let requestFilter: any;
beforeEach(() => {
requestFilter = undefined;
});

before(() => {
getMetadataArgsStorage().reset();

@JsonController()
class ClassTransformUserController {

@Get("/user")
getUsers(@QueryParam("filter", { validate: { skipMissingProperties: false } }) filter: UserFilter): any {
requestFilter = filter;
const user = new UserModel();
user.id = 1;
user._firstName = "Umed";
user._lastName = "Khudoiberdiev";
return user;
}

}
});

const options: RoutingControllersOptions = {
validation: {
whitelist: true,
skipMissingProperties: true
}
};

let expressApp: any, koaApp: any;
before(done => expressApp = createExpressServer(options).listen(3001, done));
after(done => expressApp.close(done));
before(done => koaApp = createKoaServer(options).listen(3002, done));
after(done => koaApp.close(done));

assertRequest([3001, 3002], "get", "user?filter={\"keyword\": \"aValidKeyword\", \"notKeyword\": \"Um\", \"__somethingPrivate\": \"blablabla\"}", response => {
expect(response).to.have.status(200);
expect(requestFilter).to.have.property("keyword");
expect(requestFilter).to.not.have.property("notKeyword");
});
});

describe("should pass the valid param after validation", () => {

let requestFilter: any;
Expand Down

0 comments on commit 22ff659

Please sign in to comment.