Skip to content

Allow setting class-validator options on a particular decorator. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# routing-controllers integration with TypeORM

This extension for TypeORM provides handy decorators that can be used with
This extension for TypeORM provides handy decorators that can be used with
[routing-controllers](https://github.com/pleerock/routing-controllers).

## Installation
Expand Down Expand Up @@ -157,6 +157,10 @@ for example if you are using array of entities it should be passed explicitly.

Property to find by. If not specified, then entity will be fetched by its primary keys.

* `property: validate`

`ValidatorOptions` from `class-validator` to use when validating the entity.

## Samples

Take a look on samples in [./sample](sample) for examples of usages.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"body-parser": "^1.18.2",
"chai": "^4.1.2",
"class-transformer": "^0.1.7",
"class-validator": "^0.8.1",
"del": "^3.0.0",
"express": "^4.15.5",
"gulp": "^3.9.1",
Expand Down
9 changes: 7 additions & 2 deletions sample/sample1-simple-usage/controller/PostController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Get, JsonController, Patch, Post as HttpPost} from "routing-controllers";
import {Get, JsonController, Patch, Post as HttpPost, Put} from "routing-controllers";
import {getConnectionManager, Repository} from "typeorm";
import {Post} from "../entity/Post";
import {EntityFromParam} from "../../../src/decorators/EntityFromParam";
Expand Down Expand Up @@ -29,4 +29,9 @@ export class PostController {
return this.postRepository.save([post1, post2]);
}

}
@Put("/posts/:id")
update(@EntityFromBody({validate: { skipOptionalProperties: true }}) post: Post) {
return this.postRepository.save(post);
}

}
3 changes: 2 additions & 1 deletion src/decorators/EntityFromBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function EntityFromBody(options?: EntityParamOptions) {
type: "body",
parse: options && options.parse,
required: options && options.required,
validate: options && options.validate,
transform: (action: Action, value: any) => {
const connection = getConnection(options ? options.connection : undefined);

Expand Down Expand Up @@ -49,4 +50,4 @@ export function EntityFromBody(options?: EntityParamOptions) {
}
});
};
}
}
3 changes: 2 additions & 1 deletion src/decorators/EntityFromBodyParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function EntityFromBodyParam(paramName: string, options?: EntityParamOpti
type: "body-param",
parse: options && options.parse,
required: options && options.required,
validate: options && options.validate,
transform: (actionProperties, value) => {
if (value === undefined || value === null) return undefined;

Expand Down Expand Up @@ -49,4 +50,4 @@ export function EntityFromBodyParam(paramName: string, options?: EntityParamOpti
}
});
};
}
}
1 change: 1 addition & 0 deletions src/decorators/EntityFromCookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function EntityFromCookie(paramName: string, options?: EntityParamOptions
type: "cookie",
parse: options && options.parse,
required: options && options.required,
validate: options && options.validate,
transform: (actionProperties, value) => entityTransform(value, target, isArray, options)
});
};
Expand Down
3 changes: 2 additions & 1 deletion src/decorators/EntityFromParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export function EntityFromParam(paramName: string, options?: EntityParamOptions)
type: "param",
parse: options && options.parse,
required: options && options.required,
validate: options && options.validate,
transform: (actionProperties, value) => entityTransform(value, target, isArray, options)
});
};
}
}
1 change: 1 addition & 0 deletions src/decorators/EntityFromQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function EntityFromQuery(paramName: string, options?: EntityParamOptions)
type: "query",
parse: options && options.parse,
required: options && options.required,
validate: options && options.validate,
transform: (actionProperties, value) => entityTransform(value, target, isArray, options)
});
};
Expand Down
8 changes: 8 additions & 0 deletions src/options/EntityParamOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ValidatorOptions } from "class-validator";

/**
* Entity transformation options.
*/
Expand Down Expand Up @@ -31,4 +33,10 @@ export interface EntityParamOptions {
*/
property?: string;

/**
* Options for use for class-validator when validating the entity.
*/

validate?: ValidatorOptions;

}