validate method

String? validate(
  1. dynamic value
)

Validates a parsed or raw value, returning an error message if invalid, or null if valid.

Implementation

String? validate(dynamic value) {
  if (isRequired) {
    if (value == null) return '$header is required';
    if (type == TCsvColumnType.text && value.toString().trim().isEmpty) {
      return '$header is required';
    }
  }

  if (value != null && value.toString().trim().isNotEmpty) {
    if (type == TCsvColumnType.number) {
      if (value is! num) {
        final str = value.toString().trim().replaceAll('\$', '').replaceAll(',', '');
        if (double.tryParse(str) == null) {
          return '$header must be a valid number';
        }
      }
    } else if (type == TCsvColumnType.integer) {
      if (value is! int) {
        final str = value.toString().trim().replaceAll('\$', '').replaceAll(',', '');
        if (int.tryParse(str) == null && double.tryParse(str) == null) {
          return '$header must be a whole number';
        }
      }
    }
  }

  if (validator != null) {
    return validator!(value);
  }

  return null;
}