Objective C的DSL验证器
导入FTGValidator.h
即可。
BOOL (^customBlock)(NSArray *) = ^(NSArray *array) {
if (array.count == 3) {
return YES;
}
return NO;
};
NSArray *rules = @[REQUIRE_STRING(@"John").notEmpty.with.message(@"Value must not be empty"),
REQUIRE_STRING(@"90001").to.matchRegExWithPattern(@"^[0-9][0-9][0-9][0-9][0-9]$").with.message(@"Value must be US zip code format"),
REQUIRE_NUMBER(@(1990)).greaterThan(@(1970)).with.message(@"Must be born after 1970"),
REQUIRE_ANY(@[@"one", @"two", @"three"]).satisfyBlock(customBlock).with.message(@"Array must have 3 elements"),
];
FTGValidator *validator = [FTGValidator validatorWithRules:rules];
FTGValidationResult *result = [validator validate];
if (result.succeeded) {
NSLog(@"All rules pass");
} else {
NSLog(@"Failed. Show message: %@", result.message);
}
使用XCTest
本项目发布在MIT许可证下。见LICENSE.md