Modddels
HomeGithubPub.dev
v0.1
v0.1
  • Motivation
  • Setup
  • Generalities
    • Modddels Overview
    • Validations & Validation steps
    • Structure of a Modddel
    • Member parameters & Dependency parameters
    • Usage
      • Data equality & toString
      • Pattern matching
      • Reading the fields
      • Reading the failures
      • CopyWith
  • ValueObjects
    • ValueObjects Overview
    • Creating a ValueObject
  • Entities
    • Entities Overview
    • ContentValidation & ContentFailure
    • ValidationSteps in Entities
    • SimpleEntity
      • Creating a SimpleEntity
      • Special Cases
    • IterableEntity & Iterable2Entity
      • IterableEntity / Iterable2Entity kinds
      • The Type Template
      • Creating an IterableEntity / Iterable2Entity
      • InvalidMembers Description
      • Special Cases
      • Creating your own IterableEntity / Iterable2Entity kind
  • Advanced Notions
    • The NullFailure Annotation
    • Using multiple parameters annotations
    • Class Hierarchy of ValueObjects
    • Class Hierarchy of Entities
    • Models that are always valid / invalid
  • Union of Modddels
    • Union of Modddels Overview
    • Basic Usage
    • Case-modddels pattern matching
    • Default Factory Constructor
    • Shared Properties
    • The Validate Methods
    • CopyWith
  • Unit-Testing Of Modddels
    • Unit-Testing Overview
    • Available Tests
    • Customizing Tests
  • Additional Information
    • Using type aliases
    • Re-running the generator
    • All Available Modddels
    • VsCode Snippets
Powered by GitBook
On this page
  • Steps to create a ValueObject
  • Default validationSteps names
  • Complete Example
  1. ValueObjects

Creating a ValueObject

Steps to create a ValueObject

Declare your class this way :

/// SingleValueObject
class Age extends SingleValueObject<InvalidAge, ValidAge> with _$Age {}

/// MultiValueObject
class GeoPoint extends MultiValueObject<InvalidGeoPoint, ValidGeoPoint>
    with _$GeoPoint {}

Add the imports and part statements, the @Modddel annotation, and the private empty constructor.

Add the factory constructor :

/// SingleValueObject
//...
factory Age(int value) //...

/// MultiValueObject
//...
factory GeoPoint({
  required int latitude,
  required int longitude,
}) //...

Create your failure(s) sealed class(es).

You can now run the generator, and then override and implement the "validate" methods.

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;
}
PreviousValueObjects OverviewNextEntities Overview

Last updated 2 years ago