valid_value_objects 0.1.0-nullsafety.1 copy "valid_value_objects: ^0.1.0-nullsafety.1" to clipboard
valid_value_objects: ^0.1.0-nullsafety.1 copied to clipboard

outdated

A new Flutter package project.

Valid value objects #

Dart package to create immutable and validated value objects.

Table of contents #

Overview #

You might be familiar with Value Objects from DDD concepts. Value Objects provides

  • immutability
  • type safety
  • code readability
  • validation
    • Objects are only instantiated if input parameter(s) are valid.
    • Making invalid value objects unrepresentable.

The package provides the following value objects:

  • BoundaryDateTime
  • EmailAddress
  • IPAddress
  • ISBN
  • Name, FirstName, MiddleName, LastName
  • Password
  • PhoneNumber
  • UniqueId

Note: Create your own value object by extending ValueObject class

Setup #

Import it in your pubspec.yaml under dependencies

dependencies:
  valid_value_objects: ^0.1.0

Install it with pub get or flutter pub get

Import it in your dart files

import 'package:valid_value_objects/valid_value_objects.dart';

Usage #

Some examples on how to create value objects.

Possible expections at object creation: #

  • RequiredValueException
  • InvalidValueException
  • TooShortValueException
  • TooLongValueException

Note: All of the above exceptions are instance of ValueException.

Object creation examples: #

EmailAddress

EmailAddress instantiation:

try {
  EmailAddress e = EmailAddress('example@gmail.com');
} on RequiredValueException {
  // happens if param is null or empty
} on InvalidValueException {
  // happens if param doesn't match regex
}

// OR handle all exception at once:

try {
   EmailAddress e = EmailAddress('example@gmail.com');
} on ValueException catch (e) {  
  // catches all value exceptions
  print(e.invalidValue);
  print(e.code);
  print(e.message);
}

Password

You may want to configure the default password requirements.

It can be done like this:

Password.minChar = ...;
Password.maxChar = ...;
Password.mustContainUpperChar = ...;
Password.mustContainNumeric = ...;
...

Or:

Password.setRequirements(
  minChar: 10,
  maxChar: 50,
  mustContainLowerChar: false,
  mustContainNumeric: false,
  mustContainUpperChar: false,
  mustContainSpecialChar: false,
);

Password instantiation:

try {
  Password psw = Password('xzY12&#eA');
} on RequiredValueException {
  ...
} on TooShortValueException {
  ...
} on TooLongValueException {
  ...
} on InvalidValueException {
  ...
} 

Serialization #

Above validation and type safety every value object provides a toJson and fromJson method. By that fact, storing them in a NoSQL database is easy. The package is compatible with json_serialization package, but the package on it's own does not depend on it.

Costumize the key fields if required. Every value object has a static key field which are used by toJson and fromJson functions.

Default keys:

  • EmailAddress --> "email"
  • Name --> "name",
  • FirstName --> "firstName",
  • MiddleName --> "middleName",
  • LastName --> "lastName"
  • IPAddress --> "ip"
  • ISBN --> "isbn"
  • Password --> "password"
  • PhoneNumber --> "phone"
  • UniqueId --> "id"

Finding a key in a nested Map object? No problem.

const simpleMap = {'email' : 'xy@gmail.com'};
const nestedMap = {
  'person' : {
    'contact' : {
      'phone' : '...',
      'email' : 'xy@gmail.com',
    },
  },
};

EmailAddress.fromJson(simpleMap) == EmailAddress.fromJson(nestedMap) // true

Check the example folder for more example and clarification.

4
likes
0
pub points
9%
popularity

Publisher

unverified uploader

A new Flutter package project.

Repository (GitLab)
View/report issues

Documentation

Documentation

License

unknown (LICENSE)

Dependencies

meta

More

Packages that depend on valid_value_objects