validate method

  1. @override
Map<String, dynamic>? validate(
  1. String? value
)
override

Validates the given value.

Returns null if the value is valid, or a Map<String, dynamic> containing error information if the value is invalid.

The error map typically has a single key identifying the validation error type, with a value containing detailed error information such as constraints and the actual value.

Example error format:

{
  'outOfRange': {
    'min': 0,
    'max': 100,
    'actual': 150,
  }
}

Parameters:

  • value: The value to validate. Can be null.

Returns:

  • null if validation passes
  • Map<String, dynamic> with error details if validation fails

Implementation

@override
Map<String, dynamic>? validate(String? value) {
  // Null values are allowed (optional field)
  if (value == null) return null;

  // Check for empty or whitespace-only string
  if (value.trim().isEmpty) {
    return {
      emptyError: {
        'fieldName': fieldName,
        'actual': value,
      },
    };
  }

  return null;
}