Commits

dcutting committed 3ede956

Handle URLs that are not strings.

Comments (0)

Files changed (4)

VeriJSON.xcodeproj/project.pbxproj

 		C81834A11691DC7200E6EB90 /* SimpleObjectPattern.json in Resources */ = {isa = PBXBuildFile; fileRef = C81834A01691DC7200E6EB90 /* SimpleObjectPattern.json */; };
 		C81834A81691DD5C00E6EB90 /* NSBundle+JSONLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = C81834A71691DD5C00E6EB90 /* NSBundle+JSONLoader.m */; };
 		C81834AB1691DDFB00E6EB90 /* SimpleObject.json in Resources */ = {isa = PBXBuildFile; fileRef = C81834AA1691DDFB00E6EB90 /* SimpleObject.json */; };
+		C89CC44F1695BA0C009F5842 /* ObjectWithURLNonString.json in Resources */ = {isa = PBXBuildFile; fileRef = C89CC44E1695BA0C009F5842 /* ObjectWithURLNonString.json */; };
 		C8A0CCD61691DF5F0067B7CA /* SimpleObjectInvalid.json in Resources */ = {isa = PBXBuildFile; fileRef = C8A0CCD51691DF5F0067B7CA /* SimpleObjectInvalid.json */; };
 		C8A0CCD81691E1070067B7CA /* ObjectWithString.json in Resources */ = {isa = PBXBuildFile; fileRef = C8A0CCD71691E1070067B7CA /* ObjectWithString.json */; };
 		C8A0CCDA1691E11C0067B7CA /* ObjectWithStringPattern.json in Resources */ = {isa = PBXBuildFile; fileRef = C8A0CCD91691E11C0067B7CA /* ObjectWithStringPattern.json */; };
 		C81834A61691DD5C00E6EB90 /* NSBundle+JSONLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSBundle+JSONLoader.h"; sourceTree = "<group>"; };
 		C81834A71691DD5C00E6EB90 /* NSBundle+JSONLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSBundle+JSONLoader.m"; sourceTree = "<group>"; };
 		C81834AA1691DDFB00E6EB90 /* SimpleObject.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SimpleObject.json; path = Data/SimpleObject.json; sourceTree = "<group>"; };
+		C89CC44E1695BA0C009F5842 /* ObjectWithURLNonString.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = ObjectWithURLNonString.json; path = Data/ObjectWithURLNonString.json; sourceTree = "<group>"; };
 		C8A0CCD51691DF5F0067B7CA /* SimpleObjectInvalid.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SimpleObjectInvalid.json; path = Data/SimpleObjectInvalid.json; sourceTree = "<group>"; };
 		C8A0CCD71691E1070067B7CA /* ObjectWithString.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = ObjectWithString.json; path = Data/ObjectWithString.json; sourceTree = "<group>"; };
 		C8A0CCD91691E11C0067B7CA /* ObjectWithStringPattern.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = ObjectWithStringPattern.json; path = Data/ObjectWithStringPattern.json; sourceTree = "<group>"; };
 				C8A64FA51693499700D01705 /* ObjectWithDateStringInvalid.json */,
 				C8A2072B169584C8001CA314 /* ObjectWithURLPattern.json */,
 				C8A2072A169584C8001CA314 /* ObjectWithURL.json */,
+				C89CC44E1695BA0C009F5842 /* ObjectWithURLNonString.json */,
 				C8A2072F16958593001CA314 /* ObjectWithURLInvalid.json */,
 				C8A20731169585CC001CA314 /* ObjectWithURLInvalidEmpty.json */,
 				C8A20733169586B1001CA314 /* ObjectWithURLInvalidWhitespace.json */,
 				C8A2073A1695889F001CA314 /* ObjectWithHTTPSURL.json in Resources */,
 				C8A2073B1695889F001CA314 /* ObjectWithHTTPURL.json in Resources */,
 				C8A2073C1695889F001CA314 /* ObjectWithHTTPURLPattern.json in Resources */,
+				C89CC44F1695BA0C009F5842 /* ObjectWithURLNonString.json in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

VeriJSON/VeriJSON/VeriJSON.m

     return YES;
 }
 
-- (BOOL)verifyURL:(NSString *)value {
+- (BOOL)verifyURL:(id)value {
+    return nil != [self urlFromValue:value];
+}
+
+- (BOOL)verifyHTTPURL:(id)value {
+    NSURL *url = [self urlFromValue:value];
+    NSString *scheme = [url scheme];
+    return [scheme isEqualToString:@"https"] || [scheme isEqualToString:@"http"];
+}
+
+- (NSURL *)urlFromValue:(id)value {
+    if (![value isKindOfClass:[NSString class]]) return NO;
     NSString *trimmedValue = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
     if (0 == [trimmedValue length]) return NO;
-    NSURL *url = [NSURL URLWithString:value];
-    return nil != url;
-}
-
-- (BOOL)verifyHTTPURL:(NSString *)value {
-    NSURL *url = [NSURL URLWithString:value];
-    NSString *scheme = [url scheme];
-    return [scheme isEqualToString:@"https"] || [scheme isEqualToString:@"http"];
+    return [NSURL URLWithString:value];
 }
 
 - (NSError *)buildErrorFromPatternStack:(NSArray *)patternStack {

VeriJSONTests/Data/ObjectWithURLNonString.json

+{
+    "self": 3
+}

VeriJSONTests/VeriJSONTests.m

             [[@(valid) should] beYes];
         });
         
+        it(@"non-string value", ^{
+            id json = [bundle jsonFromResource:@"ObjectWithURLNonString.json"];
+            BOOL valid = [veriJSON verifyJSON:json pattern:pattern];
+            [[@(valid) should] beNo];
+        });
+        
         it(@"invalid URL", ^{
             id json = [bundle jsonFromResource:@"ObjectWithURLInvalid.json"];
             BOOL valid = [veriJSON verifyJSON:json pattern:pattern];
             BOOL valid = [veriJSON verifyJSON:json pattern:pattern];
             [[@(valid) should] beNo];
         });
+        
+        it(@"non-string value", ^{
+            id json = [bundle jsonFromResource:@"ObjectWithURLNonString.json"];
+            BOOL valid = [veriJSON verifyJSON:json pattern:pattern];
+            [[@(valid) should] beNo];
+        });
 
         it(@"non-HTTP URL", ^{
             id json = [bundle jsonFromResource:@"ObjectWithURL.json"];