Type Formats come with a comprehensive set of built-in formats for common validation scenarios. This page documents all available formats, their parameters, and the differences between similar formats.
Import from @mionkit/type-formats/FormatsString:
import {StrEmail, StrEmailStrict, StrEmailPunycode} from '@mionkit/type-formats/FormatsString';
StrEmail vs StrEmailStrict| Format | Validation | Performance | Use Case |
|---|---|---|---|
StrEmail | Pattern-based (regex) | Faster | General email validation, allows special chars like +, () |
StrEmailStrict | Component-based | Slower | Strict RFC compliance, validates local part and domain separately |
| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
StrEmail | varchar(254) | varchar(254) | text() |
StrEmail uses a single regex pattern for validation. It allows special characters in the local part:
import {StrEmail} from '@mionkit/type-formats/FormatsString';
type UserEmail = StrEmail;
// Valid
('user@example.com'); // ✓
('user+tag@example.com'); // ✓ (allows + for email aliases)
('user(comment)@test.com'); // ✓
// Invalid
('user@name@example.com'); // ✗ (multiple @)
('@example.com'); // ✗ (missing local part)
StrEmail is the most performant option for email validation. It uses a single regex pattern which makes it significantly faster than component-based validation.StrEmailStrict validates each component separately (local part, domain, TLD):
import {StrEmailStrict} from '@mionkit/type-formats/FormatsString';
// Valid
('user@example.com'); // ✓
('user.name@sub.domain.com'); // ✓
// Invalid (stricter than StrEmail)
('user+tag@example.com'); // ✗ (disallows special chars)
('user(comment)@test.com'); // ✗ (disallows parentheses)
StrEmailPunycode allows internationalized domains (punycode):
import {StrEmailPunycode} from '@mionkit/type-formats/FormatsString';
type InternationalEmail = StrEmailPunycode;
// Valid
('user@xn--80akhbyknj4f.xn--p1ai'); // ✓ (punycode domain)
('user@sub-domain.example.com'); // ✓ (hyphenated domains)
Import from @mionkit/type-formats/FormatsString:
import {StrUrl, StrUrlHttp, StrUrlFile} from '@mionkit/type-formats/FormatsString';
StrUrl vs StrUrlHttp| Format | Allowed Protocols | Use Case |
|---|---|---|
StrUrl | http, https, ftp, ftps, ws, wss | General URL validation |
StrUrlHttp | http, https only | Web URLs only |
StrUrlFile | file only | Local file paths |
| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
StrUrl | varchar(2048) | varchar(2048) | text() |
import {StrUrl, StrUrlHttp, StrUrlFile} from '@mionkit/type-formats/FormatsString';
type GeneralUrl = StrUrl;
type WebUrl = StrUrlHttp;
type FileUrl = StrUrlFile;
// StrUrl - accepts multiple protocols
('http://example.com'); // ✓
('https://example.com'); // ✓
('ftp://example.com'); // ✓
('ws://example.com'); // ✓
// StrUrlHttp - HTTP/HTTPS only
('http://example.com'); // ✓
('https://example.com'); // ✓
('ftp://example.com'); // ✗
('ws://example.com'); // ✗
// StrUrlFile - file protocol only
('file://hello.png'); // ✓
('file:///c:/lorem/ipsum.jpg'); // ✓
('http://example.com'); // ✗
Import from @mionkit/type-formats/FormatsString:
import {StrDomain, StrDomainStrict} from '@mionkit/type-formats/FormatsString';
StrDomain vs StrDomainStrict| Format | Validation | Performance | Error Messages |
|---|---|---|---|
StrDomain | Pattern-based (regex) | Faster | Generic errors |
StrDomainStrict | Component-based | Slower | Detailed errors per component |
| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
StrDomain | varchar(253) | varchar(253) | text() |
import {StrDomain, StrDomainStrict} from '@mionkit/type-formats/FormatsString';
type QuickDomain = StrDomain;
type DetailedDomain = StrDomainStrict;
// Both validate the same cases
('example.com'); // ✓
('sub.example.com'); // ✓
('example.co.uk'); // ✓
// Invalid for both
('example'); // ✗ (missing TLD)
('example..com'); // ✗ (empty part)
('exa!mple.com'); // ✗ (invalid characters)
Custom Domain Parameters:
import {StrDomainStrict} from '@mionkit/type-formats/FormatsString';
type SocialMediaDomain = StrDomainStrict<{
maxLength: 200;
minParts: 2;
maxParts: 3;
names: {
allowedValues: {
val: ['facebook', 'twitter', 'instagram', 'linkedin'];
errorMessage: 'Only social media domains allowed';
};
};
tld: {
allowedValues: {
val: ['com'];
errorMessage: 'Only .com TLD allowed';
};
};
}>;
import {StrUUIDv4, StrUUIDv7} from '@mionkit/type-formats/FormatsString';
type UserId = StrUUIDv4; // Standard UUID v4: xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx
type EventId = StrUUIDv7; // Time-ordered UUID v7: xxxxxxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx
| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
StrUUID / StrUUIDv7 | uuid() | varchar(36) | text() |
import {StrIP, StrIPv4, StrIPv6, StrIPWithPort} from '@mionkit/type-formats/FormatsString';
type AnyIP = StrIP; // IPv4 or IPv6
type OnlyV4 = StrIPv4; // IPv4 only
type OnlyV6 = StrIPv6; // IPv6 only
type WithPort = StrIPWithPort; // Any IP with port support
// Examples
('192.168.0.1'); // ✓ StrIPv4
('2001:0db8:85a3::8a2e:0370:7334'); // ✓ StrIPv6
('localhost'); // ✓ (when allowLocalHost: true)
IP Format Parameters:
| Parameter | Type | Description |
|---|---|---|
version | 4 | 6 | 'any' | IP version to validate |
allowLocalHost | boolean | Allow localhost values (127.0.0.1, ::1) |
allowPort | boolean | Allow port suffix (:8080) |
| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
StrIP / StrIPv4 / StrIPv6 | inet() | varchar(45) | text() |
Available Date Formats:
| Format | Example | Description |
|---|---|---|
'ISO' or 'YYYY-MM-DD' | 2023-01-15 | ISO 8601 date (default) |
'DD-MM-YYYY' | 15-01-2023 | European format |
'MM-DD-YYYY' | 01-15-2023 | US format |
'YYYY-MM' | 2023-01 | Year and month only |
'MM-DD' | 01-15 | Month and day only |
'DD-MM' | 15-01 | Day and month only |
import {StrDate} from '@mionkit/type-formats/FormatsString';
// Default ISO format
type ISODate = StrDate; // 'YYYY-MM-DD'
// Custom formats
type EuropeanDate = StrDate<{format: 'DD-MM-YYYY'}>;
type USDate = StrDate<{format: 'MM-DD-YYYY'}>;
type MonthYear = StrDate<{format: 'YYYY-MM'}>;
// Validates leap years correctly
('2000-02-29'); // ✓ (leap year)
('1900-02-29'); // ✗ (not a leap year)
| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
StrDate | date() | date() | text() |
Available Time Formats:
| Format | Example | Description |
|---|---|---|
'ISO' or 'HH:mm:ss[.mmm]TZ' | 14:30:00Z | ISO 8601 with timezone (default) |
'HH:mm:ss[.mmm]' | 14:30:00.123 | With optional milliseconds |
'HH:mm:ss' | 14:30:00 | Hours, minutes, seconds |
'HH:mm' | 14:30 | Hours and minutes |
'mm:ss' | 30:45 | Minutes and seconds |
'HH' | 14 | Hours only |
'mm' | 30 | Minutes only |
'ss' | 45 | Seconds only |
import {StrTime} from '@mionkit/type-formats/FormatsString';
// Default ISO format with timezone
type ISOTime = StrTime; // 'HH:mm:ss[.mmm]TZ'
// Custom formats
type SimpleTime = StrTime<{format: 'HH:mm:ss'}>;
type ShortTime = StrTime<{format: 'HH:mm'}>;
type Duration = StrTime<{format: 'mm:ss'}>;
// ISO time examples
('14:30:00Z'); // ✓ UTC
('14:30:00+05:30'); // ✓ with offset
('14:30:00.123Z'); // ✓ with milliseconds
| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
StrTime | time() | time() | text() |
DateTime combines date and time formats with a configurable separator:
import {StrDateTime} from '@mionkit/type-formats/FormatsString';
// Default: ISO date + ISO time, separated by 'T'
type ISODateTime = StrDateTime;
// Example: '2023-01-15T14:30:00Z'
// Custom combination
type CustomDateTime = StrDateTime<{
date: {format: 'DD-MM-YYYY'};
time: {format: 'HH:mm'};
splitChar: ' '; // space separator instead of 'T'
}>;
// Example: '15-01-2023 14:30'
// Short datetime
type ShortDateTime = StrDateTime<{
date: {format: 'MM-DD'};
time: {format: 'HH'};
}>;
// Example: '01-15T14'
DateTime Parameters:
| Parameter | Type | Description |
|---|---|---|
date | FormatParams_Date | Date format configuration |
time | FormatParams_Time | Time format configuration |
splitChar | string | Separator between date and time (default: 'T') |
| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
StrDateTime | timestamp() | datetime() | text() |
import {StrAlphaNumeric, StrAlpha, StrNumeric, StrLowercase, StrUppercase} from '@mionkit/type-formats/FormatsString';
type Username = StrAlphaNumeric; // Letters and numbers only
type Name = StrAlpha; // Letters only (supports Unicode)
type Code = StrNumeric; // Digits only
type Slug = StrLowercase; // Forced lowercase
type Initials = StrUppercase; // Forced uppercase
Use StrFormat to define custom string constraints with validators and transformers:
import {StrFormat} from '@mionkit/type-formats/FormatsString';
// Username: 3-20 chars, lowercase, trimmed
type Username = StrFormat<{
minLength: 3;
maxLength: 20;
lowercase: true;
trim: true;
}>;
// Slug with pattern validation
const slugRegex = /^[a-z0-9-]+$/;
type Slug = StrFormat<{
minLength: 1;
maxLength: 100;
pattern: {
val: typeof slugRegex;
errorMessage: 'Slug can only contain lowercase letters, numbers, and hyphens';
mockSamples: ['my-post', 'hello-world', 'article-123'];
};
}>;
// Name with allowed characters only
type SafeName = StrFormat<{
minLength: 1;
maxLength: 50;
allowedChars: {
val: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
errorMessage: 'Name can only contain letters and spaces';
};
capitalize: true;
}>;
String Format Parameters:
| Parameter | Type | Description |
|---|---|---|
| Validators | ||
minLength | number | Minimum string length |
maxLength | number | Maximum string length |
length | number | Exact string length |
pattern | {val: RegExp; errorMessage: string; mockSamples: string[]} | Regex pattern validation |
allowedChars | {val: string; errorMessage: string} | Only these characters allowed |
disallowedChars | {val: string; errorMessage: string; mockSamples: string} | These characters not allowed |
allowedValues | {val: string[]; errorMessage: string} | Only these exact values allowed |
disallowedValues | {val: string[]; errorMessage: string} | These exact values not allowed |
| Transformers | ||
lowercase | boolean | Convert to lowercase |
uppercase | boolean | Convert to uppercase |
capitalize | boolean | Capitalize first letter |
trim | boolean | Remove leading/trailing whitespace |
replace | {searchValue: string; replaceValue: string} | Replace first occurrence |
replaceAll | {searchValue: string; replaceValue: string} | Replace all occurrences |
pattern, disallowedChars, or disallowedValues, you must provide mockSamples so the mock generator knows what valid values to produce.Import from @mionkit/type-formats/FormatsNumber:
import {
NumFormat,
NumInteger,
NumFloat,
NumPositive,
NumNegative,
NumPositiveInt,
NumNegativeInt,
NumInt8,
NumInt16,
NumInt32,
NumUInt8,
NumUInt16,
NumUInt32,
} from '@mionkit/type-formats/FormatsNumber';
| Type | Description | Range | Binary Size |
|---|---|---|---|
NumInteger | Any integer | Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER | 8 bytes |
NumFloat | Must be a float | Number.MIN_VALUE to Number.MAX_VALUE | 8 bytes |
NumPositive | ≥ 0 | 0 to Number.MAX_VALUE | 8 bytes |
NumNegative | ≤ 0 | Number.MIN_VALUE to 0 | 8 bytes |
NumPositiveInt | Positive integer | 0 to Number.MAX_SAFE_INTEGER | 8 bytes |
NumNegativeInt | Negative integer | Number.MIN_SAFE_INTEGER to 0 | 8 bytes |
NumInt8 | 8-bit signed | -2⁷ to 2⁷-1 (-128 to 127) | 1 byte |
NumInt16 | 16-bit signed | -2¹⁵ to 2¹⁵-1 (-32,768 to 32,767) | 2 bytes |
NumInt32 | 32-bit signed | -2³¹ to 2³¹-1 (-2,147,483,648 to 2,147,483,647) | 4 bytes |
NumUInt8 | 8-bit unsigned | 0 to 2⁸-1 (255) | 1 byte |
NumUInt16 | 16-bit unsigned | 0 to 2¹⁶-1 (65,535) | 2 bytes |
NumUInt32 | 32-bit unsigned | 0 to 2³²-1 (4,294,967,295) | 4 bytes |
NumInt8, NumUInt16) are encoded more efficiently than generic numbers, resulting in smaller binary payloads.| Format | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
NumInteger / NumPositiveInt / NumNegativeInt | integer() | int() | integer() |
NumInt8 / NumInt16 / NumInt32 | integer() | int() | integer() |
NumUInt8 / NumUInt16 / NumUInt32 | integer() | int() | integer() |
NumFloat / NumPositive / NumNegative | doublePrecision() | double() | real() |
BigIntFormat (TypeScript bigint) | bigint({mode: 'bigint'}) | bigint({mode: 'bigint'}) | blob({mode: 'bigint'}) |
import {NumFormat} from '@mionkit/type-formats/FormatsNumber';
// Age with valid range
type Age = NumFormat<{
min: 0;
max: 120;
integer: true;
}>;
// Percentage with decimals
type Percentage = NumFormat<{
min: 0;
max: 100;
}>;
// Price must be multiple of 0.01 (cents)
type Price = NumFormat<{
min: 0;
multipleOf: 1; // multipleOf must be integer
integer: true; // store as cents
}>;
Number Format Parameters:
| Parameter | Type | Description |
|---|---|---|
min | number | Minimum value (inclusive) |
max | number | Maximum value (inclusive) |
gt | number | Greater than (exclusive) |
lt | number | Less than (exclusive) |
integer | boolean | Must be an integer |
float | boolean | Must be a float |
multipleOf | number | Must be a multiple of (integer only) |
Note: min/max and gt/lt are mutually exclusive. integer and float are also mutually exclusive.