TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Apr 2015 |
Maintained by Oleg Poyaganov.
Validates object property values for class and protocol conformity.
PropertySanitizer was written to solve problem with Objective-C dynamic nature. When you parse JSON from server API response and you are unsure about property types at runtime, you should check your model properties to make your app fail earlier than crash somewhere in UI layer.
For example:
@class Account : NSObject
@property (nonatomic, strong) NSNumber *balance;
...
@end
account.balance = [JSONDictionary valueForKey:@"user_balance"]; // May fail if "user_balance" is string
To run the example project, clone the repo, and run pod install
from the Example directory first.
This library has one method for validation:
@interface NSObject (PRSSanitizer)
- (BOOL)prs_validatePropertiesValuesWithError:(NSError * __autoreleasing *)error;
@end
Example:
account.balance = [JSONDictionary valueForKey:@"user_balance"]; // May fail if "user_balance" is string
NSError *validationError;
BOOL valid = [account prs_validatePropertiesValuesWithError:&validationError];
if (valid) {
// Everythind is ok
} else {
// You can check for actual error code
if ([checkError.domain isEqualToString:PRSSanitizerErrorDomain] &&
checkError.copy == PRSSanitizerErrorValueInvalidClass) {
// Invalid class error
}
}
For error codes look at NSObject+PRSSanitizer.h file.
- Supported build target - iOS 8.3
- Earliest supported deployment target - iOS 7.0
PropertySanitizer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "PropertySanitizer"
Oleg Poyaganov, [email protected]
PropertySanitizer is available under the MIT license. See the LICENSE file for more info.