# Required Decorator

Module
import { Required } from "@tsed/common"
Source/packages/common/src/mvc/decorators/required.ts

# Overview

function Required(...allowedRequiredValues: any[]): any;

# Description

Add required annotation for a function argument.

The @Required decorator can be used on two cases.

To decorate a parameters:

@Post("/")
async method(@Required() @BodyParams("field") field: string) {}
1
2

To decorate a model:

class Model {
  @Property()
  @Required()
  field: string;
}
1
2
3
4
5

Required will throw a BadRequest when the given value is null, an empty string or undefined.

# Allow a values

In some case, you didn't want trigger a BadRequest when the value is an empty string for example. The decorator @Allow(), allow you to configure a value list for which there will be no exception.

class Model {
  @Property()
  @Required()
  @Allow("")
  field: string;
}
1
2
3
4
5
6