Skip to content

Commit

Permalink
refactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Aug 27, 2024
1 parent 63c9154 commit 0689f65
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/src/localization/language.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ abstract class Language {
code.equalTo: "'{PropertyName}' must be equal to '{ComparisonValue}'.",
code.greaterThan: "'{PropertyName}' must be greater than '{ComparisonValue}'.",
code.isEmpty: "'{PropertyName}' must be empty.",
code.isNotNull: "'{PropertyName}' must not be empty.",
code.isNull: "'{PropertyName}' must be empty.",
code.isNotNull: "'{PropertyName}' must not be null.",
code.isNull: "'{PropertyName}' must be null.",
code.lessThan: "'{PropertyName}' must be less than '{ComparisonValue}'.",
code.matchesPattern: "'{PropertyName}' is not in the correct format.",
code.max: "'{PropertyName}' must be less than or equal to {MaxValue}. You entered {PropertyValue}.",
Expand Down
24 changes: 19 additions & 5 deletions lib/src/validations/is_null_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ extension IsNullValidation<T> on SimpleValidationBuilder<T?> {
/// ruleFor((user) => user.name, key: 'name') // optional field
/// .isNull();
/// ```
SimpleValidationBuilder<T?> isNull({String message = 'Must be null', String code = 'must_be_null'}) {
return must(
(value) => value == null,
message,
code,
/// String format args:
/// - **{PropertyName}**: The name of the property.
///
SimpleValidationBuilder<T?> isNull({String? message, String? code}) {
return use(
(value, entity) {
if (value == null) return null;

final currentCode = code ?? Language.code.isNull;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
},
defaultMessage: message,
);

return ValidationError(message: currentMessage, code: currentCode);
},
);
}
}
1 change: 1 addition & 0 deletions test/mocks/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class UserModel {
String email = '';
String password = '';
String confirmPassword = '';
String? description;
int age = 0;
String phone = '';
}
Expand Down
25 changes: 25 additions & 0 deletions test/src/validations/is_not_null_validation_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:lucid_validation/lucid_validation.dart';
import 'package:test/test.dart';

import '../../mocks/mocks.dart';

void main() {
test('is not null validation ...', () {
final validator = TestLucidValidator<UserModel>();
validator
.ruleFor((e) => e.description, key: 'description') //
.isNotNull();

final user = UserModel();

final result = validator.validate(user);

expect(result.isValid, false);

expect(result.errors.length, 1);

final error = result.errors.first;

expect(error.message, r"'description' must not be null.");
});
}
26 changes: 26 additions & 0 deletions test/src/validations/is_null_validation_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:lucid_validation/lucid_validation.dart';
import 'package:test/test.dart';

import '../../mocks/mocks.dart';

void main() {
test('is null validation ...', () {
final validator = TestLucidValidator<UserModel>();

validator
.ruleFor((e) => e.description, key: 'description') //
.isNull();

final user = UserModel()..description = 'description';

final result = validator.validate(user);

expect(result.isValid, false);

expect(result.errors.length, 1);

final error = result.errors.first;

expect(error.message, r"'description' must be null.");
});
}

0 comments on commit 0689f65

Please sign in to comment.