isValid<T> function

bool isValid<T>(
  1. T? object, {
  2. Iterable<bool> customValidator(
    1. T?
    )?,
})

Checks if object has a valid value.

The following values are considered not valid:

  • objects thats fail against a custom validation functions (if provided). The custom validation function must return a list of booleans. If at least one of the booleans is true, the object is considered valid.;
  • NULL objects, independent of type;
  • Strings that are empty, equal to "null" or have only white spaces;
  • nums that are equal to 0;
  • DateTimes that are equal to minDate;
  • LatLngs that are null or equal to (0,0);
  • bools that are equal to false;
  • Iterables that are empty or have only invalid values;
  • Maps that are empty or have only invalid values;
  • Classes that implement Validators that have validation errors;
  • For other types, this method calls toString() and checks the result string against isValid.

Returns true if the object is valid, false otherwise.

Example 1:

var value = "Hello";
var isValid = isValid(value);
print(isValid); // Output: true

Example 2:

var number = 0;
var isValidNumber = isValid(number);
print(isValidNumber); // Output: false

Example 3:

var date = DateTime.now();
var isValidDate = isValid(date);
print(isValidDate); // Output: true

Example 4:

var list = [1, 2, 3];
var isValidList = isValid(list);
print(isValidList); // Output: true

Example 5:

var map = {'name': 'John', 'age': 30};
var isValidMap = isValid(map);
print(isValidMap); // Output: true

Example 6:

class Person implements Validator {
 String name;
int age;
Person(this.name, this.age);
@override
List<String> validate() {
var errors = <String>[];
if (name.isEmpty) {
  errors.add('Name is required');
}
if (age <= 0) {
  errors.add('Age must be greater than 0');
}
 return errors;
}
}
var person = Person('John', 30);
var isValidPerson = isValid(person);
print(isValidPerson); // Output: true

Implementation

bool isValid<T>(T? object, {Iterable<bool> Function(T?)? customValidator}) {
  try {
    if (customValidator != null) {
      return customValidator(object).contains(true);
    }
    if (object == null) {
      return false;
    }
    if (object is Validator) {
      return object.validate().isEmpty;
    }
    if (object is String) {
      return object.nullIf((s) => "$s".trimAll.flatEqualAny(["null", ""])) != null;
    }
    if (object is bool) {
      return object;
    }
    if (object is num) {
      return object != 0 && object.isNaN == false;
    }
    if (object is DateTime) {
      return object > minDate;
    }

    if (object is LatLng) {
      return object.latitude.isValid() && object.longitude.isValid();
    }

    if (object is Iterable) {
      var l = object;
      if (l.isEmpty) return false;
      for (var e in l) {
        if (isValid(e)) {
          return true;
        }
      }
      return false;
    }
    if (object is Map) {
      return object.isNotEmpty && object.values.isValid();
    }

    return object.toString().isValid();
  } catch (e) {
    consoleLog("IsValid => $e", error: e);
    return false;
  }
}