This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MCValidation.j
211 lines (158 loc) · 6.46 KB
/
MCValidation.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
@implementation MCValidation : CPObject
{
}
- (BOOL)errorForValue:(id)value
{
[CPException raise:CPInvalidArgumentException reason:@"*** -validate cannot be sent to an abstract object of class " + [self className] + @": Create a concrete instance and override (CPString)errorForValue:"];
}
@end
#pragma mark -
#pragma mark MCStringValidation
// Common
MCValidationOptionAllowBlankKey = "MCValidationOptionAllowBlankKey";
// String validations
MCValidationOptionMinLengthKey = "MCValidationOptionMinLengthKey";
MCValidationOptionMaxLengthKey = "MCValidationOptionMaxLengthKey";
MCValidationOptionAllowEmptyKey = "MCValidationOptionAllowEmptyKey";
// Number validations
MCValidationOptionOnlyIntegerKey = "MCValidationOptionOnlyIntegerKey";
MCValidationOptionGreaterThanKey = "MCValidationOptionGreaterThanKey";
MCValidationOptionMaxKey = "MCValidationOptionMaxKey";
@implementation MCStringValidation : MCValidation
{
int minLength @accessors;
int maxLength @accessors;
BOOL allowBlank @accessors;
BOOL allowEmpty @accessors;
}
+ (MCStringValidation)stringValidationWithOptions:(CPDictionary)options
{
var aStringValidation = [self new];
if(!options)
{
options = [CPDictionary dictionary];
}
if([options containsKey:MCValidationOptionMinLengthKey])
[aStringValidation setMinLength:[options objectForKey:MCValidationOptionMinLengthKey]];
if([options containsKey:MCValidationOptionMaxLengthKey])
[aStringValidation setMaxLength:[options objectForKey:MCValidationOptionMaxLengthKey]];
if([options containsKey:MCValidationOptionAllowBlankKey])
[aStringValidation setAllowBlank:([options objectForKey:MCValidationOptionAllowBlankKey])];
else
[aStringValidation setAllowBlank:YES];
if([options containsKey:MCValidationOptionAllowEmptyKey])
[aStringValidation setAllowEmpty:([options objectForKey:MCValidationOptionAllowEmptyKey])];
else
[aStringValidation setAllowEmpty:YES];
return aStringValidation;
}
- (CPString)errorForValue:(CPString)string
{
if(!allowBlank && (!string || (string && [string isKindOfClass:[CPString class]] && string.trim() == "")))
return MCValidationRequiredFieldErrorMessage;
if(!allowEmpty && [string isKindOfClass:[CPString class]] && string.trim() == "")
return MCValidationRequiredFieldErrorMessage;
if(minLength && string && string.length < minLength)
return MCValidationMinLengthErrorMessage(minLength);
if(maxLength && string && string.length > maxLength)
return MCValidationMaxLengthErrorMessage(maxLength);
return nil;
}
+ (MCStringValidation)notEmptyValidation
{
return [MCStringValidation stringValidationWithOptions:[CPDictionary dictionaryWithObjects:[NO, NO] forKeys:[MCValidationOptionAllowBlankKey, MCValidationOptionAllowEmptyKey]]];
}
@end
#pragma mark -
#pragma mark MCNumberValidation
@implementation MCNumberValidation : MCValidation
{
BOOL allowBlank @accessors;
BOOL onlyInteger @accessors;
CPNumber greaterThan @accessors;
CPNumber max @accessors;
}
+ (MCNumberValidation)numberValidationWithOptions:(CPDictionary)options
{
var aNumberValidation = [self new];
if(!options)
{
options = [CPDictionary dictionary];
}
if([options containsKey:MCValidationOptionAllowBlankKey])
[aNumberValidation setAllowBlank:([options objectForKey:MCValidationOptionAllowBlankKey])];
else
[aNumberValidation setAllowBlank:YES];
if([options containsKey:MCValidationOptionGreaterThanKey])
[aNumberValidation setGreaterThan:[options objectForKey:MCValidationOptionGreaterThanKey]];
if([options containsKey:MCValidationOptionMaxKey])
[aNumberValidation setMax:[options objectForKey:MCValidationOptionMaxKey]];
if([options containsKey:MCValidationOptionOnlyIntegerKey])
[aNumberValidation setOnlyInteger:[options objectForKey:MCValidationOptionOnlyIntegerKey]];
return aNumberValidation;
}
- (CPString)errorForValue:(CPNumber)number
{
var floatValue = [number floatValue];
if(!allowBlank && (floatValue === nil || floatValue === undefined || isNaN(floatValue)))
return MCValidationRequiredFieldErrorMessage;
if(greaterThan != undefined && floatValue && floatValue <= greaterThan)
return MCValidationGreaterThanErrorMessage(greaterThan);
if(max && floatValue > max)
return MCValidationMaxValueErrorMessage(max);
if(onlyInteger && floatValue !== Math.round(floatValue))
return MCValidationOnlyIntegerErrorMessage;
return nil;
}
@end
#pragma mark -
#pragma mark MCAssociationValidation
MCValidationOptionMinChildrenKey = "MCValidationOptionMinChildrenKey";
MCValidationOptionMaxChildrenKey = "MCValidationOptionMaxChildrenKey";
MCValidationOptionValidatesChildrenKey = "MCValidationOptionValidatesChildrenKey";
@implementation MCAssociationValidation : MCValidation
{
int minChildren @accessors;
int maxChildren @accessors;
BOOL validatesChildren @accessors;
}
+ (MCStringValidation)associationValidationWithOptions:(CPDictionary)options
{
var anAssociationValidation = [self new];
if(!options)
{
options = [CPDictionary dictionary];
}
[anAssociationValidation setMinChildren:[options objectForKey:MCValidationOptionMinChildrenKey]];
[anAssociationValidation setMaxChildren:[options objectForKey:MCValidationOptionMaxChildrenKey]];
[anAssociationValidation setValidatesChildren:[options objectForKey:MCValidationOptionValidatesChildrenKey]];
return anAssociationValidation;
}
- (CPString)errorForValue:(CPArray)array
{
if(minChildren > 0 && [array count] < minChildren)
{
return MCValidationMinChildrenErrorMessage;
}
if(maxChildren > 0 && [array count] > maxChildren)
{
return MCValidationMaxChildrenErrorMessage;
}
if(validatesChildren)
{
var childCount = [array count],
childErrors = [CPDictionary dictionary];
while(childCount--)
{
var child = [array objectAtIndex:childCount];
if(![child valid])
{
[childErrors setObject:[child errors] forKey:[child UID]];
}
}
if([childErrors count] > 0)
return childErrors;
}
return nil;
}
@end