# Enum Decorator validation swagger schema input

Module
import { Enum } from "@tsed/schema"
Source/packages/schema/src/decorators/common/enum.ts

# Overview

function Enum(enumValue: JSONSchema6Type | any, ...enumValues: JSONSchema6Type[]): (...args: 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

For v6 user, use Enumerable from @tsed/schema instead of @tsed/common.

# Example

# With primitive type

class Model {
   @Enum("value1", "value2")
   property: "value1" | "value2";
}
1
2
3
4

Will produce:

{
  "type": "object",
  "properties": {
    "property": {
      "type": "string",
      "enum": ["value1", "value2"]
    }
  }
}
1
2
3
4
5
6
7
8
9

# With array type

class Model {
   @Enum("value1", "value2")
   property: ("value1" | "value2")[];
}
1
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

# 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

Will produce:

{
  "type": "object",
  "properties": {
    "property": {
       "type": "string",
       "enum": ["enum1", "enum2"]
    }
  }
}
1
2
3
4
5
6
7
8
9

Other topics