InvalidMembers Description

As explained previously, the ContentFailure holds a list : List<ModddelInvalidMember> invalidMembers, where ModddelInvalidMember is a wrapper class that holds an invalid modddel and its description. This description is used in the toString method of the ContentFailure. It varies depending on the entity kind.

For IterableEntity

The description is by default "item $index", except for the MappedKeys/MappedValues kinds where it's "entry $index".

As we know already, the collection of an IterableEntity can be converted to an iterable. The index that we talk about here is the index of a modddel in that iterable.

You can customize the description for any IterableEntity by overriding the $description method.

For example : Let's say we have a BookList ListEntity that holds a list of books.

// Inside your `BookList` class :
@override
String $description(int index) => 'Book number ${index + 1}';

You can also optionally override the $descriptionDetails method. This method has two parameters : the modddel that is held inside the collection and its index. It returns a string that will be appended to the description, in parenthesis.

In our example :

// Inside your `BookList` class :
String? $descriptionDetails(BaseModddel modddel, int index) =>
    'id = ${(modddel as Book).id}';

// Example of a full description : "Book number 1 (id = 1234)"

You can cast the modddel to whatever type the modddel held inside the collection is.

For example : If you have a ListEntity that holds a list of Book, you can safely cast modddel like so :

String? $descriptionDetails(BaseModddel modddel, int index) {
  final casted = modddel as Book;
  // ...
}

If you have a ListEntity that is a union of modddels where one case-modddel holds a list of Book, and the other case-modddel holds a list of Article, you can do :

String? $descriptionDetails(BaseModddel modddel, int index) {
  if(modddel is Book) {
    // ...
  }
  else {
    final casted = modddel as Article;
    // ...
  } 
}

For Iterable2Entity

The collection of an Iterable2Entity can be converted to two iterables of modddels. Therefore, there are two descriptions, one for each iterable.

By default :

  • The description of the modddels of the first iterable is "first $index", except for the Map kinds (MapEntity...) where it's "key $index".

  • The description of the modddels of the second iterable is "second $index", except for the Map kinds (MapEntity...) where it's "value $index".

You can customize these descriptions by overriding respectively the $description1 and $description2 methods.

You can also optionally override the $descriptionDetails1 and $descriptionDetails2 methods.

Last updated