Earlier this month I mentioned a nice open source library for string and number validation.
Here’s an objective-c library called RPJSONvalidator from Reynaldo Goznales specifically for validating JSON data before it is mapped.
Here’s an example snippet from the readme showing how easy it is to validate some json data – given this of json data dictionary:
@"phoneNumber" : @"123-555-6789",
@"name" : @"Johnny Ringo",
@"age" : @"BANANA",
@"weight" : @"130.3",
@"ssn" : [NSNull null],
@"children" : @[],
@"parents" : @[
@{
@"name" : @"Mickey"
},
@{
@"name" : @"Minnie"
}
]
};
And the validator for this data:
[RPJSONValidator validateValuesFrom:json
withRequirements:@{
@"phoneNumber" : [RPValidatorPredicate.isString lengthIsGreaterThanOrEqualTo:@7],
@"name" : RPValidatorPredicate.isString,
@"age" : RPValidatorPredicate.isNumber.isOptional,
@"weight" : RPValidatorPredicate.isString,
@"ssn" : RPValidatorPredicate.isNull,
@"height" : RPValidatorPredicate.isString,
@"children" : RPValidatorPredicate.isArray,
@"parents" : [RPValidatorPredicate.isArray lengthIsGreaterThan:@1]
} error:&error];
if(error) {
NSLog(@"%@", [RPJSONValidator prettyStringGivenRPJSONValidatorError:error]);
} else {
NSLog(@"Woohoo, no errors!");
}
There’s some nice functionality included for pretty printing errors, validating sub-JSON data, and validating by specific indexes.
You can find RPJSONValidator on Github here.
A nice library for easily validating JSON data.
- Free Tool That Automatically Converts JSON Models To Objective-C Objects And Core Data Models
- Using Objective-C JSON To Do Translations In An iOS App
- Open Source JSON Based Data Model Framework
- Categories To Shorten Your Objective-C Code Including JSON Helpers, And NSComparisonMethods For iOS
- Tutorial: Parsing HTML On With The Objective-C LibXML Wrapper Hpple
Original article: Open Source Objective-C Library For Easily Validating JSON Data
©2014 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.




