How to validate a property list has the right structure

I need to read data from the user. For convenience, the data will be in a property list, so it's easy to get a dictionary containing the property list data. But, since it's coming from outside, I need to validate that the data is in the required format, i.e. it has the right keys and the right sort of data for each key, e.g. <name> has a string, <keys> has an array of appropriate values.

Since this is part of a long-established product, and targets 10.13, I want to do this in Objective-C if possible. I've been working mostly with Swift in recent years, so I've forgotten a lot of what I used to know about Objective-C, I'm sure.

My first thought was to obtain the value for each key and check the class type with isa, but I see that's deprecated in macOS 13 with no replacement. I don't see another way to check the class.

I'm sure other people have solved the same problem, but my searches have not turned up any answers.

Answered by DTS Engineer in 825084022

libxml2 is available on all of our platforms at this time. You can use that for xml dtd validation. There is an example available among the libxml2 documentation (see http://www.xmlsoft.org/examples/#parse2.c for details). Note: xml dtd validation will validate the format of the xml based on the dtd. For validating the values stored in the xml, you'll need to read the xml and validate the values based on the keys yourself.

libxml2 is available on all of our platforms at this time. You can use that for xml dtd validation. There is an example available among the libxml2 documentation (see http://www.xmlsoft.org/examples/#parse2.c for details). Note: xml dtd validation will validate the format of the xml based on the dtd. For validating the values stored in the xml, you'll need to read the xml and validate the values based on the keys yourself.

As I said, it's a property list, so the only validation I get is that it is a well-formed property list. It's the validating the values that I'm stuck on. Is there something better than code like this?

NSObject *name = dict[@"name"];
if (![name isKindOfClass:[NSString class]]) {
    // report failure
} 

In Swift I would be using the as? operator, and I guess that this is the closest equivalent. (My memory only recalled the isa method, but further searching turned up isKindOfClass.)

How to validate a property list has the right structure
 
 
Q