isValid method
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 istrue
, 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
; - 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([List<bool> Function(T?)? customValidator]) => _isvalid<T>(this, customValidator);