Value brings simple way of defining Value Objects in Dart. It uses power of Freezed package and its Union types.
The main reason to use Value is to wrap simple types like String and num into meaningful types that can be easily validated.
Some commonly used value types are provided by the library:
- EmailAddress
- Url
Add to your pubspec.yaml:
dependencies:
value:
git: https://github.com/adriank/value.gitUsing Value turns this code:
final emailAddress = 'me@example.com';
print(emailAddress.runtimeType); // Stringinto a meaningful EmailAddress type that can be validated:
final emailAddress = EmailAddress('me@example.com');
print(emailAddress.runtimeType); // String
print(emailAddress.isValid); // true
final otherEmailAddress = EmailAddress('example.com');
print(invalidEmailAddress().when(
(emailAddress) => 'Valid! $emailAddress',
invalidEmail: (invalidEmail) => 'Not valid! $invalidEmail',
)); // 'Not valid! example.com'Check example directory to learn more:
Read about making invalid state unrepresentable: Making invalid state unrepresentable by Khalil Stemmler Why we should use exhaustive checks by Nicholas Fahrenkrog