C2CNumberValidator
The C2CNumberValidator is a Dart class designed to facilitate the validation of numeric values in a flexible and customizable manner.
Constructor
C2CNumberValidator()
Creates a new instance of the validator with optional settings.
roundType: An enumeration indicating the rounding method to be applied to the numeric input value during validation. (floor, round, ceil, truncate)
Factory Constructors
C2CNumberValidator.required(String requiredMessage)
Creates a required instance of the validator with a custom error message for cases where the input is empty or null.
requiredMessage: A custom error message to be displayed when the input is required but empty.
Validation Methods
add(validator)
Adds a custom validation function to the list of validation functions.
C2CNumberValidator()
.add((value) => value == null ? "Error message" : null)
.validate("Test value");
compare(condition, message)
Adds a conditional validation rule. It takes a condition function and an error message. If the condition function returns true, the input is considered valid; otherwise, the error message is returned.
const anotherValue = 2;
C2CNumberValidator<int>()
.compare((value) => value == anotherValue, "Error message")
.validate(3);
when(condition, then, otherwise)
Adds conditional validation with branching. It takes a condition function, and two callbacks to configure validators for the "then" and "otherwise" branches. Depending on the condition, one of the branch validators is applied.
const anotherValue = 2.0;
C2CNumberValidator<double>()
.when(
condition: (value) => value == anotherValue,
then: (builder) => builder.min(5, "Min is 5"),
otherwise: (builder) => builder.min(30, "Min is 30"),
)
.validate(3);
oneOf(values, message)
Validates if the value is included in a list of allowed values. If the input is in the list, it's considered valid; otherwise, an error message is returned.
List<int> validValues = [1, 2, 3, 4];
C2CNumberValidator().oneOf(validValues, "Error message").validate(5);
notOneOf(values, message)
Validates if the value is not included in a list of disallowed values. If the input is not in the list, it's considered valid; otherwise, an error message is returned.
List<int> validValues = [1, 2, 3, 4];
C2CNumberValidator<int>()
.notOneOf(validValues, "Error message")
.validate(2);
min(min, message)
Validates that the input value is greater than or equal to the specified minimum value.
C2CNumberValidator<int>()
.min(2, "Error message")
.validate(1);
max(max, message)
Validates that the input value is less than or equal to the specified maximum value.
C2CNumberValidator<int>()
.max(5, "Error message")
.validate(6);
lessThan(less, message)
Validates that the input value is less than the specified value.
C2CNumberValidator<int>()
.lessThan(5, "Error message")
.validate(5);
moreThan(more, message)
Validates that the input value is greater than the specified value.
C2CNumberValidator<int>()
.moreThan(5, "Error message")
.validate(5);
positive(message)
Validates that the input value is a positive number (greater than zero).
C2CNumberValidator<int>()
.positive("Error message")
.validate(-1);
negative(message)
Validates that the input value is a negative number (less than zero).
C2CNumberValidator<int>()
.positive("Error message")
.validate(2);
Example Usage
C2CNumberValidator<int>.required("Value is required")
.min(10, 'Value must be at least 10')
.max(100, 'Value cannot exceed 100')
.validate(5);
C2CNumberValidator<int>()
.min(10, 'Value must be at least 10')
.max(100, 'Value cannot exceed 100')
.positive('Value must be positive')
.validate(5);
C2CNumberValidator<double>(roundType: RoundType.round)
.min(0.5, 'Value must be at least 0.5')
.max(10.0, 'Value cannot exceed 10')
.when(
condition: (value) => value > 5,
then: (builder) => builder.min(7.0, 'Value must be at least 7 when greater than 5'),
otherwise: (builder) => builder.max(3.0, 'Value cannot exceed 3 when less than or equal to 5'),
)
.validate(5)
C2CNumberValidator<int>()
.or(
(builder) => builder.min(0, 'Value must be positive'),
(builder) => builder.max(100, 'Value cannot exceed 100'),
reverse: true, // Reverse logic, choose the first valid branch
)
.validate(42)