validate method

  1. @override
bool validate(
  1. dynamic value, [
  2. List<String>? options
])
override

Validates whether the provided value meets the maximum requirement.

The value is expected to be a String or a num. The options parameter should contain a single element which is the maximum value as a String that can be parsed into a num.

For a String value, its length must be less than or equal to the specified maximum. For a num value, the number itself must be less than or equal to the specified maximum.

Returns true if the value meets the maximum requirement, otherwise returns false.

Implementation

@override
bool validate(dynamic value, [List<String>? options]) {
  // Check if options are provided and not empty
  if (options == null || options.isEmpty) return false;

  // Try to parse the first option as a number to get the maximum allowed value
  final max = num.tryParse(options[0]);
  if (max == null) return false;

  // Check if the value is a string and its length does not exceed the maximum value
  if (value is String) {
    return value.length <= max;
  }

  // Check if the value is a number and does not exceed the maximum value
  if (value is num) {
    return value <= max;
  }

  // Return false if the value is neither a string nor a number
  return false;
}