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;

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).

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.

Last updated