Skip to content

Commit

Permalink
feat: Add support for generic map validator (#99)
Browse files Browse the repository at this point in the history
* feat(luthor): Add map validator

* feat(luthor_generator): Add support for map validator

* test(luthor): Add tests for map validator
  • Loading branch information
exaby73 authored Dec 24, 2024
1 parent 4fb7f99 commit 1852250
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/luthor/lib/src/validations/map_validation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:luthor/src/validation.dart';

class MapValidation extends Validation {
String? customMessage;

MapValidation({
String? message,
}) : customMessage = message;

@override
bool call(String? fieldName, Object? value) {
super.call(fieldName, value);
return value is Map?;
}

@override
String get message =>
customMessage ?? '${fieldName ?? 'value'} must be a Map';

@override
Map<String, List<String>>? get errors => null;
}
7 changes: 7 additions & 0 deletions packages/luthor/lib/src/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:luthor/src/validations/custom_validation.dart';
import 'package:luthor/src/validations/double_validation.dart';
import 'package:luthor/src/validations/int_validation.dart';
import 'package:luthor/src/validations/list_validation.dart';
import 'package:luthor/src/validations/map_validation.dart';
import 'package:luthor/src/validations/null_validation.dart';
import 'package:luthor/src/validations/number_validation.dart';
import 'package:luthor/src/validations/required_validation.dart';
Expand Down Expand Up @@ -96,6 +97,12 @@ class Validator {
return this;
}

/// Validates that the value is a map.
Validator map({String? message}) {
validations.add(MapValidation(message: message));
return this;
}

/// Validates that the value matches the given schema.
Validator schema(Map<String, Validator> validatorSchema) {
validations.add(SchemaValidation(validatorSchema));
Expand Down
48 changes: 48 additions & 0 deletions packages/luthor/test/validations/map_validation_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:luthor/luthor.dart';
import 'package:test/test.dart';

void main() {
test('should return true when value is a map', () {
final result = l.map().validateValue({});

switch (result) {
case SingleValidationSuccess(data: _):
expect(result.data, {});
case SingleValidationError(data: _, errors: _):
fail('should not have errors');
}
});

test('should return false when value is not a map', () {
final result = l.map().validateValue('a');

switch (result) {
case SingleValidationSuccess(data: _):
fail('should not be a success');
case SingleValidationError(data: _, errors: final errors):
expect(errors, ['value must be a Map']);
}
});

test('should return true when value is null', () {
final result = l.map().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.required().map().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
Expand Up @@ -38,6 +38,10 @@ String getValidations(ParameterElement param) {
_writeListValidations(buffer, param);
}

if (param.type.isDartCoreMap) {
buffer.write('l.map()');
}

if (param.type.isDartCoreNull) {
buffer.write('l.nullValue()');
}
Expand Down

0 comments on commit 1852250

Please sign in to comment.