Skip to content

Commit

Permalink
TW-2046 added Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledNjim committed Nov 3, 2024
1 parent f8d3a83 commit 3262301
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 1 deletion.
3 changes: 2 additions & 1 deletion assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -3095,5 +3095,6 @@
},
"enable_notifications":"Enable notifications",
"disable_notifications":"Disable notifications",
"logoutDialogWarning": "You will lose access to encrypted messages. We recommend that you enable chat backups before logging out"
"logoutDialogWarning": "You will lose access to encrypted messages. We recommend that you enable chat backups before logging out",
"this_field_cannot_be_blank":"This field cannot be blank"
}
8 changes: 8 additions & 0 deletions lib/domain/app_state/validator/verify_name_view_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:fluffychat/presentation/state/failure.dart';
import 'package:fluffychat/presentation/state/success.dart';

class VerifyNameViewState extends UIState {}

class VerifyNameFailure extends FeatureFailure {
const VerifyNameFailure(dynamic exception) : super(exception: exception);
}
17 changes: 17 additions & 0 deletions lib/domain/exception/verify_name_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:equatable/equatable.dart';

abstract class VerifyNameException extends Equatable implements Exception {
static const spaceOnlyWithinName = 'The name cannot contain only spaces';

final String? message;

const VerifyNameException(this.message);

@override
List<Object?> get props => [message];
}

class NameWithSpaceOnlyException extends VerifyNameException {
const NameWithSpaceOnlyException()
: super(VerifyNameException.spaceOnlyWithinName);
}
20 changes: 20 additions & 0 deletions lib/domain/model/extensions/list_validator_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart';
import 'package:fluffychat/domain/model/verification/new_name_request.dart';
import 'package:fluffychat/domain/model/verification/validator.dart';

extension ListValidatorExtension on List<Validator> {
Either<Failure, Success> getValidatorNameViewState(
NewNameRequest newNameRequest,
) {
for (final validator in this) {
final either = validator.validate(newNameRequest);
if (either.isLeft()) {
return either;
}
}
return Right<Failure, Success>(VerifyNameViewState());
}
}
14 changes: 14 additions & 0 deletions lib/domain/model/extensions/validator_failure_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart';
import 'package:fluffychat/domain/exception/verify_name_exception.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';

extension ValidatorFailureExtension on VerifyNameFailure {
String getMessage(BuildContext context) {
if (exception is NameWithSpaceOnlyException) {
return L10n.of(context)!.this_field_cannot_be_blank;
} else {
return '';
}
}
}
20 changes: 20 additions & 0 deletions lib/domain/model/verification/composite_name_validator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart';
import 'package:fluffychat/domain/model/verification/new_name_request.dart';
import 'package:fluffychat/domain/model/verification/validator.dart';
import 'package:fluffychat/domain/model/extensions/list_validator_extension.dart';

class CompositeNameValidator extends Validator<NewNameRequest> {
final List<Validator> _listValidator;

CompositeNameValidator(this._listValidator);

@override
Either<Failure, Success> validate(NewNameRequest value) {
return _listValidator.isNotEmpty
? _listValidator.getValidatorNameViewState(value)
: Right<Failure, Success>(VerifyNameViewState());
}
}
22 changes: 22 additions & 0 deletions lib/domain/model/verification/name_with_space_only_validator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart';
import 'package:fluffychat/domain/exception/verify_name_exception.dart';
import 'package:fluffychat/domain/model/verification/new_name_request.dart';
import 'package:fluffychat/domain/model/verification/validator.dart';

class NameWithSpaceOnlyValidator extends Validator<NewNameRequest> {
@override
Either<Failure, Success> validate(NewNameRequest value) {
if (value.value != null &&
value.value!.isNotEmpty &&
value.value!.trim().isEmpty) {
return const Left<Failure, Success>(
VerifyNameFailure(NameWithSpaceOnlyException()),
);
} else {
return Right<Failure, Success>(VerifyNameViewState());
}
}
}
10 changes: 10 additions & 0 deletions lib/domain/model/verification/new_name_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:equatable/equatable.dart';

class NewNameRequest with EquatableMixin {
final String? value;

NewNameRequest(this.value);

@override
List<Object?> get props => [value];
}
7 changes: 7 additions & 0 deletions lib/domain/model/verification/validator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';

abstract class Validator<T> {
Either<Failure, Success> validate(T value);
}
21 changes: 21 additions & 0 deletions lib/domain/usecase/verify_name_interactor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart';
import 'package:fluffychat/domain/model/verification/composite_name_validator.dart';
import 'package:fluffychat/domain/model/verification/new_name_request.dart';
import 'package:fluffychat/domain/model/verification/validator.dart';

class VerifyNameInteractor {
Either<Failure, Success> execute(
String? newName,
List<Validator> listValidator,
) {
try {
return CompositeNameValidator(listValidator)
.validate(NewNameRequest(newName));
} catch (exception) {
return Left<Failure, Success>(VerifyNameFailure(exception));
}
}
}

0 comments on commit 3262301

Please sign in to comment.