# Decorator Enum
Decorator validation swagger schema inputModule | import { Enum } from "@tsed/schema" |
---|---|
Source | /packages/schema/src/decorators/common/enum.ts |
# Overview
function Enum(...enumValues: (JSONSchema6Type | any)[]): Function;
# Description
The enum keyword is used to restrict a value to a fixed set of values. It must be an array with at least one element, where each element is unique.
Elements in the array might be of any value, including null.
WARNING
For v6 user, use Enum from @tsed/schema instead of @tsed/common.
# Example
# With primitive type
class Model {
@Enum("value1", "value2")
property: "value1" | "value2";
}
1
2
3
4
2
3
4
Will produce:
{
"type": "object",
"properties": {
"property": {
"type": "string",
"enum": ["value1", "value2"]
}
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# With array type
class Model {
@Enum("value1", "value2")
property: ("value1" | "value2")[];
}
1
2
3
4
2
3
4
Will produce:
{
"type": "object",
"properties": {
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["value1", "value2"]
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# With Typescript Enum
enum SomeEnum {
ENUM_1 = "enum1",
ENUM_2 = "enum2"
}
class Model {
@Enum(SomeEnum)
property: SomeEnum;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Will produce:
{
"type": "object",
"properties": {
"property": {
"type": "string",
"enum": ["enum1", "enum2"]
}
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
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