CEPValidator class
A validator that ensures a given string is a valid Brazilian postal code (CEP).
The CEPValidator
checks whether the provided value follows the Brazilian CEP format,
which consists of exactly 8 digits (without special characters) and is not a repeated sequence of numbers.
Validation Modes
ValidationMode.formatted
→ Expects the CEP in the formatted pattern:"XXXXX-XXX"
(e.g.,"01001-000"
).ValidationMode.unformatted
(default) → Expects only digits:"XXXXXXXX"
(e.g.,"01001000"
).
Example
final validator = CEPValidator(message: 'Invalid CEP');
print(validator.validate('01001-000')); // null (valid)
print(validator.validate('01001000')); // null (valid)
print(validator.validate('00000-000')); // 'Invalid CEP' (invalid)
print(validator.validate('abcdefgh')); // 'Invalid CEP' (invalid)
print(validator.validate('1234')); // 'Invalid CEP' (invalid)
print(validator.validate(null)); // 'Invalid CEP' (invalid)
Behavior
- Ensures the CEP format matches the expected pattern.
- Removes non-digit characters when
ValidationMode.unformatted
is used. - Rejects values where all characters are the same (e.g.,
"00000000"
or"11111-111"
).
- Inheritance
-
- Object
- Validator<
String> - ValidatorWithMessage<
String> - CEPValidator
Constructors
- CEPValidator.new({required String message, ValidationMode mode = ValidationMode.unformatted})
-
Creates a
CEPValidator
with a custommessage
for validation failures.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- message → String
-
The error message to be returned when validation fails.
finalinherited
- mode → ValidationMode
-
Defines whether the CEP should be validated as formatted (
XXXXX-XXX
) or unformatted (XXXXXXXX
).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