Skip to content

Commit

Permalink
Merge pull request #4 from skillbox-koutja/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
DanielCardonaRojas authored Oct 10, 2023
2 parents a4f5afc + 13d55fa commit cb5f0b0
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ final validator = Verify.inOrder<SignUpFormState>([
final Map<FormField, SignUpError> errorMap = validator
.verify<SignUpError>(someState)
.groupedErrorsBy((error) => error.field);
.errorsGroupedBy((error) => error.field);
```

## Sequencing
Expand Down
2 changes: 1 addition & 1 deletion example/lib/bloc/signup/signup_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {

final errors = signUpValidation
.verify<SignUpError>(newState)
.groupedErrorsBy((error) => error.field)
.errorsGroupedBy((error) => error.field)
.messages;

yield newState.copyWith(errors: errors);
Expand Down
8 changes: 4 additions & 4 deletions example/lib/bloc/signup/signup_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import 'package:verify/verify.dart';
import 'package:verify_example/bloc/signup/signup_bloc.dart';
import 'package:verify_example/bloc/signup/signup_error.dart';

final Validator_<SignUpState> signUpValidation = Verify.all<SignUpState>([
final Validator<SignUpState> signUpValidation = Verify.all<SignUpState>([
Verify.subject<SignUpState>().checkField(
(state) => state.password,
Verify.property<String?>((s) => s != null && s.length > 4,
Verify.that<String?>((s) => s != null && s.length > 4,
error: SignUpError(
field: SignUpFormField.password,
message: 'incorrect length',
))),
Verify.subject<SignUpState>().checkField(
(state) => state.email,
Verify.property<String?>((s) => s?.contains('@') ?? false,
Verify.that<String?>((s) => s?.contains('@') ?? false,
error: SignUpError(
field: SignUpFormField.email,
message: 'invalid format',
))),
Verify.property((SignUpState state) => state.confirmation == state.password,
Verify.that((SignUpState state) => state.confirmation == state.password,
error: SignUpError(
field: SignUpFormField.passwordConfirmation,
message: 'not match',
Expand Down
6 changes: 3 additions & 3 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'package:dartz/dartz.dart';

import 'base_types.dart';
import 'package:verify/src/base_types.dart';

/// Extensions to facilitate manipulating validation results.
extension ValidationResult<T, E extends ValidationError> on Either<List<E>, T> {
/// Gets the first error according to the composition sequence used
/// creating of calling validation result
E? get firstError {
return fold((errors) => errors.first, (_) => null);
return fold((errors) => errors.isEmpty ? null : errors.first , (_) => null);
}

/// Returns a map of grouped validatiton errors keyed by a formfield type.
/// Returns a map of grouped validation errors keyed by a formfield type.
///
/// The provided selector must be a the field in the provided error that uniquely
/// identifies the corresponding form field.
Expand Down
3 changes: 2 additions & 1 deletion lib/src/verify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ extension ValidatorUtils<S, T> on ValidatorT<S, T> {
ErrorType? firstError<ErrorType>(S subject) {
return this(subject).leftMap((errors) {
final Iterable<ErrorType> filtered = errors.whereType();
return filtered.toList().first;
final list = filtered.toList();
return list.isEmpty ? null : list.first;
}).fold((l) => l, (r) => null);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/verify_factories.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'verify.dart';
///
/// All the methods in this scope are static
extension Verify on ValidatorT {
/// Creates an always succeding validator
/// Creates an always succeeding validator
///
/// Ignores input and always returns the provided value.
static Validator<S> valid<S>(S subject) {
Expand Down
11 changes: 2 additions & 9 deletions test/verify_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import 'data/error.dart';
import 'data/product_error.dart';
import 'data/user.dart';

class ValidateString {
static Validator<String> length(int length,
{required ValidationError error}) {
return Verify.that((s) => s.length == length, error: error);
}
}

void main() {
final listEquality = const ListEquality().equals;

Expand Down Expand Up @@ -131,7 +124,7 @@ void main() {
assert(result.isLeft());
});

test('Can create and run an always succeding validator', () {
test('Can create and run an always succeeding validator', () {
final validator = Verify.valid(tUserGood);
final result = validator.verify(tUserBad);
assert(result.isRight());
Expand Down Expand Up @@ -211,7 +204,7 @@ void main() {
});

test(
'performing a succeding check on a failing validator returns a single value error list',
'performing a succeeding check on a failing validator returns a single value error list',
() {
final parentError = Error('some errror');
const tUser = User('', '', 10);
Expand Down

0 comments on commit cb5f0b0

Please sign in to comment.