Permalink
Browse files

Adding regexString property to US2Condition base class

Adding a regex string property to the US2Condition base class
Updating all regex based US2Condition subclasses to implement new regexString
property
Users are now able to override US2Condition's regexString property if they wish to
implement a custom regex string check.
  • Loading branch information...
1 parent c4ee625 commit acc4d5c3ec8830f67f6618491759aac5ba4d4fbf @alexfish alexfish committed Jan 16, 2013
@@ -83,18 +83,39 @@
@property (nonatomic, assign) BOOL shouldAllowViolation;
/**
+ A regular expression string which the validated string is matched against
+ Defaults to nil
+ */
+@property (nonatomic, copy) NSString *regexString;
+
+/**
Static shorthand for creating a validator.
*/
+ (US2Condition *) condition;
/**
Initialize condition with a custom localized violation string.
*/
-- (id) initWithLocalizedViolationString: (NSString *) localizedViolationString;
+- (id)initWithLocalizedViolationString:(NSString *)localizedViolationString;
/**
Set the localized violation string and return self.
*/
-- (id) withLocalizedViolationString: (NSString *) localizedViolationString;
+- (id)withLocalizedViolationString:(NSString *)localizedViolationString;
+
+/**
+ Initialize condition with a custom localized violation string and regex string.
+ */
+- (id)initWithLocalizedViolationString:(NSString *)localizedViolationString andRegexString:(NSString *)regexString;
+
+/**
+ Initialize condition with a custom regex string.
+ */
+- (id)initWithRegexString:(NSString *)regexString;
+
+/**
+ Set the regex string and return self.
+ */
+- (id)withRegexString:(NSString *)regexString;
@end
@@ -30,6 +30,7 @@ @implementation US2Condition
@synthesize shouldAllowViolation = _shouldAllowViolation;
@synthesize localizedViolationString = _localizedViolationString;
+@synthesize regexString = _regex;
#pragma mark - Init
@@ -48,6 +49,33 @@ - (id)initWithLocalizedViolationString:(NSString *)localizedViolationString
return self;
}
+- (id)initWithLocalizedViolationString:(NSString *)localizedViolationString andRegexString:(NSString *)regexString
+{
+ if (self = [super init])
+ {
+ self.localizedViolationString = localizedViolationString;
+ self.regexString = regexString;
+ }
+
+ return self;
+}
+
+- (id)initWithRegexString:(NSString *)regexString
+{
+ if (self = [super init])
+ {
+ self.regexString = regexString;
+ }
+
+ return self;
+}
+
+- (id)withRegexString:(NSString *)regexString
+{
+ self.regexString = regexString;
+ return self;
+}
+
- (id)withLocalizedViolationString:(NSString *)localizedViolationString
{
self.localizedViolationString = localizedViolationString;
@@ -64,7 +92,24 @@ - (id)withLocalizedViolationString:(NSString *)localizedViolationString
*/
- (BOOL)check:(NSString *)string
{
- return YES;
+ BOOL success = YES;
+
+ if(!string)
+ {
+ success = NO;
+ }
+ else if(self.regexString)
+ {
+ NSError *error = NULL;
+ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:self.regexString options:NSRegularExpressionCaseInsensitive error:&error];
+ if(!error)
+ {
+ NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
+ success = numberOfMatches == 1;
+ }
+ }
+
+ return success;
}
@@ -73,7 +118,7 @@ - (BOOL)check:(NSString *)string
/**
Create a localized violation string.
*/
-- (NSString *) createLocalizedViolationString
+- (NSString *)createLocalizedViolationString
{
return nil;
}
@@ -70,7 +70,7 @@
/**
Remove all conditions from collection.
*/
-- (void) removeAllConditions;
+- (void)removeAllConditions;
@end
@@ -34,11 +34,9 @@ - (BOOL)check:(NSString *)string
if (nil == string)
string = [NSString string];
- NSError *error = NULL;
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[+\\w\\.\\-']+@[a-zA-Z0-9-]+(\\.[a-zA-Z]{2,})+$" options:NSRegularExpressionCaseInsensitive error:&error];
- NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
+ self.regexString = @"^[+\\w\\.\\-']+@[a-zA-Z0-9-]+(\\.[a-zA-Z]{2,})+$";
- return numberOfMatches == 1;
+ return [super check:string];
}
@@ -34,11 +34,9 @@ - (BOOL)check:(NSString *)string
if (nil == string)
return NO;
- NSError *error = NULL;
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:0 error:&error];
- NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
+ self.regexString = @"\\d+";
- return numberOfMatches == string.length;
+ return [super check:string];
}
@@ -34,11 +34,9 @@ - (BOOL)check:(NSString *)string
if (nil == string)
string = @"";
- NSError *error = NULL;
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\\ 0[Aa][Aa])|([Ss][Aa][Nn]\\ {0,1}[Tt][Aa]1)|([Bb][Ff][Pp][Oo]\\ {0,1}([Cc]\\/[Oo]\\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Tt][Hh][Ll]|[Tt][Dd][Cc][Uu]|[Tt][Kk][Cc][Aa])\\ {0,1}1[Zz][Zz]))$" options:0 error:&error];
- NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
+ self.regexString = @"^([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\\ 0[Aa][Aa])|([Ss][Aa][Nn]\\ {0,1}[Tt][Aa]1)|([Bb][Ff][Pp][Oo]\\ {0,1}([Cc]\\/[Oo]\\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Tt][Hh][Ll]|[Tt][Dd][Cc][Uu]|[Tt][Kk][Cc][Aa])\\ {0,1}1[Zz][Zz]))$";
- return numberOfMatches == 1;
+ return [super check:string];
}
@@ -55,12 +55,9 @@ - (BOOL)check:(NSString *)string
if (nil == string)
string = [NSString string];
- NSError *error = NULL;
- NSString *regexString = [NSString stringWithFormat:@"^.{%d,%d}$", _range.location, _range.length];
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:0 error:&error];
- NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
+ self.regexString = [NSString stringWithFormat:@"^.{%d,%d}$", _range.location, _range.length];
- return numberOfMatches == 1;
+ return [super check:string];
}
@@ -36,11 +36,9 @@ - (BOOL)check:(NSString *)string
return NO;
}
- NSError *error = NULL;
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^((https?)://)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$" options:NSRegularExpressionCaseInsensitive error:&error];
- NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
+ self.regexString = @"^((https?)://)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
- return numberOfMatches == 1;
+ return [super check:string];
}
@@ -36,11 +36,9 @@ - (BOOL)check:(NSString *)string
return NO;
}
- NSError *error = NULL;
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^((https?)://)[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$" options:NSRegularExpressionCaseInsensitive error:&error];
- NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
+ self.regexString = @"^((https?)://)[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
- return numberOfMatches == 1;
+ return [super check:string];
}
@@ -126,6 +126,22 @@ - (void)testUS2ConditionAlphanumeric
}
/**
+ Test Condition with custom regex check
+ */
+- (void)testUS2ConditionRegexString
+{
+ NSString *successTestString1 = @"hello world";
+ NSString *failureTestString1 = @"a?1";
+ NSString *failureTestString2 = nil;
+
+ US2Condition *condition = [[US2Condition alloc] initWithRegexString:@"hello world"];
+ STAssertTrue([condition check:successTestString1], @"The US2Condition should respond with TRUE and not FALSE", nil);
+
+ STAssertFalse([condition check:failureTestString1], @"The US2Condition should respond with FALSE and not TRUE", nil);
+ STAssertFalse([condition check:failureTestString2], @"The US2Condition should respond with FALSE and not TRUE", nil);
+}
+
+/**
Test US2ConditionEmail check
*/
- (void)testUS2ConditionEmail
@@ -43,7 +43,6 @@ - (id)init
US2ConditionRange *rangeCondition = [[[US2ConditionRange alloc] init] autorelease];
rangeCondition.range = NSMakeRange(1, 12);
rangeCondition.shouldAllowViolation = YES;
-
[self addCondition:rangeCondition];
}

0 comments on commit acc4d5c

Please sign in to comment.