For now, you can override the initializer. It seems more concise to override validate: as it is called in the last step before Mantle returns the deserialized object. It makes sense to put all your validation logic in the validate method ...
See the final line MTLJSONAdapter
id model = [self.modelClass modelWithDictionary:dictionaryValue error:error]; return [model validate:error] ? model : nil;
This tells us that if our user model returns NO from validate , then Mantle discards the object.
Thus, you can simply do the following in your subclass:
- (BOOL)validate:(NSError **)error { return [super validate:error] && self.name.length > 0; }
Ideally, in your own implementation, you probably want to return the corresponding error.
The validate method then calls Foundation validateValue:forKey:error: for each property that you registered with Mantle in JSONKeyPathsByPropertyKey . Therefore, if you need a more controlled verification setting, you can also check your details here.
Daniel Galasko
source share