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

Base object validator not called #318

Open
rpankhurst opened this issue Sep 7, 2020 · 0 comments
Open

Base object validator not called #318

rpankhurst opened this issue Sep 7, 2020 · 0 comments

Comments

@rpankhurst
Copy link

I'm deserializing a JSON document that has multiple properties that aren't found in the target object and since the server-side object structure is subject to change we use the validateValue() callback to filter out properties that aren't on the local object so that we don't get NSUnknownKeyExceptions while deserializing.

It looks like there's a logic error in EVReflection.swift that's keeping this check from being done. validateValue() exists on the target object and is set to throw an exception on invalid keys but setValue() gets called leading to an NSUnknownKeyException. The problem seems to be the check on line 989

public static func setObjectValue<T>(_ anyObject: T, key: String, theValue: Any?, typeInObject: String? = nil, valid: Bool, conversionOptions: ConversionOptions = .DefaultDeserialize, parents: [NSObject] = []) where T: NSObject {
...
(line 987) var setValue: AnyObject? = value as AnyObject?
let validateFunction = "validate" + key.prefix(1).uppercased() + key.dropFirst() + ":error:"
if (anyObject as AnyObject).responds(to: Selector(validateFunction)) {
    try anyObject.validateValue(&setValue, forKey: key)
}
anyObject.setValue(setValue, forKey: key)

Which as I understand is checking if a custom key validator exists and if not call the base validator validateValue(). I think the check needs to be changed to

if !(anyObject as AnyObject).responds(to: Selector(validateFunction)) {
    try anyObject.validateValue(&setValue, forKey: key)
}

I've been doing this in my local copy the last few times I updated the library and it seems to fix the problem without causing other side effects.

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

1 participant