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
  • Nullable parameters
  • The @validParam Annotation
  • The @invalidParam Annotation
  1. Entities
  2. SimpleEntity

Special Cases

PreviousCreating a SimpleEntityNextIterableEntity & Iterable2Entity

Nullable parameters

When a SimpleEntity contains a nullable modddel, when it is null, it is considered valid during the contentValidationStep.

If you want a nullable modddel to cause a failure when it's null, you should use .

The @validParam Annotation

Sometimes, you want a SimpleEntity to contain a ValidModddel (1), or a parameter that isn't a modddel and that should be considered as being valid (2). In these cases, you can annotate it with @validParam.

Example :

factory Person({
    required FullName name,
    /// (1)
    @validParam required ValidAge age,
    /// (2) More frequent usecase
    @validParam required bool isMarried,
  }) { ...

In this example, the contentValidation will be skipped for both age and isMarried.

The parameters of a SimpleEntity can't all be annotated with @validParam. If you feel the need to do so, and either :

  • Your entity only has one validation which is the contentValidation : This would mean that the entity would always be valid, which is not allowed. For an alternative,

  • Your entity has multiple validations : Use a instead.

The @invalidParam Annotation

Sometimes (rarely), you want a SimpleEntity to contain an InvalidModddel. In that case, you can annotate it with @invalidParam.

Example :

factory FreeArticle({
    required Description description,
    @invalidParam required InvalidPrice? price,
  }) { ...

In this example, InvalidPrice is the invalid union-case of a Price modddel. If price is null, then it's considered valid during the contentValidation. However, if it's not null, then the contentValidation fails with its failures.

If FreeArticle passes the contentValidationStep, it means that price is null. As a result, the union-cases that come after the contentValidationStep will hold the null version of price : Null price. We call this the Null param transformation.

The parameter annotated with @invalidParam must be nullable. That's because, if if is non-nullable, the SimpleEntity would always be invalid, while fundamentally a Modddel can either be valid or invalid.

For an alternative, see

the @NullFailure annotation
this section.
see this section.
MultiValueObject