Skip to content

Commit

Permalink
feat(luthor_generator): added custom validator for generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kaziwaseef committed Feb 18, 2024
1 parent 55bdd42 commit 9eb1c10
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/luthor_generator/example/lib/sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ part 'sample.freezed.dart';

part 'sample.g.dart';

bool customValidatorFn(Object? value) {
return value == 'custom';
}

@luthor
@freezed
class Sample with _$Sample {
Expand All @@ -35,6 +39,7 @@ class Sample with _$Sample {
required String luthorPath,
required AnotherSample anotherSample,
@JsonKey(name: 'jsonKeyName') required String foo,
@WithCustomValidator(customValidatorFn) required String custom,
}) = _Sample;

static SchemaValidationResult<Sample> validate(Map<String, dynamic> json) =>
Expand All @@ -61,6 +66,7 @@ void main() {
"minAndMaxInt": 1,
"minAndMaxDouble": 1.0,
"minAndMaxNumber": 1,
"custom": 'custom',
});
switch (result2) {
case SchemaValidationError(errors: final errors):
Expand All @@ -78,6 +84,7 @@ void main() {
"minAndMaxInt": 5,
"minAndMaxDouble": 5.0,
"minAndMaxNumber": 5,
"custom": 1,
});
switch (result3) {
case SchemaValidationError(errors: final errors):
Expand Down
39 changes: 32 additions & 7 deletions packages/luthor_generator/example/lib/sample.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ mixin _$Sample {
AnotherSample get anotherSample => throw _privateConstructorUsedError;
@JsonKey(name: 'jsonKeyName')
String get foo => throw _privateConstructorUsedError;
@WithCustomValidator(customValidatorFn)
String get custom => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
Expand Down Expand Up @@ -85,7 +87,8 @@ abstract class $SampleCopyWith<$Res> {
@IsUri(allowedSchemes: ['https']) String? httpsLink,
@MatchRegex(r'^https:\/\/pub\.dev\/packages\/luthor') String luthorPath,
AnotherSample anotherSample,
@JsonKey(name: 'jsonKeyName') String foo});
@JsonKey(name: 'jsonKeyName') String foo,
@WithCustomValidator(customValidatorFn) String custom});

$AnotherSampleCopyWith<$Res> get anotherSample;
}
Expand Down Expand Up @@ -122,6 +125,7 @@ class _$SampleCopyWithImpl<$Res, $Val extends Sample>
Object? luthorPath = null,
Object? anotherSample = null,
Object? foo = null,
Object? custom = null,
}) {
return _then(_value.copyWith(
anyValue: freezed == anyValue
Expand Down Expand Up @@ -200,6 +204,10 @@ class _$SampleCopyWithImpl<$Res, $Val extends Sample>
? _value.foo
: foo // ignore: cast_nullable_to_non_nullable
as String,
custom: null == custom
? _value.custom
: custom // ignore: cast_nullable_to_non_nullable
as String,
) as $Val);
}

Expand Down Expand Up @@ -238,7 +246,8 @@ abstract class _$$SampleImplCopyWith<$Res> implements $SampleCopyWith<$Res> {
@IsUri(allowedSchemes: ['https']) String? httpsLink,
@MatchRegex(r'^https:\/\/pub\.dev\/packages\/luthor') String luthorPath,
AnotherSample anotherSample,
@JsonKey(name: 'jsonKeyName') String foo});
@JsonKey(name: 'jsonKeyName') String foo,
@WithCustomValidator(customValidatorFn) String custom});

@override
$AnotherSampleCopyWith<$Res> get anotherSample;
Expand Down Expand Up @@ -274,6 +283,7 @@ class __$$SampleImplCopyWithImpl<$Res>
Object? luthorPath = null,
Object? anotherSample = null,
Object? foo = null,
Object? custom = null,
}) {
return _then(_$SampleImpl(
anyValue: freezed == anyValue
Expand Down Expand Up @@ -352,6 +362,10 @@ class __$$SampleImplCopyWithImpl<$Res>
? _value.foo
: foo // ignore: cast_nullable_to_non_nullable
as String,
custom: null == custom
? _value.custom
: custom // ignore: cast_nullable_to_non_nullable
as String,
));
}
}
Expand Down Expand Up @@ -379,7 +393,8 @@ class _$SampleImpl implements _Sample {
@MatchRegex(r'^https:\/\/pub\.dev\/packages\/luthor')
required this.luthorPath,
required this.anotherSample,
@JsonKey(name: 'jsonKeyName') required this.foo})
@JsonKey(name: 'jsonKeyName') required this.foo,
@WithCustomValidator(customValidatorFn) required this.custom})
: _listValue = listValue;

factory _$SampleImpl.fromJson(Map<String, dynamic> json) =>
Expand Down Expand Up @@ -445,10 +460,13 @@ class _$SampleImpl implements _Sample {
@override
@JsonKey(name: 'jsonKeyName')
final String foo;
@override
@WithCustomValidator(customValidatorFn)
final String custom;

@override
String toString() {
return 'Sample(anyValue: $anyValue, boolValue: $boolValue, doubleValue: $doubleValue, intValue: $intValue, listValue: $listValue, numValue: $numValue, stringValue: $stringValue, email: $email, date: $date, dateTime: $dateTime, exactly10Characters: $exactly10Characters, minAndMaxString: $minAndMaxString, minAndMaxInt: $minAndMaxInt, minAndMaxDouble: $minAndMaxDouble, minAndMaxNumber: $minAndMaxNumber, httpsLink: $httpsLink, luthorPath: $luthorPath, anotherSample: $anotherSample, foo: $foo)';
return 'Sample(anyValue: $anyValue, boolValue: $boolValue, doubleValue: $doubleValue, intValue: $intValue, listValue: $listValue, numValue: $numValue, stringValue: $stringValue, email: $email, date: $date, dateTime: $dateTime, exactly10Characters: $exactly10Characters, minAndMaxString: $minAndMaxString, minAndMaxInt: $minAndMaxInt, minAndMaxDouble: $minAndMaxDouble, minAndMaxNumber: $minAndMaxNumber, httpsLink: $httpsLink, luthorPath: $luthorPath, anotherSample: $anotherSample, foo: $foo, custom: $custom)';
}

@override
Expand Down Expand Up @@ -489,7 +507,8 @@ class _$SampleImpl implements _Sample {
other.luthorPath == luthorPath) &&
(identical(other.anotherSample, anotherSample) ||
other.anotherSample == anotherSample) &&
(identical(other.foo, foo) || other.foo == foo));
(identical(other.foo, foo) || other.foo == foo) &&
(identical(other.custom, custom) || other.custom == custom));
}

@JsonKey(ignore: true)
Expand All @@ -514,7 +533,8 @@ class _$SampleImpl implements _Sample {
httpsLink,
luthorPath,
anotherSample,
foo
foo,
custom
]);

@JsonKey(ignore: true)
Expand Down Expand Up @@ -554,7 +574,9 @@ abstract class _Sample implements Sample {
@MatchRegex(r'^https:\/\/pub\.dev\/packages\/luthor')
required final String luthorPath,
required final AnotherSample anotherSample,
@JsonKey(name: 'jsonKeyName') required final String foo}) = _$SampleImpl;
@JsonKey(name: 'jsonKeyName') required final String foo,
@WithCustomValidator(customValidatorFn)
required final String custom}) = _$SampleImpl;

factory _Sample.fromJson(Map<String, dynamic> json) = _$SampleImpl.fromJson;

Expand Down Expand Up @@ -612,6 +634,9 @@ abstract class _Sample implements Sample {
@JsonKey(name: 'jsonKeyName')
String get foo;
@override
@WithCustomValidator(customValidatorFn)
String get custom;
@override
@JsonKey(ignore: true)
_$$SampleImplCopyWith<_$SampleImpl> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
3 changes: 3 additions & 0 deletions packages/luthor_generator/example/lib/sample.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/luthor_generator/lib/checkers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ const hasMaxNumberChecker = TypeChecker.fromRuntime(HasMaxNumber);
const hasMinNumberChecker = TypeChecker.fromRuntime(HasMinNumber);
const isUriChecker = TypeChecker.fromRuntime(IsUri);
const matchRegexChecker = TypeChecker.fromRuntime(MatchRegex);
const customValidatorChecker = TypeChecker.fromRuntime(WithCustomValidator);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:luthor_generator/checkers.dart';
import 'package:luthor_generator/errors/unsupported_type_error.dart';
import 'package:luthor_generator/helpers/validations/custom_validations.dart';
import 'package:luthor_generator/helpers/validations/double_validations.dart';
import 'package:luthor_generator/helpers/validations/int_validations.dart';
import 'package:luthor_generator/helpers/validations/number_validations.dart';
Expand Down Expand Up @@ -56,6 +57,8 @@ String getValidations(ParameterElement param) {
_checkAndAddCustomSchema(buffer, param);
}

getCustomValidations(param, buffer);

if (param.type is! DynamicType && !isNullable) buffer.write('.required()');

return buffer.toString();
Expand Down
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(')');
}
}

0 comments on commit 9eb1c10

Please sign in to comment.