# Enum Decorator

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.

# 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