Skip to content

Required

@tsed/schema

Usage

typescript
import { Required } from "@tsed/schema";

See /packages/specs/schema/src/types/decorators/common/required.ts.

Overview

ts
const Required: import("../../utils/withErrorMsg.js").ErrorChainedDecorator<(required?: boolean, ...allowedRequiredValues: any[]) => any>;

Description

Add required annotation on Property or Parameter.

The Required decorator can be used on two cases.

To decorate a parameters:

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

To decorate a model:

typescript
class Model {
  @Required()
  field: string;
}

TIP

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

Allow 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.

typescript
class Model {
  @Allow("") // add automatically required flag
  field: string;
}

Released under the MIT License.