/
ignore.ts
69 lines (67 loc) · 1.4 KB
/
ignore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {IgnoreCallback} from "../../interfaces";
import {JsonEntityFn} from "./jsonEntityFn";
/**
* @ignore
*/
const defaultCB = (value: any, ctx: any) => ctx.mongoose !== true;
/**
* Ignore the property when JsonMapper serialize the class to a Plain Object JavaScript.
*
* ::: warning
* Swagger will not generate documentation for the ignored property.
* :::
*
* ```typescript
* class User {
* @Ignore()
* _id: string;
*
* @Property()
* firstName: string;
*
* @Property()
* lastName: string;
*
* @Ignore((value, ctx) => !ctx.mongoose) // don't ignore prop only if mongoose
* password: string;
* }
* ```
*
* The controller:
* ```typescript
* import {Post, Controller, BodyParams} from "@tsed/common";
* import {Person} from "../models/Person";
*
* @Controller("/")
* export class UsersCtrl {
* @Get("/")
* get(): User {
* const user = new User();
* user._id = "12345";
* user.firstName = "John";
* user.lastName = "Doe";
* user.password = "secretpassword";
* return
* }
* }
* ```
*
* The expected json object:
*
* ```json
* {
* "firstName": "John",
* "lastName": "Doe"
* }
* ```
* @param cb Callback to know if the property must be ignored
* @decorator
* @validation
* @swagger
* @schema
*/
export function Ignore(cb: boolean | IgnoreCallback = defaultCB) {
return JsonEntityFn((store) => {
store.schema.ignore(cb);
});
}