Open Source Objective-C Library For Easily Validating JSON Data

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:

NSDictionary *json = @{
            @"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:

NSError *error;

[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.

FacebookTwitterDiggStumbleUponGoogle Plus

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.

Leave a Reply

Your email address will not be published. Required fields are marked *