diff --git a/lib/src/localization/language.dart b/lib/src/localization/language.dart index 18b6cbc..edc6b7b 100644 --- a/lib/src/localization/language.dart +++ b/lib/src/localization/language.dart @@ -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}.", diff --git a/lib/src/validations/is_null_validation.dart b/lib/src/validations/is_null_validation.dart index c519509..33f9732 100644 --- a/lib/src/validations/is_null_validation.dart +++ b/lib/src/validations/is_null_validation.dart @@ -18,11 +18,25 @@ extension IsNullValidation on SimpleValidationBuilder { /// ruleFor((user) => user.name, key: 'name') // optional field /// .isNull(); /// ``` - SimpleValidationBuilder 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 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); + }, ); } } diff --git a/test/mocks/mocks.dart b/test/mocks/mocks.dart index 00ad340..44d6fc6 100644 --- a/test/mocks/mocks.dart +++ b/test/mocks/mocks.dart @@ -5,6 +5,7 @@ class UserModel { String email = ''; String password = ''; String confirmPassword = ''; + String? description; int age = 0; String phone = ''; } diff --git a/test/src/validations/is_not_null_validation_test.dart b/test/src/validations/is_not_null_validation_test.dart new file mode 100644 index 0000000..a876ba5 --- /dev/null +++ b/test/src/validations/is_not_null_validation_test.dart @@ -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(); + 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."); + }); +} diff --git a/test/src/validations/is_null_validation_test.dart b/test/src/validations/is_null_validation_test.dart new file mode 100644 index 0000000..09d81de --- /dev/null +++ b/test/src/validations/is_null_validation_test.dart @@ -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(); + + 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."); + }); +}