2016-12-26 01:09:24 +00:00
|
|
|
import 'package:angel_validate/angel_validate.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2019-09-27 13:28:20 +00:00
|
|
|
final Validator emailSchema =
|
|
|
|
Validator({'to': isEmail}, customErrorMessages: {'to': 'Hello, world!'});
|
2016-12-26 13:04:42 +00:00
|
|
|
|
2019-09-27 13:28:20 +00:00
|
|
|
final Validator todoSchema = Validator({
|
2016-12-26 01:09:24 +00:00
|
|
|
'id': [isInt, isPositive],
|
|
|
|
'text*': isString,
|
2017-04-26 12:41:15 +00:00
|
|
|
'completed*': isBool,
|
|
|
|
'foo,bar': [isTrue]
|
2016-12-26 01:09:24 +00:00
|
|
|
}, defaultValues: {
|
|
|
|
'completed': false
|
|
|
|
});
|
|
|
|
|
|
|
|
main() {
|
2016-12-26 13:04:42 +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!'));
|
|
|
|
});
|
|
|
|
|
2018-06-28 02:34:05 +00:00
|
|
|
test('requireField', () => expect(requireField('foo'), 'foo*'));
|
|
|
|
|
|
|
|
test('requireFields',
|
|
|
|
() => expect(requireFields(['foo', 'bar']), 'foo*, bar*'));
|
|
|
|
|
2016-12-26 01:09:24 +00:00
|
|
|
test('todo', () {
|
|
|
|
expect(() {
|
|
|
|
todoSchema
|
|
|
|
.enforce({'id': 'fool', 'text': 'Hello, world!', 'completed': 4});
|
2018-06-28 16:34:05 +00:00
|
|
|
// ignore: deprecated_member_use
|
2019-09-27 13:28:20 +00:00
|
|
|
}, throwsA(isInstanceOf<ValidationException>()));
|
2016-12-26 01:09:24 +00:00
|
|
|
});
|
2017-01-25 23:03:45 +00:00
|
|
|
|
|
|
|
test('filter', () {
|
|
|
|
var inputData = {'foo': 'bar', 'a': 'b', '1': 2};
|
|
|
|
var only = filter(inputData, ['foo']);
|
|
|
|
expect(only, equals({'foo': 'bar'}));
|
|
|
|
});
|
2017-04-26 12:41:15 +00:00
|
|
|
|
|
|
|
test('comma in schema', () {
|
|
|
|
expect(todoSchema.rules.keys, allOf(contains('foo'), contains('bar')));
|
|
|
|
expect([todoSchema.rules['foo'].first, todoSchema.rules['bar'].first],
|
|
|
|
everyElement(predicate((x) => x == isTrue)));
|
|
|
|
});
|
2016-12-26 01:09:24 +00:00
|
|
|
}
|