pparth602

Untitled

Sep 3rd, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.42 KB | None | 0 0
  1. `````````````````````````````````````````````````````````````````````````````````````````````````````````
  2. value_validator.dart :
  3.  
  4. ```````````````````````````````````````````````````````````````
  5. import 'package:dartz/dartz.dart';
  6. import 'package:statquest/domain/core/failures.dart';
  7.  
  8. Either<ValueFailure<String>, String> validEmailAddress(String input) {
  9.   const emailRegex =
  10.       r"""^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+""";
  11.  
  12.   if (RegExp(emailRegex).hasMatch(input)) {
  13.     return right(input);
  14.   } else {
  15.     return left(ValueFailure.invalidEmail(failedValue: input));
  16.   }
  17. }
  18.  
  19. Either<ValueFailure<String>, String> validatePassword(String input) {
  20.   if (input.length >= 6) {
  21.     return right(input);
  22.   } else {
  23.     return left(ValueFailure.shortPassword(failedValue: input));
  24.   }
  25. }
  26.  
  27. `````````````````````````````````````````````````````````````````````````````````````````````````````````
  28. For Value_equality : value_object.dart :
  29.  
  30. ```````````````````````````````````````````````````````````````
  31. import 'package:dartz/dartz.dart';
  32. import 'package:flutter/foundation.dart';
  33. import 'package:freezed_annotation/freezed_annotation.dart';
  34. import 'package:statquest/domain/core/failures.dart';
  35.  
  36. @immutable
  37. abstract class ValueObject<T> {
  38.   const ValueObject();
  39.   Either<ValueFailure<T>, T> get value;
  40.  
  41.   @override
  42.   String toString() => 'Value(value: $value)';
  43.  
  44.   @override
  45.   bool operator ==(Object o) {
  46.     if (identical(this, o)) return true;
  47.  
  48.     return o is ValueObject && o.value == value;
  49.   }
  50.  
  51.   @override
  52.   int get hashCode => value.hashCode;
  53. }
  54. `````````````````````````````````````````````````````````````````````````````````````````````````````````
  55. for EmailAddress and : value_object.dart
  56. ````````````````````````````````````````````````
  57. import 'package:dartz/dartz.dart';
  58. import 'package:statquest/domain/core/failures.dart';
  59. import 'package:statquest/domain/core/value_objects.dart';
  60. import 'package:statquest/domain/core/value_validators.dart';
  61.  
  62. class EmailAddress extends ValueObject<String> {
  63.   final Either<ValueFailure<String>, String> value;
  64.   bool isValid() => value.isRight();
  65.  
  66.   factory EmailAddress(String input) {
  67.     assert(input != null);
  68.     return EmailAddress._(
  69.       validEmailAddress(input),
  70.     );
  71.   }
  72.  
  73.   EmailAddress._(this.value);
  74. }
  75.  
  76. class Password extends ValueObject<String> {
  77.   final Either<ValueFailure<String>, String> value;
  78.  
  79.   factory Password(String input) {
  80.     assert(input != null);
  81.     return Password._(
  82.       validatePassword(input),
  83.     );
  84.   }
  85.  
  86.   Password._(this.value);
  87. }
  88. `````````````````````````````````````````````````````````````````````````````````````````````````````````
  89. Please if you know the answer the following question. Please reply it helps a lot.
  90.  
  91. 01. How EmailAddress and Password class object use value_object class for checking the value equality?
  92.  
  93. 02. Could you please explain the flow of email and password provided to constructor of EmailAddress  and Password  class and Value equality and value validator class.
  94.  
  95. 03.Does my understanding is correct please give some comments : when we pass String to EmailAddress and Password class Constructor. It is passed to the factory EmailAddress constructor first then private constructor of  EmailAddress class=> EmailAddress_.() and Password class  => Password_.()  and store passed string of  factory EmailAddress as a new value object that contains validated email_id.
Advertisement
Add Comment
Please, Sign In to add comment