# Format Decorator validation swagger schema input

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

# Overview

function Format(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 (opens new window).
  • 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 (opens new window).
  • 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 ErrorFormatter from @tsed/schema instead of @tsed/common.

# Example

# With primitive type

class Model {
   @Format("email")
   property: string;
}
1
2
3
4
{
  "type": "object",
  "properties": {
    "property": {
      "type": "string",
      "format": "email"
    }
  }
}
1
2
3
4
5
6
7
8
9

# With array type

class Model {
   @Format("email")
   @CollectionOf(String)
   property: string[];
}
1
2
3
4
5

Will produce:

{
  "type": "object",
  "properties": {
    "property": {
      "type": "array",
      "items": {
         "type": "string",
         "format": "email"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12

Other topics