Skip to content

Support String formats #60

@nomisRev

Description

@nomisRev

See https://swagger.io/docs/specification/data-models/data-types/ Strings format

Strings
A string of text is defined as:

type: string

String length can be restricted using minLength and maxLength:

type: string
minLength: 3
maxLength: 20

Note that an empty string "" is a valid string unless minLength or pattern is specified.

String Formats

An optional format modifier serves as a hint at the contents and format of the string. OpenAPI defines the following built-in string formats:

  • date – full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21
  • date-time – the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z
  • password – a hint to UIs to mask the input
  • byte – base64-encoded characters, for example, U3dhZ2dlciByb2Nrcw==
  • binary – binary data, used to describe files (see Files below)

However, format is an open value, so you can use any formats, even not those defined by the OpenAPI Specification, such as:

  • email
  • uuid
  • uri
  • hostname
  • ipv4
  • ipv6
  • and others

Tools can use the format to validate the input or to map the value to a specific type in the chosen programming language. Tools that do not support a specific format may default back to the type alone, as if the format is not specified.
pattern
The pattern keyword lets you define a regular expression template for the string value. Only the values that match this template will be accepted. The regular expression syntax used is from JavaScript (more specifically, ECMA 262). Regular expressions are case-sensitive, that is, [a-z] and [A-Z] are different expressions. For example, the following pattern matches a Social Security Number (SSN) in the 123-45-6789 format:
ssn:

  type: string
  pattern: '^\d{3}-\d{2}-\d{4}$'

Note that the regular expression is enclosed in the ^…$ tokens, where ^ means the beginning of the string, and $ means the end of the string. Without ^…$, pattern works as a partial match, that is, matches any string that contains the specified regular expression. For example, pattern: pet matches pet, petstore and carpet. The ^…$ token forces an exact match.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions