String
string() validates text values.
Use string rules for common inputs like emails, phone numbers, IDs, IPs, and custom regex patterns.
notEmpty
Value must not be an empty string.
Parameters
message: LocalizedStringResource?– optional custom error message if empty
let notEmpty = FormCraftValidationRules()
.string()
.notEmpty()
notEmpty.validate(value: "text") // ✅ is valid
notEmpty.validate(value: "") // ❌ is not validtrimmed
No leading or trailing whitespace is allowed.
Parameters
message: LocalizedStringResource?– optional custom error message if whitespace is found
let trimmed = FormCraftValidationRules()
.string()
.trimmed()
trimmed.validate(value: "hello") // ✅ is valid
trimmed.validate(value: " hello ") // ❌ is not validmin
Minimum length requirement.
Parameters
min: Int– minimum number of charactersmessage: ((Int) -> LocalizedStringResource)?– optional custom error message builder
let minRule = FormCraftValidationRules()
.string()
.min(min: 3)
minRule.validate(value: "hey") // ✅ is valid
minRule.validate(value: "hi") // ❌ is not validmax
Maximum length limit.
Parameters
max: Int– maximum number of charactersmessage: ((Int) -> LocalizedStringResource)?– optional custom error message builder
let maxRule = FormCraftValidationRules()
.string()
.max(max: 5)
maxRule.validate(value: "short") // ✅ is valid
maxRule.validate(value: "too long") // ❌ is not validlength
Exact length requirement.
Parameters
length: Int– required number of charactersmessage: ((Int) -> LocalizedStringResource)?– optional custom error message builder
let lengthRule = FormCraftValidationRules()
.string()
.length(length: 6)
lengthRule.validate(value: "ABC123") // ✅ is valid
lengthRule.validate(value: "ABC12") // ❌ is not validequals
Value must be equal to the given string.
Parameters
to: String– the string to compare againstmessage: ((String, String) -> LocalizedStringResource)?– optional custom error message builder
let equalsRule = FormCraftValidationRules()
.string()
.equals(to: "secret")
equalsRule.validate(value: "secret") // ✅ is valid
equalsRule.validate(value: "SECRET") // ❌ is not validregex
Validation against a custom regex pattern.
Parameters
pattern: Regex<Substring>– regex to validate againstmessage: LocalizedStringResource?– optional custom error message if pattern does not match
let code4Digits = FormCraftValidationRules()
.string()
.regex(pattern: /^[0-9]{4}$/)
code4Digits.validate(value: "1234") // ✅ is valid
code4Digits.validate(value: "12a4") // ❌ is not validcuid
Value must be a valid CUID.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let cuidRule = FormCraftValidationRules()
.string()
.cuid()
cuidRule.validate(value: "ckjq8y3nj0001x9m5h8b9kqzg") // ✅ is valid
cuidRule.validate(value: "invalid-cuid") // ❌ is not validcuid2
Value must be a valid CUID2.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let cuid2Rule = FormCraftValidationRules()
.string()
.cuid2()
cuid2Rule.validate(value: "a1b2c3d4e5f6") // ✅ is valid
cuid2Rule.validate(value: "A1B2C3") // ❌ is not validulid
Value must be a valid ULID.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let ulidRule = FormCraftValidationRules()
.string()
.ulid()
ulidRule.validate(value: "01ARZ3NDEKTSV4RRFFQ69G5FAV") // ✅ is valid
ulidRule.validate(value: "01ARZ3NDEKTSV4RRFFQ69G5FA!") // ❌ is not validuuid
Value must be a valid UUID.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let uuidRule = FormCraftValidationRules()
.string()
.uuid()
uuidRule.validate(value: "123e4567-e89b-12d3-a456-426614174000") // ✅ is valid
uuidRule.validate(value: "invalid-uuid") // ❌ is not validnanoId
Value must be a valid NanoID (21 chars).
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let nanoIdRule = FormCraftValidationRules()
.string()
.nanoId()
nanoIdRule.validate(value: "abcde12345_fghij-67890") // ✅ is valid
nanoIdRule.validate(value: "ABC-123") // ❌ is not validipv4
IPv4 address format validation.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let ipv4Rule = FormCraftValidationRules()
.string()
.ipv4()
ipv4Rule.validate(value: "192.168.0.1") // ✅ is valid
ipv4Rule.validate(value: "999.168.0.1") // ❌ is not validipv6
IPv6 address format validation.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let ipv6Rule = FormCraftValidationRules()
.string()
.ipv6()
ipv6Rule.validate(value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334") // ✅ is valid
ipv6Rule.validate(value: "2001:::7334") // ❌ is not validcidrv4
IPv4 CIDR notation validation.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let cidrv4Rule = FormCraftValidationRules()
.string()
.cidrv4()
cidrv4Rule.validate(value: "192.168.1.0/24") // ✅ is valid
cidrv4Rule.validate(value: "192.168.1.0/33") // ❌ is not validcidrv6
IPv6 CIDR notation validation.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let cidrv6Rule = FormCraftValidationRules()
.string()
.cidrv6()
cidrv6Rule.validate(value: "2001:db8::/32") // ✅ is valid
cidrv6Rule.validate(value: "2001:db8::/129") // ❌ is not validisoDate
Date string in the format YYYY-MM-DD (leap-year aware).
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let isoDateRule = FormCraftValidationRules()
.string()
.isoDate()
isoDateRule.validate(value: "2024-02-29") // ✅ is valid
isoDateRule.validate(value: "2023-02-29") // ❌ is not validemail
Email format validation.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let emailRule = FormCraftValidationRules()
.string()
.email()
emailRule.validate(value: "user@example.com") // ✅ is valid
emailRule.validate(value: "user@") // ❌ is not valide164phoneNumber
E.164-like phone number validation.
Parameters
message: LocalizedStringResource?– optional custom error message if invalid
let phoneRule = FormCraftValidationRules()
.string()
.e164phoneNumber()
phoneRule.validate(value: "+14155552671") // ✅ is valid
phoneRule.validate(value: "141-555-2671") // ❌ is not validstartsWith
Value must start with the given prefix.
Parameters
prefix: String– required starting substringmessage: ((String) -> LocalizedStringResource)?– optional custom error message builder
let startsWithRule = FormCraftValidationRules()
.string()
.startsWith(prefix: "Hello")
startsWithRule.validate(value: "Hello World") // ✅ is valid
startsWithRule.validate(value: "World Hello") // ❌ is not validendsWith
Value must end with the given suffix.
Parameters
suffix: String– required ending substringmessage: ((String) -> LocalizedStringResource)?– optional custom error message builder
let endsWithRule = FormCraftValidationRules()
.string()
.endsWith(suffix: ".com")
endsWithRule.validate(value: "example.com") // ✅ is valid
endsWithRule.validate(value: "example.org") // ❌ is not validincludes
Value must contain the given substring.
Parameters
substring: String– required substringmessage: ((String) -> LocalizedStringResource)?– optional custom error message builder
let includesRule = FormCraftValidationRules()
.string()
.includes(substring: "@")
includesRule.validate(value: "name@example.com") // ✅ is valid
includesRule.validate(value: "name.example.com") // ❌ is not validuppercase
Value must be fully uppercase.
Parameters
message: LocalizedStringResource?– optional custom error message if value is not uppercase
let uppercaseRule = FormCraftValidationRules()
.string()
.uppercase()
uppercaseRule.validate(value: "ABC") // ✅ is valid
uppercaseRule.validate(value: "AbC") // ❌ is not validlowercase
Value must be fully lowercase.
Parameters
message: LocalizedStringResource?– optional custom error message if value is not lowercase
let lowercaseRule = FormCraftValidationRules()
.string()
.lowercase()
lowercaseRule.validate(value: "abc") // ✅ is valid
lowercaseRule.validate(value: "aBc") // ❌ is not valid