ibireme / YYModel

High performance model framework for iOS/OSX.
MIT License
4.34k stars 917 forks source link

Passing mapping errors #154

Open Emailrus opened 8 years ago

Emailrus commented 8 years ago

Currently it seems, that there is no way to determine if conversion failed due to mapping error or because property in original json was null. For example:

Models


@interface Error : NSObject

@property (nonatomic, strong) NSString *data;

@end

@implementation Error
@end

@interface Result : NSObject

@property (nonatomic, strong) Error *error;
@property (nonatomic, strong) NSString *data;

@end

@implementation Result
@end

{
  "error": {
    "data": 404
  },
  "data": null
}

With this json conversion is fine, Result has an Error object. We can add assumption here, that if Error property is nil, then there were no errors on server.


{
  "error": [
    {"data": 401},
    {"data": 403}
  ],
  "data": null
}

Here server changed its API and now returns errors in array and conversion in old clients will always assign nil to that property, making clients assume, that there were no errors on server.

There is no safe mechanism in YYModel to determine why property is nil. My guess that having a method with + (instancetype)yy_modelWithJSON:(id)json error:(NSError **)pError will help

ibireme commented 8 years ago

You may use this method to validate the input data: https://github.com/ibireme/YYModel#data-validate-and-custom-transform

Emailrus commented 8 years ago

That would require validate every single field of json object, basically making a manual deserialization. Something that this library is trying to automate, isn't it?