ObjcModelValidation 灵感来自 Ruby ORM 框架的 ActiveRecord,实现了 Objective-C 的数据验证框架。
示例代码
Model:
#import "ValidationModel.h" @interface SampleModel : ValidationModel @property (nonatomic, strong) NSString *nameString; @property (nonatomic, strong) NSNumber *ageNumber; @end
使用方法:
#import "SampleModel.h" @implementation SampleModel -(NSError *)validateAgeNumber:(NSNumber *)ageNumber{ if ([ageNumber greaterThan:@18] && [ageNumber presence]) { return nil; } return [self errorWithLocalizedMessage:NSLocalizedString(@"You should be older than 18.", @"")]; } -(NSError *)validateNameString:(NSString *)nameString{ if ([nameString lengthMin:4]) { return nil; } return [self errorWithLocalizedMessage:NSLocalizedString(@"Your name should be at least 4 characters long", @"")]; } @end