Modddels
HomeGithubPub.dev
v0.2
v0.2
  • 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
  • Adding the contentValidation
  • The ContentFailure
  1. Entities

ContentValidation & ContentFailure

As explained earlier, the "contentValidation" is a validation during which the modddels held inside the Entity are verified. For an Entity to pass the contentValidation, all of the modddels held inside it must be valid. If any of the modddels are invalid, the contentValidation fails with a ContentFailure that holds the invalid modddel(s) and their failure(s).

Adding the contentValidation

When creating an Entity, you should always add the contentValidation in the @Modddel annotation.

By definition, the contentValidation is the validation which failureType is ContentFailure, so you can provide it this way :

@Modddel(
  validationSteps: [
    ValidationStep([
      // Validation(...),
      Validation('myContentValidationName', FailureType<ContentFailure>())
    ]),
    // ValidationStep(...),
  ],
)

As a good practice, it's best to name the contentValidation "content". To make it easier, you can use the predefined contentValidation constant :

@Modddel(
  validationSteps: [
    ValidationStep([
      // Validation(...),
      contentValidation,
    ]),
    // ValidationStep(...),
  ],
)

This constant evaluates to Validation('content', FailureType<ContentFailure>()).

The ContentFailure

The ContentFailure is the failure of the contentValidation. It holds a list of all the invalid modddels of the Entity, which you can access using the invalidMembers getter.

final List<ModddelInvalidMember> invalidMembers = contentFailure.invalidMembers;

For example : Let's say we have a Person SimpleEntity that has two fields : Age age and FullName name. It has only one validationStep, that only contains the contentValidation.

// In this example, the age is invalid and the name is valid.
final person = Person(
  age: Age(10),
  name: FullName(firstName: 'Dash', lastName: 'Birdy'),
);

person.map(
  valid: (validPerson) {},
  invalidMid: (invalidPersonMid) {
    final contentFailure = invalidPersonMid.contentFailure;

    print(contentFailure);
    // Output :
    // ContentFailure(invalidMembers: {age: [AgeLegalFailure.minor()]})
  },
);

Notice how the contentFailure's toString describes which members are invalid and what failure(s) they hold.

PreviousEntities OverviewNextValidationSteps in Entities

ModddelInvalidMember is a wrapper class that holds an invalid modddel and its description. The description is used in the toString method of the ContentFailure. For a SimpleEntity, the description is simply the name of the parameter (Ex : "age", "name"...). For an IterableEntity/Iterable2Entity, it's usually the index of the invalid modddel (Ex : "item 0", "entry 0"...) .

(See this section for more details)