StartsWithValidator class

A validator that checks whether a given string starts with a specific prefix.

The StartsWithValidator ensures that the input string begins with the expected prefix, with support for case-sensitive and case-insensitive validation.

Example

final validator = StartsWithValidator(
  'Hello',
  message: 'Must start with "Hello"',
);

print(validator.validate('Hello World')); // null (valid)
print(validator.validate('hello world')); // 'Must start with "Hello"' (invalid)

final caseInsensitiveValidator = StartsWithValidator(
  'hello',
  message: 'Must start with "hello"',
  caseSensitivity: CaseSensitivity.insensitive,
);

print(caseInsensitiveValidator.validate('Hello World')); // null (valid)

Parameters

  • prefix: The required prefix that the string must start with.
  • message: The error message if validation fails.
  • caseSensitivity: Defines whether the validation should be case-sensitive or not (default: CaseSensitivity.sensitive).

Behavior:

  • If caseSensitivity is CaseSensitivity.insensitive, both the prefix and the input value are converted to lowercase before comparison.
  • If the input value is null, validation fails.
Inheritance

Constructors

StartsWithValidator(String prefix, {required String message, CaseSensitivity caseSensitivity = CaseSensitivity.sensitive})
Creates a StartsWithValidator with the given prefix, message, and caseSensitivity.

Properties

caseSensitivity CaseSensitivity
Determines whether the validation is case-sensitive or not.
final
hashCode int
The hash code for this object.
no setterinherited
message String
The error message to be returned when validation fails.
finalinherited
prefix String
The required prefix for validation.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
validate(covariant String value) String?
Validates the provided value.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited