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

What is a proper way to validate present of mandatory field #256

Closed
sarunw opened this issue Mar 6, 2014 · 2 comments
Closed

What is a proper way to validate present of mandatory field #256

sarunw opened this issue Mar 6, 2014 · 2 comments

Comments

@sarunw
Copy link

sarunw commented Mar 6, 2014

Now I'm using + (id)modelOfClass:(Class)modelClass fromJSONDictionary:(NSDictionary *)JSONDictionary error:(NSError **)error to parse JSON to an object, but if some field is missing it would just assign nil to that property. Is there a way to validate required field and return an error, so I can handle it properly. Didn't see any validation in README.

Another question is when I use + (NSValueTransformer *)mtl_JSONArrayTransformerWithModelClass:(Class)modelClass if the data return isn't array it will failed from the NSAssert([dictionaries isKindOfClass:NSArray.class], @"Expected a array of dictionaries, got: %@", dictionaries); is there a way I can handle this before NSAssert happen, because this will crash the app in production if API is changed.

@robb
Copy link
Member

robb commented Mar 6, 2014

You can implement -validate<Key>:error: so that it considers nil an error and then call -[MTLModel validate:] after deserializing to make sure all fields are in place.

Your second problem will be fixed 2.0 where this will simply be an decoding error. In the mean-time, I guess you can simply copy the current +mtl_JSONArrayTransformerWithModelClass: implementation and make it return something useful for you if its expectations are not met.

Please note that there will be additional changes to the behavior of said method 2.0.

@robb
Copy link
Member

robb commented Nov 14, 2014

Closing this one for lack of activity

@robb robb closed this as completed Nov 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants