Skip to content

Format

@tsed/schema

Usage

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

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

Overview

ts
const Format: import("../../utils/withErrorMsg.js").ErrorChainedDecorator<(format: JsonFormatTypes | ValueOf<JsonFormatTypes>) => (...args: any[]) => any>;

Description

The following formats are supported for string validation with format keyword:

  • date: full-date according to RFC3339.
  • time: time with optional time-zone.
  • date-time: date-time from the same source (time-zone is mandatory).
  • uri: full uri with optional protocol.
  • email: email address.
  • hostname: host name according to RFC1034.
  • ipv4: IP address v4.
  • ipv6: IP address v6.
  • regex: tests whether a string is a valid regular expression by passing it to RegExp constructor.

WARNING

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

Example

With primitive type

typescript
class Model {
   @Format("email")
   property: string;
}
json
{
  "type": "object",
  "properties": {
    "property": {
      "type": "string",
      "format": "email"
    }
  }
}

With array type

typescript
class Model {
   @Format("email")
   @CollectionOf(String)
   property: string[];
}

Will produce:

json
{
  "type": "object",
  "properties": {
    "property": {
      "type": "array",
      "items": {
         "type": "string",
         "format": "email"
      }
    }
  }
}

Released under the MIT License.