Skip to content

Base object validator not called #318

Open
@rpankhurst

Description

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.

Activity

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions