Skip to main content

String Mutators

String mutators transform the input data before validation. All mutators are executed first, then validators run on the transformed data.


All Mutators


Case Conversion

toString()

Convert value to string

v.string().toString()
// Input: 123 → Output: "123"

uppercase()

Convert to uppercase

v.string().uppercase()
// Input: "hello" → Output: "HELLO"

lowercase()

Convert to lowercase

v.string().lowercase()
// Input: "HELLO" → Output: "hello"

capitalize()

Capitalize first letter

v.string().capitalize()
// Input: "hello world" → Output: "Hello world"

titleCase()

Convert to title case

v.string().titleCase()
// Input: "hello world" → Output: "Hello World"

camelCase()

Convert to camelCase

v.string().camelCase()
// Input: "hello world" → Output: "helloWorld"

pascalCase()

Convert to PascalCase

v.string().pascalCase()
// Input: "hello world" → Output: "HelloWorld"

snakeCase()

Convert to snake_case

v.string().snakeCase()
// Input: "hello world" → Output: "hello_world"

kebabCase()

Convert to kebab-case

v.string().kebabCase()
// Input: "hello world" → Output: "hello-world"

slug()

Convert to URL-friendly slug

v.string().slug()
// Input: "Hello World!" → Output: "hello-world"

Trimming & Padding

trim(needle?)

Trim whitespace or specified character

v.string().trim()        // Trim spaces
v.string().trim('*') // Trim asterisks
// Input: " hello " → Output: "hello"

ltrim(needle?)

Trim from left

v.string().ltrim()
// Input: " hello" → Output: "hello"

rtrim(needle?)

Trim from right

v.string().rtrim()
// Input: "hello " → Output: "hello"

trimMultipleWhitespace()

Trim multiple whitespace into single space

v.string().trimMultipleWhitespace()
// Input: "hello world" → Output: "hello world"

padStart(length, char)

Pad from start

v.string().padStart(10, '0')
// Input: "123" → Output: "0000000123"

padEnd(length, char)

Pad from end

v.string().padEnd(10, '0')
// Input: "123" → Output: "1230000000"

Encoding & Decoding

base64Encode()

Base64 encode

v.string().base64Encode()
// Input: "hello" → Output: "aGVsbG8="

base64Decode()

Base64 decode

v.string().base64Decode()
// Input: "aGVsbG8=" → Output: "hello"

urlEncode()

URL encode

v.string().urlEncode()
// Input: "hello world" → Output: "hello%20world"

urlDecode()

URL decode

v.string().urlDecode()
// Input: "hello%20world" → Output: "hello world"

htmlEscape()

HTML escape

v.string().htmlEscape()
// Input: "<script>" → Output: "&lt;script&gt;"

unescapeHtml()

Unescape HTML

v.string().unescapeHtml()
// Input: "&lt;script&gt;" → Output: "<script>"

String Manipulation

reverse()

Reverse string

v.string().reverse()
// Input: "hello" → Output: "olleh"

truncate(maxLength, suffix)

Truncate to maximum length

v.string().truncate(10, "...")
// Input: "This is a long string" → Output: "This is..."

repeat(count)

Repeat string N times

v.string().repeat(3)
// Input: "hello" → Output: "hellohellohello"

mask(start, end?, char)

Mask part of string

v.string().mask(2, 4, '*')
// Input: "password" → Output: "pa****rd"

replace(search, replace)

Replace substring or pattern

v.string().replace('world', 'universe')
// Input: "hello world" → Output: "hello universe"

replaceAll(search, replace)

Replace all occurrences

v.string().replaceAll('l', 'L')
// Input: "hello" → Output: "heLLo"

append(suffix)

Append text to the end

v.string().append('!')
// Input: "hello" → Output: "hello!"

prepend(prefix)

Prepend text to the beginning

v.string().prepend('Mr. ')
// Input: "Smith" → Output: "Mr. Smith"

Advanced Mutators

toAlpha()

Keep only alphabetic characters

v.string().toAlpha()
// Input: "hello123world" → Output: "helloworld"

toAlphanumeric()

Keep only alphanumeric characters

v.string().toAlphanumeric()
// Input: "hello@#$world" → Output: "helloworld"

removeNumbers()

Remove numeric characters

v.string().removeNumbers()
// Input: "hello123world" → Output: "helloworld"

removeSpecialCharacters()

Remove special characters

v.string().removeSpecialCharacters()
// Input: "hello@#$world" → Output: "hello world"

safeHtml()

Remove HTML tags

v.string().safeHtml()
// Input: "<p>Hello</p>" → Output: "Hello"

Examples

Chaining Mutators

const schema = v.string()
.trim() // Remove whitespace
.lowercase() // Convert to lowercase
.slug() // Convert to slug
.minLength(3); // Validate length

const result = await validate(schema, " Hello World! ");
// Result: { isValid: true, data: "hello-world" }

Email Normalization

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

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

Text Sanitization

const sanitizedSchema = v.string()
.trim()
.removeSpecialCharacters()
.titleCase();

const result = await validate(sanitizedSchema, " hello@#$world! ");
// Result: { isValid: true, data: "Hello World" }

See Also