string
@tsed/schema
Usage
typescript
import { string } from "@tsed/schema";Overview
ts
function string(): import("../index.js").JsonSchema<string>;Description
Create a string schema programmatically.
This function provides a fluent API for building string schemas without decorators. It returns a JsonSchema instance configured for string type, which can be further customized with validation constraints, formats, and other JSON Schema properties.
Usage
typescript
import {string} from "@tsed/schema";
// Basic string schema
const nameSchema = string();
// String with constraints
const emailSchema = string()
.format("email")
.minLength(5)
.maxLength(100)
.description("User email address");
// String with pattern
const phoneSchema = string()
.pattern(/^\d{3}-\d{3}-\d{4}$/)
.description("Phone number in format: 123-456-7890");
// Enum string
const statusSchema = string()
.enum("active", "inactive", "pending");Available Methods
The returned JsonSchema provides many chainable methods:
minLength(n)/maxLength(n)- Length constraintspattern(regex)- Pattern matchingformat(type)- Format validation (email, uri, date-time, etc.)enum(...values)- Restrict to specific valuesdescription(text)- Add descriptiondefault(value)- Set default valuenullable()- Allow null values