Creating a SimpleEntity

Steps to create a SimpleEntity

Declare your class this way :

class Person extends SimpleEntity<InvalidPerson, ValidPerson> with _$Person {}

Add the imports and part statements, the @Modddel annotation (don't forget including the contentValidation), and the private empty constructor.

Add the factory constructor. The member parameters of the factory constructor should all be modddels.

//...
factory Person({
  required FullName name,
  required Age age,
}) //...

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

You can now run the generator, and then override and implement the "validate" methods. Note that the "validate" method of the contentValidation is automatically generated, and you should never override it (Your IDE's Quick Fix won't override it neither).

Complete Example

// ... Imports & Part statements 

@Modddel(
  validationSteps: [
    ValidationStep([
      contentValidation,
    ]),
    ValidationStep([
      Validation('blackList', FailureType<PersonBlackListFailure>()),
    ])
  ],
)
class Person extends SimpleEntity<InvalidPerson, ValidPerson> with _$Person {
  Person._();

  factory Person({
    required Age age,
    required FullName name,
  }) {
    return _$Person._create(age: age, name: name);
  }

  @override
  Option<PersonBlackListFailure> validateBlackList(person) {
    /// Notice how we can directly access the fields of `age` and `name`,
    /// because at this point they are ValidModddels.
    if (person.name.firstName == 'Dash' &&
        person.name.lastName == 'Birdy' &&
        person.age.value == 28) {
      return some(const PersonBlackListFailure.blackListed());
    }
    return none();
  }
}

@freezed
class PersonBlackListFailure extends ValueFailure
    with _$PersonBlackListFailure {
  const factory PersonBlackListFailure.blackListed() = _BlackListed;
}