-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from kaziwaseef/feat/number-validator-methods
Feat: min and max validator methods for int, double and number
- Loading branch information
Showing
23 changed files
with
832 additions
and
50 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
packages/luthor/lib/src/validations/numbers/max_validation.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:luthor/src/validation.dart'; | ||
|
||
class NumberMaxValidation extends Validation { | ||
num maxValue; | ||
String? customMessage; | ||
|
||
NumberMaxValidation({ | ||
required this.maxValue, | ||
String? message, | ||
}) : customMessage = message; | ||
|
||
@override | ||
bool call(String? fieldName, Object? value) { | ||
super.call(fieldName, value); | ||
|
||
if (value == null) return true; | ||
if (value is! num) return false; | ||
|
||
return value <= maxValue; | ||
} | ||
|
||
@override | ||
String get message { | ||
if (customMessage != null) return customMessage!; | ||
return '${fieldName ?? 'value'} must be less than or equal to $maxValue'; | ||
} | ||
|
||
@override | ||
Map<String, List<String>>? get errors => null; | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/luthor/lib/src/validations/numbers/min_validation.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:luthor/src/validation.dart'; | ||
|
||
class NumberMinValidation extends Validation { | ||
num minValue; | ||
String? customMessage; | ||
|
||
NumberMinValidation({ | ||
required this.minValue, | ||
String? message, | ||
}) : customMessage = message; | ||
|
||
@override | ||
bool call(String? fieldName, Object? value) { | ||
super.call(fieldName, value); | ||
|
||
if (value == null) return true; | ||
if (value is! num) return false; | ||
|
||
return value >= minValue; | ||
} | ||
|
||
@override | ||
String get message { | ||
if (customMessage != null) return customMessage!; | ||
return '${fieldName ?? 'value'} must be greater than or equal to $minValue'; | ||
} | ||
|
||
@override | ||
Map<String, List<String>>? get errors => null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:luthor/src/validations/numbers/max_validation.dart'; | ||
import 'package:luthor/src/validations/numbers/min_validation.dart'; | ||
import 'package:luthor/src/validator.dart'; | ||
|
||
/// Validator for ints. | ||
class DoubleValidator extends Validator { | ||
DoubleValidator({super.initialValidations}); | ||
|
||
/// Validates that the int is greater than or equal to minValue. | ||
DoubleValidator min(double minValue, {String? message}) { | ||
validations.add(NumberMinValidation(minValue: minValue, message: message)); | ||
return this; | ||
} | ||
|
||
/// Validates that the int is less than or equal to minValue. | ||
DoubleValidator max(double maxValue, {String? message}) { | ||
validations.add(NumberMaxValidation(maxValue: maxValue, message: message)); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:luthor/src/validations/numbers/max_validation.dart'; | ||
import 'package:luthor/src/validations/numbers/min_validation.dart'; | ||
import 'package:luthor/src/validator.dart'; | ||
|
||
/// Validator for ints. | ||
class IntValidator extends Validator { | ||
IntValidator({super.initialValidations}); | ||
|
||
/// Validates that the int is greater than or equal to minValue. | ||
IntValidator min(int minValue, {String? message}) { | ||
validations.add(NumberMinValidation(minValue: minValue, message: message)); | ||
return this; | ||
} | ||
|
||
/// Validates that the int is less than or equal to minValue. | ||
IntValidator max(int maxValue, {String? message}) { | ||
validations.add(NumberMaxValidation(maxValue: maxValue, message: message)); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:luthor/src/validations/numbers/max_validation.dart'; | ||
import 'package:luthor/src/validations/numbers/min_validation.dart'; | ||
import 'package:luthor/src/validator.dart'; | ||
|
||
/// Validator for nums. | ||
class NumberValidator extends Validator { | ||
NumberValidator({super.initialValidations}); | ||
|
||
/// Validates that the int is greater than or equal to minValue. | ||
NumberValidator min(num minValue, {String? message}) { | ||
validations.add(NumberMinValidation(minValue: minValue, message: message)); | ||
return this; | ||
} | ||
|
||
/// Validates that the int is less than or equal to minValue. | ||
NumberValidator max(num maxValue, {String? message}) { | ||
validations.add(NumberMaxValidation(maxValue: maxValue, message: message)); | ||
return this; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
packages/luthor/test/validations/doubles/double_max_validation_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:luthor/luthor.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test( | ||
'should return true if the double is less than or equal to maxValue', | ||
() { | ||
final result = l.double().max(3.0).validateValue(2.0); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
expect(result.data, 2.0); | ||
case SingleValidationError(data: _, errors: _): | ||
fail('should not have errors'); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return false if the double is greater than maxValue', | ||
() { | ||
final result = l.double().max(3.0).validateValue(4.0); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
fail('should not be a success'); | ||
case SingleValidationError(data: _, errors: final errors): | ||
expect(errors, ['value must be less than or equal to 3.0']); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return true when the value is null', | ||
() { | ||
final result = l.double().max(3.0).validateValue(null); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
expect(result.data, isNull); | ||
case SingleValidationError(data: _, errors: _): | ||
fail('should not have errors'); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return false if the value is null with required()', | ||
() { | ||
final result = l.double().max(3.0).required().validateValue(null); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
fail('should not be a success'); | ||
case SingleValidationError(data: _, errors: final errors): | ||
expect(errors, ['value is required']); | ||
} | ||
}, | ||
); | ||
} |
60 changes: 60 additions & 0 deletions
60
packages/luthor/test/validations/doubles/double_min_validation_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:luthor/luthor.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test( | ||
'should return true if the double is greater than or equal to minValue', | ||
() { | ||
final result = l.double().min(3.0).validateValue(4.0); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
expect(result.data, 4.0); | ||
case SingleValidationError(data: _, errors: _): | ||
fail('should not have errors'); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return false if the double is less than minValue', | ||
() { | ||
final result = l.double().min(3.0).validateValue(2.0); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
fail('should not be a success'); | ||
case SingleValidationError(data: _, errors: final errors): | ||
expect(errors, ['value must be greater than or equal to 3.0']); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return true when the value is null', | ||
() { | ||
final result = l.double().min(3.0).validateValue(null); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
expect(result.data, isNull); | ||
case SingleValidationError(data: _, errors: _): | ||
fail('should not have errors'); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return false if the value is null with required()', | ||
() { | ||
final result = l.double().min(3.0).required().validateValue(null); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
fail('should not be a success'); | ||
case SingleValidationError(data: _, errors: final errors): | ||
expect(errors, ['value is required']); | ||
} | ||
}, | ||
); | ||
} |
60 changes: 60 additions & 0 deletions
60
packages/luthor/test/validations/ints/int_max_validation_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:luthor/luthor.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test( | ||
'should return true if the int is less than or equal to maxValue', | ||
() { | ||
final result = l.int().max(3).validateValue(2); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
expect(result.data, 2); | ||
case SingleValidationError(data: _, errors: _): | ||
fail('should not have errors'); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return false if the int is greater than maxValue', | ||
() { | ||
final result = l.int().max(3).validateValue(4); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
fail('should not be a success'); | ||
case SingleValidationError(data: _, errors: final errors): | ||
expect(errors, ['value must be less than or equal to 3']); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return true when the value is null', | ||
() { | ||
final result = l.int().max(3).validateValue(null); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
expect(result.data, isNull); | ||
case SingleValidationError(data: _, errors: _): | ||
fail('should not have errors'); | ||
} | ||
}, | ||
); | ||
|
||
test( | ||
'should return false if the value is null with required()', | ||
() { | ||
final result = l.int().max(3).required().validateValue(null); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
fail('should not be a success'); | ||
case SingleValidationError(data: _, errors: final errors): | ||
expect(errors, ['value is required']); | ||
} | ||
}, | ||
); | ||
} |
Oops, something went wrong.