# ModelStrict Decorator endpoint converters deprecated
Module | import { ModelStrict } from "@tsed/common" |
---|---|
Source | /packages/common/src/converters/decorators/modelStrict.ts |
# Overview
function ModelStrict(value: boolean): (...parameters: any[]) => any;
# Description
Change the default behavior when the converters deserialize/serialize your model.
# validationModelStrict options
When validationModelStrict
is true
, the converters will check the model consistency. For example, when a property is unknown
on the object sent in the request, Converters will throw a BadRequest
because the property doesn't exists on the defined Model.
Example:
import {InjectorService, ConvertersService, Required, Property} from "@tsed/common";
const injector = new InjectorService()
injector.load();
class TaskModel {
@Required()
subject: string;
@Property()
rate: number;
}
const convertersService = injector.get(ConvertersService);
convertersService.validationModelStrict = true;
convertersService.deserialize({unknowProperty: "test"}, TaskModel); // BadRequest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
It's possible to disable this behavior for a specific Model with the @ModelStrict
decorator.
Example:
import {InjectorService, ConvertersService, ModelStrict, Required, Property} from "@tsed/common";
const injector = new InjectorService()
injector.load();
@ModelStrict(false)
class TaskModel {
@Required()
subject: string;
@Property()
rate: number;
[key: string]: any; // recommended
}
const convertersService = injector.get(ConvertersService);
convertersService.validationModelStrict = true;
const result = convertersService.deserialize({unknownProperty: "test"}, TaskModel);
console.log(result) // TaskModel {unknownProperty: "test"}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
If the validationModelStrict is false, you can use
@ModelStrict
decorator to enable the strict validation for a specific model.
- Session & cookies
- Passport.js
- TypeORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Throw HTTP Exceptions
- Customize 404
- AWS
- Jest
- Seq
- Controllers
- Providers
- Model
- Converters
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Injection scopes
- Custom providers
- Custom endpoint decorator
- Testing