Skip to content

Commit

Permalink
Merge pull request #58 from kaziwaseef/feat/number-validator-methods
Browse files Browse the repository at this point in the history
Feat: min and max validator methods for int, double and number
  • Loading branch information
exaby73 authored Feb 18, 2024
2 parents e45606e + 0b50f96 commit 0fd3a58
Show file tree
Hide file tree
Showing 23 changed files with 832 additions and 50 deletions.
30 changes: 30 additions & 0 deletions packages/luthor/lib/src/validations/numbers/max_validation.dart
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 packages/luthor/lib/src/validations/numbers/min_validation.dart
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;
}
15 changes: 9 additions & 6 deletions packages/luthor/lib/src/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import 'package:luthor/src/validations/number_validation.dart';
import 'package:luthor/src/validations/required_validation.dart';
import 'package:luthor/src/validations/schema_validation.dart';
import 'package:luthor/src/validations/string_validation.dart';
import 'package:luthor/src/validators/double_validator.dart';
import 'package:luthor/src/validators/int_validator.dart';
import 'package:luthor/src/validators/number_validator.dart';
import 'package:luthor/src/validators/string_validator.dart';

typedef FromJson<T> = T Function(Map<String, Object?> json);
Expand Down Expand Up @@ -46,21 +49,21 @@ class Validator {
}

/// Validates that the value is a number (int or double).
Validator number({String? message}) {
NumberValidator number({String? message}) {
validations.add(NumberValidation(message: message));
return this;
return NumberValidator(initialValidations: validations);
}

/// Validates that the value is an int.
Validator int({String? message}) {
IntValidator int({String? message}) {
validations.add(IntValidation(message: message));
return this;
return IntValidator(initialValidations: validations);
}

/// Validates that the value is a double.
Validator double({String? message}) {
DoubleValidator double({String? message}) {
validations.add(DoubleValidation(message: message));
return this;
return DoubleValidator(initialValidations: validations);
}

/// Validates that the value is a bool.
Expand Down
20 changes: 20 additions & 0 deletions packages/luthor/lib/src/validators/double_validator.dart
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;
}
}
20 changes: 20 additions & 0 deletions packages/luthor/lib/src/validators/int_validator.dart
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;
}
}
20 changes: 20 additions & 0 deletions packages/luthor/lib/src/validators/number_validator.dart
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;
}
}
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']);
}
},
);
}
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 packages/luthor/test/validations/ints/int_max_validation_test.dart
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']);
}
},
);
}
Loading

0 comments on commit 0fd3a58

Please sign in to comment.