Get Updates By E-mail 

Article Written By:
Johann Dowa

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.


  •  
  •  
  •  
  •  
  •  

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments