Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a strict version of modelOfClass:fromJSONDictionary: ? #518

Closed
knlr opened this issue May 16, 2015 · 5 comments
Closed

a strict version of modelOfClass:fromJSONDictionary: ? #518

knlr opened this issue May 16, 2015 · 5 comments

Comments

@knlr
Copy link

knlr commented May 16, 2015

Consider I'm using modelOfClass:fromJSONDictionary: and I want to be able to tell if something in the mapping went wrong, like a missing key or a type error (e.g. property is a NSNumber but it's a string in the JSON and an NSString is assigned).

I could write validation functions in my model classes checking the validity of each property. Or there could be a 'strict' version of modelOfClass:fromJSONDictionary that returns nil and gives an error when a key was not found or a wrong type is assigned to a property. Is it possible? If so, would you add this?

@jspahrsummers
Copy link
Member

AFAIK the method should already have this “strict” behavior. Are you seeing something different?

@knlr
Copy link
Author

knlr commented May 17, 2015

Yes I do. Maybe I didn't make myself clear. When a mapped key is not contained in the JSON, the mapped property will be nil, the model object will be created and the error is nil. Example:

@interface MTLTweet : MTLModel <MTLJSONSerializing>

@property (nonatomic) NSString* text;
@property (nonatomic) NSString* userName;
@property (nonatomic) NSString* profileImageURL;

@end

@implementation MTLTweet

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
        return @{
             @"text": @"Text", 
             @"userName": @"user.name",
             @"profileImageURL": @"user.profileimageurl" // intentionelly misspelled 
             };
}

@end

the NSDictionary (abbreviated):
(lldb) po response

{        
    text = "Piraeus Egypt has highest retail banking portfolio growth for 2014 http://t.co/t4YI8M7bee #DaytonFinancialAdvice";    
    user =     {    
        name = "John Stamos";
        "profile_image_url" = "http://pbs.twimg.com/profile_images/378800000731641435/275da1fb86bcb0d8b55a9da640881643_normal.jpeg";
    };
}

instantiating the model object:

MTLTweet* tweet = [MTLJSONAdapter modelOfClass:MTLTweet.class
                            fromJSONDictionary:response
                                         error:&error];

(lldb) po tweet
<MTLTweet: 0xc767c80> {
    profileImageURL = "<null>";
    text = "Piraeus Egypt has highest retail banking portfolio growth for 2014 http://t.co/t4YI8M7bee #DaytonFinancialAdvice";
    userName = John Stamos;
}

I want the tweet to be nil if one of the properties is nil.

@robb
Copy link
Member

robb commented May 17, 2015

I could write validation functions in my model classes checking the validity of each property.

In this case, -validateProfileImageURL:error: would never be invoked. I'd recommend overriding -[MTLModel validate:] and check profileImageURL != nil there.

@danielgalasko
Copy link
Contributor

So @knlr just to confirm, you would like to specify a list of non nullable properties on an MTLModel subclass?

Like a MTLJSONCompulsoryKeyPathsSerializing protocol that the JSONAdapter would use as an added step during serialization?

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants