/// SingleValueObject
class Age extends SingleValueObject<InvalidAge, ValidAge> with _$Age {}
/// MultiValueObject
class GeoPoint extends MultiValueObject<InvalidGeoPoint, ValidGeoPoint>
with _$GeoPoint {}
Add the , the , and the .
Add the :
/// SingleValueObject
//...
factory Age(int value) //...
/// MultiValueObject
//...
factory GeoPoint({
required int latitude,
required int longitude,
}) //...
Default validationSteps names
The name parameter of the ValidationStep is optional. If you omit it, it will be replaced by a default name :
Step
Default name
Example
1
Value1
InvalidAgeValue1
2
Value2
InvalidAgeValue2
3
Value3
InvalidAgeValue3
...
...
...
If there's only one step, then its default name is 'Value' (instead of 'Value1').
Complete Example
// ... Imports & Part statements
@Modddel(
validationSteps: [
ValidationStep([
Validation('location', FailureType<GeoPointLocationFailure>()),
]),
],
)
class GeoPoint extends MultiValueObject<InvalidGeoPoint, ValidGeoPoint> with _$GeoPoint {
GeoPoint._();
factory GeoPoint({
required int latitude,
required int longitude,
}) {
return _$GeoPoint._create(
latitude: latitude,
longitude: longitude,
);
}
@override
Option<GeoPointLocationFailure> validateLocation(geoPoint) {
if (geoPoint.latitude == 0 && geoPoint.longitude == 0) {
return some(const GeoPointLocationFailure.nullIsland());
}
return none();
}
}
@freezed
class GeoPointLocationFailure extends ValueFailure with _$GeoPointLocationFailure{
/// Null Island is the point on the Earth's surface at zero degrees
/// latitude and zero degrees longitude (0°N 0°E)
const factory GeoPointLocationFailure.nullIsland() = _NullIsland;
}
Create your .
You can now run the generator, and then override and implement the .