Skip to content

MaxLength

@tsed/schema

Usage

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

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

Overview

ts
const MaxLength: import("../../utils/withErrorMsg.js").ErrorChainedDecorator<(maxLength: number) => (...args: any[]) => any>;

Description

A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword.

The length of a string instance is defined as the number of its characters as defined by RFC 7159.

WARNING

The value of maxLength MUST be a non-negative integer.

TIP

Omitting this keyword has the same behavior as a value of 0.

WARNING

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

Example

With primitive type

typescript
class Model {
   @MaxLength(10)
   property: number;
}

Will produce:

json
{
  "type": "object",
  "properties": {
    "property": {
      "type": "string",
      "maxLength": 10
    }
  }
}

With array type

typescript
class Model {
   @MaxLength(10)
   @CollectionOf(String)
   property: string[];
}

Will produce:

json
{
  "type": "object",
  "properties": {
    "property": {
      "type": "array",
      "items": {
         "type": "string",
         "maxLength": 10
      }
    }
  }
}

Released under the MIT License.