# Constant Required
Constant validation swagger schema inputModule | import { Required } from "@tsed/schema" |
---|---|
Source | /packages/specs/schema/src/types/decorators/common/required.ts |
# Overview
const Required: import("../../utils/withErrorMsg").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:
@Post("/")
async method(@Required() @BodyParams("field") field: string) {}
1
2
2
To decorate a model:
class Model {
@Required()
field: string;
}
1
2
3
4
2
3
4
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.
class Model {
@Allow("") // add automatically required flag
field: string;
}
1
2
3
4
2
3
4