# Enum Decorator validation swagger schema
Module | import { Enum } from "@tsed/common" |
---|---|
Source | /packages/common/src/jsonschema/decorators/enum.ts |
# Overview
function Enum(enumValue: JSONSchema6Type | any, ...enumValues: JSONSchema6Type[]): (...parameters: any[]) => any;
# 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
This decorator will be removed in v7. For v6 user, use 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
- 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