Skip to content

Commit

Permalink
feat: Add name support (#98)
Browse files Browse the repository at this point in the history
* Update example

* feat(luthor): Add name

* feat(luthor_generator): Add withName support
  • Loading branch information
exaby73 authored Dec 24, 2024
1 parent 66c2100 commit de2e511
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/luthor/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() {
print(l.number().validateValue(aNumber));
print(l.int().validateValue(anInt));
print(l.double().validateValue(aDouble));
print(l.bool().validateValue(aBoolean));
print(l.boolean().validateValue(aBoolean));
print(l.nullValue().validateValue(aNullValue));

// General validations
Expand Down
11 changes: 11 additions & 0 deletions packages/luthor/lib/src/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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';
import 'package:meta/meta.dart';

typedef FromJson<T> = T Function(Map<String, Object?> json);

Expand All @@ -23,8 +24,18 @@ class Validator {
Validator({List<Validation> initialValidations = const []})
: validations = List.from(initialValidations);

String? _name;

String? get name => _name;

@protected
final List<Validation> validations;

Validator withName(String? name) {
_name = name;
return this;
}

/// Validates a value against a custom validator function.
Validator custom(CustomValidator customValidator, {String? message}) {
validations.add(CustomValidation(customValidator, message: message));
Expand Down

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

2 changes: 1 addition & 1 deletion 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.

4 changes: 2 additions & 2 deletions packages/luthor_generator/example/lib/without_freezed.g.dart

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

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class LuthorGenerator extends GeneratorForAnnotation<Luthor> {

final params = constructor.parameters;
final buffer = StringBuffer();
buffer.write('Validator \$${name}Schema = l.schema({\n');
buffer.write("Validator \$${name}Schema = l.withName('$name').schema({\n");

for (final param in params) {
var name = param.name;
Expand Down

0 comments on commit de2e511

Please sign in to comment.