StringValidator class
A validator for string values, providing various validation rules such as length constraints, format checks (email, URL, UUID), and pattern matching.
Constructors
- StringValidator({String invalidTypeMessage = "Must be a valid string"})
- Creates a StringValidator with a custom error message for invalid types.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- invalidTypeMessage → String
-
finalinherited
-
rules
→ List<
String? Function(String?)> -
Returns an immutable list of validation rules.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addRule(
String? rule(String?)) → Validator< String> -
Adds a validation rule to the validator.
inherited
-
alpha(
{String message = "Must contain only letters"}) → StringValidator - Ensures the string contains only alphabetic characters (A-Z, a-z).
-
alphanumeric(
{String message = "Must contain only letters and numbers"}) → StringValidator - Ensures the string contains only alphanumeric characters (A-Z, a-z, 0-9).
-
ascii(
{String message = "Must contain only ASCII characters"}) → StringValidator - Ensures the string contains only ASCII characters.
-
base64(
{String message = "Invalid Base64 format"}) → StringValidator - Ensures the string is in valid Base64 encoding.
-
build(
{bool returnAllErrors = false}) → String? Function(String?) -
Builds a function that validates a
String?input.inherited -
cidr(
{String message = "Invalid CIDR notation"}) → StringValidator - Ensures the string is a valid CIDR notation.
-
contains(
String expected, {String? message}) → StringValidator -
Ensures the string contains the
expectedsubstring. -
creditCard(
{String message = "Invalid credit card"}) → StringValidator - Ensures the string is a valid credit card number.
-
cuid(
{String message = "Invalid CUID"}) → StringValidator - Validates that the string is a valid CUID (must start with 'c' followed by at least 24 lowercase alphanumeric characters).
-
cuid2(
{String message = "Invalid CUID2"}) → StringValidator - Validates that the string is a valid CUID2 (24+ lowercase alphanumeric characters, excluding 'c' as the first character).
-
date(
{String message = "Invalid date"}) → StringValidator - Ensures the string is a valid date (YYYY-MM-DD).
-
datetime(
{String message = "Invalid datetime"}) → StringValidator - Ensures the string is a valid datetime (YYYY-MM-DD HH:mm:ss).
-
email(
{String message = "Invalid email"}) → StringValidator - Validates that the string is a properly formatted email.
-
emoji(
{String message = "Must contain at least one emoji"}) → StringValidator - Validates that the string contains at least one emoji.
-
endsWith(
String suffix, {String? message}) → StringValidator -
Ensures the string ends with the given
suffix. -
hexColor(
{String message = "Invalid HEX color"}) → StringValidator - Ensures the string is a valid hexadecimal color.
-
ip(
{String message = "Invalid IP address"}) → StringValidator - Ensures the string is a valid IP address (IPv4 or IPv6).
-
isNumeric(
{String message = "Must contain only numbers"}) → StringValidator - Ensures string contains only numeric digits (0-9)
-
isoDatetime(
{String message = "Invalid ISO 8601 datetime"}) → StringValidator - Validates that the string follows the ISO 8601 datetime format (e.g., "2023-06-29T12:34:56Z").
-
isPalindrome(
{String message = "String is not a palindrome"}) → StringValidator - Ensures the string is a palindrome (reads the same forward and backward).
-
json(
{String message = "Invalid JSON"}) → StringValidator - Ensures the string is a valid JSON.
-
length(
int length, {String? message}) → StringValidator -
Ensures the string has exactly
lengthcharacters. -
maxLength(
int length, {String? message}) → StringValidator -
Ensures the string has at most
lengthcharacters. -
minLength(
int length, {String? message}) → StringValidator -
Ensures the string has at least
lengthcharacters. -
nanoid(
{String message = "Invalid NanoID"}) → StringValidator - Validates that the string is a valid NanoID (alphanumeric, underscore, or hyphen, with at least 10 characters).
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
parseValue(
String? input) → String? -
Converts a
String?input into a value of typeT?. -
phoneNumber(
{String message = "Invalid phone number"}) → StringValidator - Ensures the string is a valid phone number.
-
regex(
RegExp pattern, {String message = "Invalid format"}) → StringValidator -
Ensures the string matches the provided
pattern. -
required(
{String message = "Field is required"}) → StringValidator - Ensures the field is not null or empty.
-
slug(
{String message = "Invalid slug"}) → StringValidator - Ensures the string is a valid slug (lowercase, alphanumeric, hyphens).
-
startsWith(
String prefix, {String? message}) → StringValidator -
Ensures the string starts with the given
prefix. -
time(
{String message = "Invalid time"}) → StringValidator - Ensures the string is a valid time (HH:mm:ss).
-
toString(
) → String -
A string representation of this object.
inherited
-
url(
{String message = "Invalid URL"}) → StringValidator - Validates that the string is a properly formatted URL.
-
uuid(
{String message = "Invalid UUID"}) → StringValidator - Ensures the string is a valid UUID (v4 format).
-
validate(
dynamic value, {bool returnAllErrors = false, bool returnAsList = false}) → Object? -
Validates a given value by parsing it into type
T?, then applying validation rules. -
validateStrict(
String? value, {bool returnAllErrors = false, bool returnAsList = false}) → Object? -
Validates a value of type
T?directly, skipping parsing.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited