# ModelStrict Decorator

Module
import { ModelStrict } from "@tsed/common"
Source/packages/common/src/converters/decorators/modelStrict.ts

# Overview

function ModelStrict(value: boolean): Function;

# 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

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({unknowProperty: "test"}, TaskModel);
console.log(result) // TaskModel {unknowProperty: "test"}
1
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.