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
  1. Union of Modddels

The Validate Methods

PreviousShared PropertiesNextCopyWith

In unions of modddels, the validate methods also benefit from the shared properties feature and the case-modddels pattern matching.

Continuing with our Weather example , here is an example of an implementation of the validateHabitable method.

@override
Option<WeatherHabitableFailure> validateHabitable(weather) {
  // [temperature] is a shared property, so we can directly access it 
  // from [weather] :
  final temperature = weather.temperature;

  if (temperature > 60) {
    return some(const WeatherHabitableFailure.tooHot());
  }

  if (temperature < -10) {
    return some(const WeatherHabitableFailure.tooCold());
  }

  // You can use the case-modddels pattern matching methods on [weather] :
  return weather.maybeMapWeather(
    rainy: (rainy) => rainy.rainIntensity > 2.5
        ? some(const WeatherHabitableFailure.flooding())
        : none(),
    orElse: () => none(),
  );
}

In this implementation, the 'habitable' validation will fail under the following conditions :

  • No matter the weather kind (Sunny or Rainy) : If the temperature is more than 60 or less than -10, the validation fails

  • For the Rainy weather : If the rainIntensity is more than 2.5, the validation fails.

(you can find it here)