Skip to main content

String Validators

String validators check the data after mutators have been applied. They validate format, length, content, and more.


All Validators


Format Validation

email()

Validate email format

v.string().email()
// Input: "user@example.com" → Valid ✅
// Input: "invalid-email" → Invalid ❌

url()

Validate URL format

v.string().url()
// Input: "https://example.com" → Valid ✅
// Input: "not-a-url" → Invalid ❌

ip()

Validate IP address (IPv4 or IPv6)

v.string().ip()
// Input: "192.168.1.1" → Valid ✅
// Input: "256.256.256.256" → Invalid ❌

ip4()

Validate IPv4 address

v.string().ip4()
// Input: "192.168.1.1" → Valid ✅
// Input: "2001:db8::1" → Invalid ❌

ip6()

Validate IPv6 address

v.string().ip6()
// Input: "2001:db8::1" → Valid ✅
// Input: "192.168.1.1" → Invalid ❌

creditCard()

Validate credit card number

v.string().creditCard()
// Input: "4111111111111111" → Valid ✅
// Input: "123456789" → Invalid ❌

Length Validation

minLength(length) / min(length)

Minimum length

v.string().minLength(5)
// Input: "hello" → Valid ✅
// Input: "hi" → Invalid ❌

maxLength(length) / max(length)

Maximum length

v.string().maxLength(10)
// Input: "hello" → Valid ✅
// Input: "very long string" → Invalid ❌

length(length)

Exact length

v.string().length(5)
// Input: "hello" → Valid ✅
// Input: "hi" → Invalid ❌

between(min, max) / lengthBetween(min, max)

Length between range

v.string().between(3, 10)
// Input: "hello" → Valid ✅
// Input: "hi" → Invalid ❌

minWords(words)

Minimum word count

v.string().minWords(2)
// Input: "hello world" → Valid ✅
// Input: "hello" → Invalid ❌

maxWords(words)

Maximum word count

v.string().maxWords(3)
// Input: "hello world" → Valid ✅
// Input: "hello world test example" → Invalid ❌

words(words)

Exact word count

v.string().words(2)
// Input: "hello world" → Valid ✅
// Input: "hello" → Invalid ❌

Content Validation

contains(value)

Must contain substring

v.string().contains("world")
// Input: "hello world" → Valid ✅
// Input: "hello universe" → Invalid ❌

notContains(value)

Must not contain substring

v.string().notContains("test")
// Input: "hello world" → Valid ✅
// Input: "hello test" → Invalid ❌

startsWith(value)

Must start with

v.string().startsWith("hello")
// Input: "hello world" → Valid ✅
// Input: "world hello" → Invalid ❌

endsWith(value)

Must end with

v.string().endsWith("world")
// Input: "hello world" → Valid ✅
// Input: "world hello" → Invalid ❌

pattern(regex)

Must match regex pattern

v.string().pattern(/^[A-Z]+$/)
// Input: "HELLO" → Valid ✅
// Input: "hello" → Invalid ❌

withoutWhitespace()

Must not have whitespace

v.string().withoutWhitespace()
// Input: "hello" → Valid ✅
// Input: "hello world" → Invalid ❌

alpha()

Only alphabetic characters

v.string().alpha()
// Input: "hello" → Valid ✅
// Input: "hello123" → Invalid ❌

alphanumeric()

Only alphanumeric characters

v.string().alphanumeric()
// Input: "hello123" → Valid ✅
// Input: "hello@" → Invalid ❌

numeric()

Only numeric characters

v.string().numeric()
// Input: "123" → Valid ✅
// Input: "123abc" → Invalid ❌

Color Validation

color()

Validate any color format

v.string().color()
// Input: "#ff0000" → Valid ✅
// Input: "rgb(255,0,0)" → Valid ✅
// Input: "invalid" → Invalid ❌

hexColor()

Validate hex color

v.string().hexColor()
// Input: "#ff0000" → Valid ✅
// Input: "rgb(255,0,0)" → Invalid ❌

rgbColor()

Validate RGB color

v.string().rgbColor()
// Input: "rgb(255,0,0)" → Valid ✅
// Input: "#ff0000" → Invalid ❌

rgbaColor()

Validate RGBA color

v.string().rgbaColor()
// Input: "rgba(255,0,0,0.5)" → Valid ✅
// Input: "rgb(255,0,0)" → Invalid ❌

hslColor()

Validate HSL color

v.string().hslColor()
// Input: "hsl(0,100%,50%)" → Valid ✅
// Input: "#ff0000" → Invalid ❌

lightColor()

Validate light color

v.string().lightColor()
// Input: "#ffffff" → Valid ✅
// Input: "#000000" → Invalid ❌

darkColor()

Validate dark color

v.string().darkColor()
// Input: "#000000" → Valid ✅
// Input: "#ffffff" → Invalid ❌

Password Validation

strongPassword(minLength?)

Validate strong password

v.string().strongPassword(8)
// Requirements: 8+ chars, 1 uppercase, 1 lowercase, 1 number, 1 special char
// Input: "MyStr0ng!Pass" → Valid ✅
// Input: "weak" → Invalid ❌

Value Selection

enum(enumObject)

Must be one of enum values

enum Status { ACTIVE = 'active', INACTIVE = 'inactive' }
v.string().enum(Status)
// Input: "active" → Valid ✅
// Input: "pending" → Invalid ❌

oneOf(values) / in(values)

Must be one of values

v.string().oneOf(['admin', 'user', 'guest'])
// Input: "admin" → Valid ✅
// Input: "moderator" → Invalid ❌

allowsOnly(values)

Allow only specified values

v.string().allowsOnly(['yes', 'no'])
// Input: "yes" → Valid ✅
// Input: "maybe" → Invalid ❌

forbids(values) / notIn(values)

Forbid specified values

v.string().forbids(['admin', 'root'])
// Input: "user" → Valid ✅
// Input: "admin" → Invalid ❌

Examples

Email Validation

const emailSchema = v.string()
.required()
.lowercase()
.trim()
.email();

const result = await validate(emailSchema, " USER@EXAMPLE.COM ");
// Result: { isValid: true, data: "user@example.com" }

Username Validation

const usernameSchema = v.string()
.required()
.trim()
.minLength(3)
.maxLength(20)
.alphanumeric();

const result = await validate(usernameSchema, " john_doe123 ");
// Result: { isValid: true, data: "john_doe123" }

Password Validation

const passwordSchema = v.string()
.required()
.minLength(8)
.strongPassword();

const result = await validate(passwordSchema, "MyStr0ng!Pass");
// Result: { isValid: true, data: "MyStr0ng!Pass" }

Pattern Validation

const phoneSchema = v.string()
.required()
.pattern(/^\+?[1-9]\d{1,14}$/);

const result = await validate(phoneSchema, "+1234567890");
// Result: { isValid: true, data: "+1234567890" }

URL Validation

const urlSchema = v.string()
.required()
.url();

const result = await validate(urlSchema, "https://example.com");
// Result: { isValid: true, data: "https://example.com" }

Color Validation

const colorSchema = v.string()
.required()
.hexColor();

const result = await validate(colorSchema, "#ff0000");
// Result: { isValid: true, data: "#ff0000" }

See Also