diff --git a/packages/luthor/example/lib/main.dart b/packages/luthor/example/lib/main.dart index 6c21feed..e4b67af2 100644 --- a/packages/luthor/example/lib/main.dart +++ b/packages/luthor/example/lib/main.dart @@ -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 diff --git a/packages/luthor/lib/src/validator.dart b/packages/luthor/lib/src/validator.dart index 74b9e806..881bebbe 100644 --- a/packages/luthor/lib/src/validator.dart +++ b/packages/luthor/lib/src/validator.dart @@ -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 Function(Map json); @@ -23,8 +24,18 @@ class Validator { Validator({List initialValidations = const []}) : validations = List.from(initialValidations); + String? _name; + + String? get name => _name; + + @protected final List 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)); diff --git a/packages/luthor_generator/example/lib/another_sample.g.dart b/packages/luthor_generator/example/lib/another_sample.g.dart index 6729d50a..c904e184 100644 --- a/packages/luthor_generator/example/lib/another_sample.g.dart +++ b/packages/luthor_generator/example/lib/another_sample.g.dart @@ -30,7 +30,7 @@ Map _$$AnotherSampleImplToJson(_$AnotherSampleImpl instance) => // LuthorGenerator // ************************************************************************** -Validator $AnotherSampleSchema = l.schema({ +Validator $AnotherSampleSchema = l.withName('AnotherSample').schema({ 'id': l.int().required(), 'full_name': l.string(), 'email': l.string().email(message: 'Invalid email').required(), diff --git a/packages/luthor_generator/example/lib/sample.g.dart b/packages/luthor_generator/example/lib/sample.g.dart index f8cb2239..26016d1f 100644 --- a/packages/luthor_generator/example/lib/sample.g.dart +++ b/packages/luthor_generator/example/lib/sample.g.dart @@ -69,7 +69,7 @@ Map _$$SampleImplToJson(_$SampleImpl instance) => // LuthorGenerator // ************************************************************************** -Validator $SampleSchema = l.schema({ +Validator $SampleSchema = l.withName('Sample').schema({ 'anyValue': l.any(), 'boolValue': l.boolean().required(), 'doubleValue': l.double().required(), diff --git a/packages/luthor_generator/example/lib/without_freezed.g.dart b/packages/luthor_generator/example/lib/without_freezed.g.dart index 8cacce0d..84d332b2 100644 --- a/packages/luthor_generator/example/lib/without_freezed.g.dart +++ b/packages/luthor_generator/example/lib/without_freezed.g.dart @@ -6,7 +6,7 @@ part of 'without_freezed.dart'; // LuthorGenerator // ************************************************************************** -Validator $WithoutFreezedSchema = l.schema({ +Validator $WithoutFreezedSchema = l.withName('WithoutFreezed').schema({ 'name': l.string().email().required(), 'age': l.int().required(), }); @@ -21,7 +21,7 @@ extension WithoutFreezedValidationExtension on WithoutFreezed { $WithoutFreezedValidate(toJson()); } -Validator $WithDartMappableSchema = l.schema({ +Validator $WithDartMappableSchema = l.withName('WithDartMappable').schema({ 'email': l.string().email().required(), 'password': l.string().min(8).required(), }); diff --git a/packages/luthor_generator/lib/generators/luthor_generator.dart b/packages/luthor_generator/lib/generators/luthor_generator.dart index 7cbe01fb..8a310682 100644 --- a/packages/luthor_generator/lib/generators/luthor_generator.dart +++ b/packages/luthor_generator/lib/generators/luthor_generator.dart @@ -43,7 +43,7 @@ class LuthorGenerator extends GeneratorForAnnotation { 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;