Permalink
Browse files

doc: isKeyInMap shows Map as JSON-Structure

  • Loading branch information...
MikeMitterer committed Jul 14, 2016
1 parent 9e0bda2 commit 471c4e2fb5807dc660a00275840f6f00bf4dc947
Showing with 20 additions and 4 deletions.
  1. +2 −2 Validate.iml
  2. +8 −2 lib/validate.dart
  3. +10 −0 test/unit/validate_test.dart
@@ -7,8 +7,8 @@
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/packages" />
<excludeFolder url="file://$MODULE_DIR$/test/packages" />
<excludeFolder url="file://$MODULE_DIR$/test/src/packages" />
<excludeFolder url="file://$MODULE_DIR$/web/packages" />
<excludeFolder url="file://$MODULE_DIR$/test/unit/packages" />
<excludeFolder url="file://$MODULE_DIR$/tool/packages" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="application" />
@@ -19,6 +19,8 @@

library validate;

import 'dart:convert';

part "src/errors.dart";
part "src/utils.dart";

@@ -57,10 +59,13 @@ abstract class Validate {
static const String DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE = "The value is not in the specified inclusive range";
static const String DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE = "The value is not in the specified exclusive range";
static const String DEFAULT_JSON_MESSAGE = "The value is neither a num, String, bool, Null, List or Map";
static const String DEFAULT_KEY_IN_MAP_MESSAGE = "The key '%key%' is not available for this Map";
static const String DEFAULT_KEY_IN_MAP_MESSAGE = "The key '%key%' is not available for this structure: %structure%";

static const String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "The instance of the validated object is invalid. Should have been %wish% but was %truth%";

/// prettyPrint for JSON (Used for KeyInMap check)
static const JsonEncoder _PRETTYJSON = const JsonEncoder.withIndent(' ');

/*
static const String _DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EX_MESSAGE = "The validated collection contains null element at specified index";
static const String _DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE = "The validated character sequence index is invalid";
@@ -371,7 +376,8 @@ abstract class Validate {
static void isKeyInMap(final dynamic key,final Map map,[String message = DEFAULT_KEY_IN_MAP_MESSAGE]) {
Validate.notNull(map, message);
if(!map.containsKey(key) || map[key] == null) {
throw new ArgumentError(message.replaceFirst("%key%", key.toString()));
throw new ArgumentError(message.replaceFirst("%key%", key.toString())
.replaceFirst("%structure%",_PRETTYJSON.convert(map)));
}
}

@@ -139,6 +139,16 @@ main() {

//expect(() => (Validate.isKeyInMap("email",map1ToTest)),returnsNormally);
expect(() => (Validate.isKeyInMap("email", map1ToTest)), throwsA(new isInstanceOf<ArgumentError>()));

try {
Validate.isKeyInMap("email", map1ToTest);
expect(false,isTrue);

} on ArgumentError catch(e) {
// Strip out all whitespaces
expect(e.message.replaceAll(new RegExp("\\s+")," "),
"The key 'email' is not available for this structure: { \"name\": \"Mike\", \"number\": 42 }");
}
});

test('> isInstanceOf', () {

0 comments on commit 471c4e2

Please sign in to comment.