platform/packages/validate/test/basic_data_test.dart

49 lines
1.4 KiB
Dart
Raw Normal View History

2021-05-14 11:47:44 +00:00
import 'package:angel3_validate/angel3_validate.dart';
2021-02-21 02:47:23 +00:00
import 'package:test/test.dart';
final Validator emailSchema =
Validator({'to': isEmail}, customErrorMessages: {'to': 'Hello, world!'});
final Validator todoSchema = Validator({
'id': [isInt, isPositive],
'text*': isString,
'completed*': isBool,
'foo,bar': [isTrue]
}, defaultValues: {
'completed': false
});
2021-06-26 13:13:43 +00:00
void main() {
2021-02-21 02:47:23 +00:00
test('custom error message', () {
var result = emailSchema.check({'to': 2});
expect(result.errors, isList);
expect(result.errors, hasLength(1));
expect(result.errors.first, equals('Hello, world!'));
});
test('requireField', () => expect(requireField('foo'), 'foo*'));
test('requireFields',
() => expect(requireFields(['foo', 'bar']), 'foo*, bar*'));
test('todo', () {
expect(() {
todoSchema
.enforce({'id': 'fool', 'text': 'Hello, world!', 'completed': 4});
2023-10-27 15:52:29 +00:00
}, throwsA(isA<ValidationException>()));
2021-02-21 02:47:23 +00:00
});
test('filter', () {
var inputData = {'foo': 'bar', 'a': 'b', '1': 2};
var only = filter(inputData, ['foo']);
expect(only, equals({'foo': 'bar'}));
});
test('comma in schema', () {
expect(todoSchema.rules.keys, allOf(contains('foo'), contains('bar')));
2021-04-10 12:42:55 +00:00
expect([todoSchema.rules['foo']!.first, todoSchema.rules['bar']!.first],
everyElement(predicate((dynamic x) => x == isTrue)));
2021-02-21 02:47:23 +00:00
});
}