# Required Decorator operation input schema validation
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
2
To decorate a model:
class Model {
@Property()
@Required()
field: string;
}
1
2
3
4
5
2
3
4
5
Required will throw a BadRequest when the given value is
null
, an empty string orundefined
.
# 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
2
3
4
5
6
- Session & cookies
- Passport.js
- TypeORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Throw HTTP Exceptions
- Customize 404
- AWS
- Jest
- Seq
- Controllers
- Providers
- Model
- Converters
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Injection scopes
- Custom providers
- Custom endpoint decorator
- Testing