diff --git a/README.md b/README.md index 41f263e6..79ff91c7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # validate -[![version 1.0.2+1](https://img.shields.io/badge/pub-v1.0.2+1-brightgreen.svg)](https://pub.dartlang.org/packages/angel_validate) +[![version 1.0.2+2](https://img.shields.io/badge/pub-v1.0.2+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) @@ -38,9 +38,14 @@ import 'package:angel_validate/angel_validate.dart'; main() { var validator = new Validator({ 'username': isAlphaNum, + 'multiple,keys,with,same,rules': [isString, isNotEmpty], 'balance': [ greaterThanOrEqualTo(0), lessThan(1000000) + ], + 'nested': [ + foo, + [bar, baz] ] }); } @@ -225,6 +230,7 @@ including: * `isInt`: Asserts that a value is an `int`. * `isNum`: Asserts that a value is a `num`. * `isString`: Asserts that a value is a `String`. +* `isNonEmptyString`: Asserts that a value is a non-empty `String`. * `isUrl`: Asserts that a `String` is an HTTPS or HTTP URL. The remaining functionality is diff --git a/lib/src/matchers.dart b/lib/src/matchers.dart index a5a21f94..de2d22b8 100644 --- a/lib/src/matchers.dart +++ b/lib/src/matchers.dart @@ -32,12 +32,20 @@ final Matcher isNum = predicate((value) => value is num, 'a number'); /// Asserts that a value is a `String`. final Matcher isString = predicate((value) => value is String, 'a String'); +/// Asserts that a value is a non-empty `String`. +final Matcher isNonEmptyString = predicate( + (value) => value is String && value.isNotEmpty, 'a non-empty String'); + /// Asserts that a `String` is an `http://` or `https://` URL. /// /// The regular expression used: /// ``` /// https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*) /// ``` -final Matcher isurl = predicate( +final Matcher isUrl = predicate( (value) => value is String && _url.hasMatch(value), 'a valid url, starting with http:// or https://'); + +/// Use [isUrl] instead. +@deprecated +final Matcher isurl = isUrl; diff --git a/lib/src/validator.dart b/lib/src/validator.dart index 814b8304..06702aa2 100644 --- a/lib/src/validator.dart +++ b/lib/src/validator.dart @@ -76,8 +76,18 @@ class Validator extends Matcher { requiredFields.add(fieldName); } - Iterable iterable = + Iterable _iterable = schema[keys] is Iterable ? schema[keys] : [schema[keys]]; + var iterable = []; + + _addTo(x) { + if (x is Iterable) + x.map(_addTo); + else + iterable.add(x); + } + + _iterable.map(_addTo); for (var rule in iterable) { if (rule is Matcher) { diff --git a/pubspec.yaml b/pubspec.yaml index 8adb14c3..cf7e2a6c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: angel_validate description: Cross-platform validation library based on `matcher`. -version: 1.0.2+1 +version: 1.0.2+2 author: Tobe O homepage: https://github.com/angel-dart/validate environment: