# Decorator Required
Decorator validation swagger schema inputModule | import { Required } from "@tsed/schema" |
---|---|
Source | /packages/schema/src/decorators/common/required.ts |
# Overview
function Required(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
Other topics
- Session & cookies
- Passport.js
- TypeORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Throw HTTP Exceptions
- Customize 404
- AWS
- Seq
- OIDC
- Stripe
- Controllers
- Providers
- Model
- JsonMapper
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Exceptions
- Hooks
- Response Filter
- Injection scopes
- Custom providers
- Custom endpoint decorator
- Testing