firestoreodm-flutter icon indicating copy to clipboard operation
firestoreodm-flutter copied to clipboard

Validator over multiple values

Open SunlightBro opened this issue 4 months ago • 0 comments

Feature Request

Consider a odm like this:

@firestoreSerializable
class TimeFrame {
  final Timestamp start;
  final Timestamp end;
  
  TimeFrame(this.lowerLimit, this.upperLimit) {
    _$assertTimeFrame(this);
  }
}

I would like to validate that start is before end.

Idea

abstract class ValidatorWithOther<T> {
  const ValidatorWithOther(this.otherPropertyName);
  final String otherPropertyName;

  void validate(Object? value, T other, String propertyName);
}

that could be extended

class IsAfterValidator extends ValidatorWithOther<Timestamp> {
  const IsAfterValidator(super.otherPropertyName);

  @override
  void validate(Object? value, Timestamp other, String propertyName) {
    // TODO: implement validate
  }
}

And then applied to a property

@firestoreSerializable
class TimeFrame {
  final Timestamp start;
  @IsAfterValidator('start')
  final Timestamp end;
  
  TimeFrame(this.lowerLimit, this.upperLimit) {
    _$assertTimeFrame(this);
  }
}

code generation should throw an exception if the other parameter with the correct Type does not exist. (maybe even a custom_lint)

SunlightBro avatar Oct 09 '24 06:10 SunlightBro