Skip to content

Const

@tsed/schema

Usage

typescript
import { Const } from "@tsed/schema";

See /packages/specs/schema/src/types/decorators/common/const.ts.

Overview

ts
function Const(constValue: JSONSchema6Type | any): (...args: any[]) => any;

Description

The const keyword is used to restrict a value to a fixed value.

WARNING

For v6 user, use Const from @tsed/schema instead of @tsed/platform-http.

Example

With a string

typescript
class Model {
   @Const("value1")
   property: "value1";
}

Will produce:

json
{
  "type": "object",
  "properties": {
    "property": {
      "type": "string",
      "const": "value1"
    }
  }
}

With a boolean

typescript
class Model {
   @Const(true)
   property: boolean;
}

Will produce:

json
{
  "type": "object",
  "properties": {
    "property": {
      "type": "boolean",
      "const": true
    }
  }
}

Released under the MIT License.