You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
StringtoStringValue(Object? data) {
if (data ==null) throwconstNullValueException();
if (data isString) return data;
return data.toString();
}
/// Converts [data] to `int` or throws FormatException if cannot convert.////// If provided [min] and [max] are used to clamp the returned value.inttoIntValue(Object? data, {int? min, int? max}) {
if (data ==null) throwconstNullValueException();
int result;
if (data isnum) {
result = data.round();
} elseif (data isBigInt) {
result = data.toInt();
} elseif (data isString) {
result =_stringToInt(data);
} elseif (data isbool) {
result = data ?1:0;
} else {
throwConversionException(target:int, data: data);
}
if (min !=null&& result < min) {
result = min;
}
if (max !=null&& result > max) {
result = max;
}
return result;
}
int_stringToInt(String value) =>int.tryParse(value) ??double.parse(value).round();
Solutions choices to implement custom validation / conversions:
Define typedefs like typedef StringConverter = String Function(Object data) that could be given as an optional param in "getXXX" and "tryXXX" methods. These optional converts would convert raw data + also validate as necessary.
Define typedefs like typedef StringValidator = String Function(String value) that could be given as an optional param in "getXXX" and "tryXXX" methods. These optional validators would validate a converter value, and throw ValidationException (extending FormatException) if a value is not allowed by domain specific rules.
Keep "getXXX" and "tryXXX" methods as they are, no validation by default on DataObject and DataArray, but those classes could be extended for some custom domain case to bind validation logic of some kind.
Of course there could be other alternatives too...
Easier input data validation when consuming external data (like JSON) via
DataObject
andDataArray
classes on domain model classes.Also some validation helper functions.
Consider also relation to value conversion functions like
toIntValue()
currently available.The text was updated successfully, but these errors were encountered: