This commit is contained in:
thosakwe 2017-04-26 08:41:15 -04:00
parent 72d1e1d2bf
commit 2ffd607230
5 changed files with 36 additions and 24 deletions

2
.gitignore vendored
View file

@ -27,3 +27,5 @@ doc/api/
pubspec.lock
log.txt
.idea

View file

@ -1,5 +1,5 @@
# validate
[![version 1.0.1](https://img.shields.io/badge/pub-v1.0.1-brightgreen.svg)](https://pub.dartlang.org/packages/angel_validate)
[![version 1.0.2](https://img.shields.io/badge/pub-v1.0.2-brightgreen.svg)](https://pub.dartlang.org/packages/angel_validate)
[![build status](https://travis-ci.org/angel-dart/validate.svg)](https://travis-ci.org/angel-dart/validate)
[Live Example](https://angel-dart.github.io/validate)

View file

@ -61,30 +61,33 @@ class Validator extends Matcher {
final List<String> requiredFields = [];
void _importSchema(Map<String, dynamic> schema) {
for (var key in schema.keys) {
var fieldName = key
.replaceAll(_asterisk, '')
.replaceAll(_forbidden, '')
.replaceAll(_optional, '');
var isForbidden = _forbidden.hasMatch(key),
isRequired = _asterisk.hasMatch(key);
for (var keys in schema.keys) {
for (var key in keys.split(',')) {
var fieldName = key
.replaceAll(_asterisk, '')
.replaceAll(_forbidden, '')
.replaceAll(_optional, '');
var isForbidden = _forbidden.hasMatch(key),
isRequired = _asterisk.hasMatch(key);
if (isForbidden) {
forbiddenFields.add(fieldName);
} else if (isRequired) {
requiredFields.add(fieldName);
}
if (isForbidden) {
forbiddenFields.add(fieldName);
} else if (isRequired) {
requiredFields.add(fieldName);
}
Iterable iterable = schema[key] is Iterable ? schema[key] : [schema[key]];
Iterable iterable =
schema[keys] is Iterable ? schema[keys] : [schema[keys]];
for (var rule in iterable) {
if (rule is Matcher) {
addRule(fieldName, rule);
} else if (rule is Filter) {
addRule(fieldName, predicate(rule));
} else {
throw new ArgumentError(
'Cannot use a(n) ${rule.runtimeType} as a validation rule.');
for (var rule in iterable) {
if (rule is Matcher) {
addRule(fieldName, rule);
} else if (rule is Filter) {
addRule(fieldName, predicate(rule));
} else {
throw new ArgumentError(
'Cannot use a(n) ${rule.runtimeType} as a validation rule.');
}
}
}
}

View file

@ -1,6 +1,6 @@
name: angel_validate
description: Cross-platform validation library based on `matcher`.
version: 1.0.1
version: 1.0.2
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/validate
environment:

View file

@ -7,7 +7,8 @@ final Validator emailSchema = new Validator({'to': isEmail},
final Validator todoSchema = new Validator({
'id': [isInt, isPositive],
'text*': isString,
'completed*': isBool
'completed*': isBool,
'foo,bar': [isTrue]
}, defaultValues: {
'completed': false
});
@ -33,4 +34,10 @@ main() {
var only = filter(inputData, ['foo']);
expect(only, equals({'foo': 'bar'}));
});
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)));
});
}