-
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.
* feat(luthor): added custom validator * feat(luthor_annotation): added custom validator annotation fixup * feat(luthor_generator): added custom validator for generator
- Loading branch information
1 parent
38b94e6
commit 952d09b
Showing
13 changed files
with
169 additions
and
16 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -5,4 +5,6 @@ | |
pubspec.lock | ||
|
||
.fvm/ | ||
.fvmrc | ||
.fvmrc | ||
|
||
.nvim.lua |
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
35 changes: 35 additions & 0 deletions
35
packages/luthor/lib/src/validations/custom_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,35 @@ | ||
import 'package:luthor/src/validation.dart'; | ||
|
||
typedef CustomValidator = bool Function(Object? value); | ||
|
||
class CustomValidation extends Validation { | ||
String? customMessage; | ||
final CustomValidator customValidator; | ||
|
||
CustomValidation( | ||
this.customValidator, { | ||
String? message, | ||
}) : customMessage = message; | ||
|
||
@override | ||
bool call(String? fieldName, Object? value) { | ||
super.call(fieldName, value); | ||
try { | ||
return customValidator(value); | ||
} catch (e, s) { | ||
// ignore: avoid_print | ||
print(e); | ||
// ignore: avoid_print | ||
print(s); | ||
return false; | ||
} | ||
} | ||
|
||
@override | ||
String get message => | ||
customMessage ?? | ||
'${fieldName ?? 'value'} does not pass custom validation'; | ||
|
||
@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
43 changes: 43 additions & 0 deletions
43
packages/luthor/test/validations/custom_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,43 @@ | ||
import 'package:luthor/luthor.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('should return true when custom validator passes', () { | ||
final result = l.custom((value) { | ||
return value! as bool; | ||
}).validateValue(true); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
expect(result.data, true); | ||
case SingleValidationError(data: _, errors: _): | ||
fail('should not have errors'); | ||
} | ||
}); | ||
|
||
test('should return false when custom validator fails', () { | ||
final result = l.custom((value) { | ||
return !(value! as bool); | ||
}).validateValue(true); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
fail('should not be a success'); | ||
case SingleValidationError(data: _, errors: final errors): | ||
expect(errors, ['value does not pass custom validation']); | ||
} | ||
}); | ||
|
||
test('should return false if the value is null with required()', () { | ||
final result = l.required().custom((value) { | ||
return true; | ||
}).validateValue(null); | ||
|
||
switch (result) { | ||
case SingleValidationSuccess(data: _): | ||
fail('should not be a success'); | ||
case SingleValidationError(data: _, errors: final errors): | ||
expect(errors, ['value is required']); | ||
} | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
export './src/luthor.dart'; | ||
export './src/validators/custom.dart'; | ||
export './src/validators/date_time.dart'; | ||
export './src/validators/email.dart'; | ||
export './src/validators/length.dart'; | ||
export './src/validators/max.dart'; | ||
export './src/validators/min.dart'; | ||
export './src/validators/uri.dart'; | ||
export './src/validators/regex.dart'; | ||
export './src/validators/uri.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,6 @@ | ||
class WithCustomValidator { | ||
final String? message; | ||
final bool Function(Object? value) customValidator; | ||
|
||
const WithCustomValidator(this.customValidator, {this.message}); | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
24 changes: 24 additions & 0 deletions
24
packages/luthor_generator/lib/helpers/validations/custom_validations.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,24 @@ | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:luthor_generator/checkers.dart'; | ||
import 'package:luthor_generator/helpers/validations/base_validations.dart'; | ||
|
||
void getCustomValidations(ParameterElement param, StringBuffer buffer) { | ||
_checkAndWriteCustomValidation(buffer, param); | ||
} | ||
|
||
void _checkAndWriteCustomValidation( | ||
StringBuffer buffer, | ||
ParameterElement param, | ||
) { | ||
final customAnnotation = getAnnotation(customValidatorChecker, param); | ||
if (customAnnotation != null) { | ||
buffer.write('.custom('); | ||
final message = customAnnotation.getField('message')?.toStringValue(); | ||
final customFuntion = | ||
customAnnotation.getField('customValidator')!.toFunctionValue()!.name; | ||
|
||
buffer.write(customFuntion); | ||
if (message != null) buffer.write(", message: '$message'"); | ||
buffer.write(')'); | ||
} | ||
} |