Oolite
Loading...
Searching...
No Matches
OOPListSchemaVerifier.h
Go to the documentation of this file.
1/*
2
3OOPListSchemaVerifier.h
4
5Utility class to verify the structure of a property list based on a schema
6(which is itself a property list).
7
8
9Copyright (C) 2007-2013 Jens Ayton
10
11Permission is hereby granted, free of charge, to any person obtaining a copy
12of this software and associated documentation files (the "Software"), to deal
13in the Software without restriction, including without limitation the rights
14to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15copies of the Software, and to permit persons to whom the Software is
16furnished to do so, subject to the following conditions:
17
18The above copyright notice and this permission notice shall be included in all
19copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28
29*/
30
31#import "OOOXPVerifier.h"
32
33#if OO_OXP_VERIFIER_ENABLED
34
36#import <Foundation/Foundation.h>
37
38@interface OOPListSchemaVerifier : NSObject {
39@private
40 NSDictionary* _schema;
41 NSDictionary* _definitions;
42
44 uint32_t _badDelegateWarning : 1;
45}
46
47+ (instancetype)verifierWithSchema:(NSDictionary*)schema;
48- (id)initWithSchema:(NSDictionary*)schema;
49
50- (void)setDelegate:(id)delegate;
51- (id)delegate;
52
53- (BOOL)verifyPropertyList:(id)plist named:(NSString*)name;
54
55/* Convert a key path (such as provided to the delegate method
56 -verifier:withPropertyList:failedForProperty:atPath:expectedType:) to a
57 human-readable string. Strings are separated by dots and numbers are give
58 brackets. For instance, the key path ( "adder-player", "custom_views", 0,
59 "view_description" ) is transfomed to
60 "adder-player.custom_views[0].view_description".
61*/
62+ (NSString*)descriptionForKeyPath:(NSArray*)keyPath;
63
64@end
65
66@interface NSObject (OOPListSchemaVerifierDelegate)
67
68// Handle "delegated types". Return YES for valid, NO for invalid.
69- (BOOL)verifier:(OOPListSchemaVerifier*)verifier
70 withPropertyList:(id)rootPList
71 named:(NSString*)name
72 testProperty:(id)subPList
73 atPath:(NSArray*)keyPath
74 againstType:(NSString*)typeKey
75 error:(NSError**)outError;
76
77/* Method notifying of verification failure.
78 Return YES to continue verifying, NO to stop.
79*/
80- (BOOL)verifier:(OOPListSchemaVerifier*)verifier
81 withPropertyList:(id)rootPList
82 named:(NSString*)name
83 failedForProperty:(id)subPList
84 withError:(NSError*)error
85 expectedType:(NSDictionary*)localSchema;
86
87@end
88
89// NSError domain and codes used to report schema verifier errors.
90extern NSString* const kOOPListSchemaVerifierErrorDomain;
91
92extern NSString* const kPListKeyPathErrorKey; // Array specifying key path in plist.
93extern NSString* const kSchemaKeyPathErrorKey; // Array specifying key path in schema.
94
95extern NSString* const kExpectedClassErrorKey; // Expected class. Nil for vector and quaternion.
96extern NSString* const kExpectedClassNameErrorKey; // String describing expected class. May be more specific (for instance, "boolean" or "positive integer" for NSNumber).
97extern NSString* const kUnknownKeyErrorKey; // Unallowed key found in dictionary.
98extern NSString* const kMissingRequiredKeysErrorKey; // Set of required keys not present in dictionary
99extern NSString* const kMissingSubStringErrorKey; // String or array of strings not found for kPListErrorStringPrefixMissing/kPListErrorStringSuffixMissing/kPListErrorStringSubstringMissing.
100extern NSString* const kUnnownFilterErrorKey; // Unrecognized filter specifier for kPListErrorSchemaUnknownFilter. Not specified if filter is not a string.
101extern NSString* const kErrorsByOptionErrorKey; // Dictionary of errors for oneOf types.
102
103extern NSString* const kUnknownTypeErrorKey; // Set for kPListErrorSchemaUnknownType.
104extern NSString* const kUndefinedMacroErrorKey; // Set for kPListErrorSchemaUndefiniedMacroReference.
105
106// All plist verifier errors have a short error description in their -localizedFailureReason.
107
108typedef enum {
110 kPListErrorInternal, // PList verifier did something dumb.
111
112 // Verification errors -- property list doesn't match schema.
113 kPListErrorTypeMismatch, // Basic type mismatch -- array instead of number, for instance.
114
115 kPListErrorMinimumConstraintNotMet, // minimum/minCount/minLength constraint violated
116 kPListErrorMaximumConstraintNotMet, // maximum/maxCount/maxLength constraint violated
117 kPListErrorNumberIsNegative, // Negative number in positiveFloat.
118
119 kPListErrorStringPrefixMissing, // String does not match requiredPrefix rule. kMissingSubStringErrorKey is set.
120 kPListErrorStringSuffixMissing, // String does not match requiredSuffix rule. kMissingSubStringErrorKey is set.
121 kPListErrorStringSubstringMissing, // String does not match requiredSuffix rule. kMissingSubStringErrorKey is set.
122
123 kPListErrorDictionaryUnknownKey, // Unknown key for dictionary with allowOthers = NO.
124 kPListErrorDictionaryMissingRequiredKeys, // requiredKeys rule is not fulfilled. The missing keys are listed in kMissingRequiredKeysErrorKey.
125
126 kPListErrorEnumerationBadValue, // Enumeration type contains string that isn't in permitted set.
127
128 kPListErrorOneOfNoMatch, // No match for oneOf type. kErrorsByOptionErrorKey is set to a dictionary of type specifiers to errors. Note that the keys in this dictionary can be either strings or dictionaries.
129
130 kPListDelegatedTypeError, // Delegate's verification method failed. If it returned an error, this will be in NSUnderlyingErrorKey.
131
132 // Schema errors -- schema is broken.
134
135 kPListErrorSchemaBadTypeSpecifier, // Bad type specifier - specifier is not a string or a dictionary, or is a dictionary with no type key. kUndefinedMacroErrorKey is set.
136 kPListErrorSchemaUndefiniedMacroReference, // Reference to $macro not found in $definitions.
137 kPListErrorSchemaUnknownType, // Unknown type specified in type specifier. kUnknownTypeErrorKey is set.
138 kPListErrorSchemaNoOneOfOptions, // OneOf clause has no options array.
139 kPListErrorSchemaNoEnumerationValues, // Enumeration clause has no values array.
140 kPListErrorSchemaUnknownFilter, // Bad value for string/enumeration filter specifier.
141 kPListErrorSchemaBadComparator, // String comparision requirement value (requiredPrefix etc.) is not a string.
142
145
150
151@interface NSError (OOPListSchemaVerifierConveniences)
152
153- (NSArray*)plistKeyPath;
154- (NSString*)plistKeyPathDescription; // Result of calling +[OOPListSchemaVerifier descriptionForKeyPath:] on kPListKeyPathErrorKey.
155
156- (NSSet*)missingRequiredKeys;
157
158- (Class)expectedClass;
159- (NSString*)expectedClassName;
160
161@end
162
163#endif // OO_OXP_VERIFIER_ENABLED
#define OOINLINE
NSString *const kMissingRequiredKeysErrorKey
NSString *const kExpectedClassErrorKey
OOINLINE BOOL OOPlistErrorIsSchemaError(OOPListSchemaVerifierErrorCode error)
NSString *const kUndefinedMacroErrorKey
NSString *const kSchemaKeyPathErrorKey
NSString *const kErrorsByOptionErrorKey
NSString *const kExpectedClassNameErrorKey
OOPListSchemaVerifierErrorCode
@ kPListErrorDictionaryMissingRequiredKeys
@ kPListErrorSchemaNoEnumerationValues
@ kPListErrorStartOfSchemaErrors
@ kPListErrorNumberIsNegative
@ kPListErrorEnumerationBadValue
@ kPListErrorSchemaBadTypeSpecifier
@ kPListErrorInternal
@ kPListErrorSchemaNoOneOfOptions
@ kPListErrorTypeMismatch
@ kPListErrorStringSubstringMissing
@ kPListErrorSchemaUnknownType
@ kPListErrorMaximumConstraintNotMet
@ kPListErrorNone
@ kPListErrorDictionaryUnknownKey
@ kPListErrorMinimumConstraintNotMet
@ kPListErrorLastErrorCode
@ kPListDelegatedTypeError
@ kPListErrorStringSuffixMissing
@ kPListErrorOneOfNoMatch
@ kPListErrorSchemaUndefiniedMacroReference
@ kPListErrorSchemaBadComparator
@ kPListErrorSchemaUnknownFilter
@ kPListErrorStringPrefixMissing
NSString *const kUnnownFilterErrorKey
NSString *const kPListKeyPathErrorKey
NSString *const kUnknownTypeErrorKey
NSString *const kOOPListSchemaVerifierErrorDomain
NSString *const kMissingSubStringErrorKey
NSString *const kUnknownKeyErrorKey