In fact I don't use Mantle, but for validation I use another GitHub Library called RPJSONValidator
It tells you the type that you expect and what type the value has been arrived.
A simple example code
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!");
}
Each key-value pair describes requirements for each JSON value. For example, the key-value pair @"name"
: RPValidatorPredicate.isString
will place a requirement on the JSON value with key "name" to be an NSString
. We can also chain requirements. For example, @"age"
: RPValidatorPredicate.isNumber.isOptional
will place a requirement on the value of "age" to be an NSNumber
, but only if it exists in the JSON.