-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
validation-group-builder.js
263 lines (226 loc) · 8.51 KB
/
validation-group-builder.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {SwitchCaseValidationRulesCollection} from './validation-rules-collection';
import {ValidationProperty} from './validation-property';
import {ValidationConfig} from './validation-config';
import {
MinimumValueValidationRule,
MinimumInclusiveValueValidationRule,
BetweenValueValidationRule,
InCollectionValidationRule,
MaximumValueValidationRule,
MaximumInclusiveValueValidationRule,
EqualityValidationRule,
EqualityWithOtherLabelValidationRule,
InEqualityValidationRule,
InEqualityWithOtherLabelValidationRule,
EmailValidationRule,
URLValidationRule,
MinimumLengthValidationRule,
MaximumLengthValidationRule,
BetweenLengthValidationRule,
NumericValidationRule,
NoSpacesValidationRule,
DigitValidationRule,
AlphaValidationRule,
AlphaOrWhitespaceValidationRule,
AlphaNumericValidationRule,
AlphaNumericOrWhitespaceValidationRule,
StrongPasswordValidationRule,
MediumPasswordValidationRule,
ContainsOnlyValidationRule,
RegexValidationRule,
CustomFunctionValidationRule
} from './validation-rules';
export class ValidationGroupBuilder {
constructor(observerLocator, validationGroup) {
this.observerLocator = observerLocator;
this.validationRuleCollections = []; //Flattened out queue of the nested collections
this.validationGroup = validationGroup;
}
ensure(propertyName, configurationCallback) {
let newValidationProperty = null;
this.validationRuleCollections = [];
for (let i = 0; i < this.validationGroup.validationProperties.length; i++) {
if (this.validationGroup.validationProperties[i].propertyName === propertyName) {
newValidationProperty = this.validationGroup.validationProperties[i];
if (configurationCallback !== undefined && typeof(configurationCallback) === 'function') {
throw Error('When creating validation rules on binding path ' + propertyName + ' a configuration callback function was provided, but validation rules have previously already been instantiated for this binding path');
}
break;
}
}
if (newValidationProperty === null) {
let propertyResult = this.validationGroup.result.addProperty(propertyName);
let config = new ValidationConfig(this.validationGroup.config);
if (configurationCallback !== undefined && typeof(configurationCallback) === 'function') {
configurationCallback(config);
}
newValidationProperty = new ValidationProperty(this.observerLocator, propertyName, this.validationGroup, propertyResult, config);
this.validationGroup.validationProperties.push(newValidationProperty);
}
this.validationRuleCollections.unshift(newValidationProperty.collectionOfValidationRules);
return this.validationGroup;
}
isNotEmpty() {
this.validationRuleCollections[0].isNotEmpty();
this.checkLast();
return this.validationGroup;
}
canBeEmpty() {
this.validationRuleCollections[0].canBeEmpty();
this.checkLast();
return this.validationGroup;
}
isGreaterThan(minimumValue) {
return this.passesRule(new MinimumValueValidationRule(minimumValue));
}
isGreaterThanOrEqualTo(minimumValue) {
return this.passesRule(new MinimumInclusiveValueValidationRule(minimumValue));
}
isBetween(minimumValue, maximumValue) {
return this.passesRule(new BetweenValueValidationRule(minimumValue, maximumValue));
}
isIn(collection) {
return this.passesRule(new InCollectionValidationRule(collection));
}
isLessThan(maximumValue) {
return this.passesRule(new MaximumValueValidationRule(maximumValue));
}
isLessThanOrEqualTo(maximumValue) {
return this.passesRule(new MaximumInclusiveValueValidationRule(maximumValue));
}
isEqualTo(otherValue, otherValueLabel) {
if (!otherValueLabel) {
return this.passesRule(new EqualityValidationRule(otherValue));
}
return this.passesRule(new EqualityWithOtherLabelValidationRule(otherValue, otherValueLabel));
}
isNotEqualTo(otherValue, otherValueLabel) {
if (!otherValueLabel) {
return this.passesRule(new InEqualityValidationRule(otherValue));
}
return this.passesRule(new InEqualityWithOtherLabelValidationRule(otherValue, otherValueLabel));
}
isEmail() {
return this.passesRule(new EmailValidationRule());
}
isURL() {
return this.passesRule(new URLValidationRule());
}
hasMinLength(minimumValue) {
return this.passesRule(new MinimumLengthValidationRule(minimumValue));
}
hasMaxLength(maximumValue) {
return this.passesRule(new MaximumLengthValidationRule(maximumValue));
}
hasLengthBetween(minimumValue, maximumValue) {
return this.passesRule(new BetweenLengthValidationRule(minimumValue, maximumValue));
}
isNumber() {
return this.passesRule(new NumericValidationRule());
}
containsNoSpaces() {
return this.passesRule(new NoSpacesValidationRule());
}
containsOnlyDigits() {
return this.passesRule(new DigitValidationRule());
}
containsOnlyAlpha() {
return this.passesRule(new AlphaValidationRule());
}
containsOnlyAlphaOrWhitespace() {
return this.passesRule(new AlphaOrWhitespaceValidationRule());
}
containsOnlyAlphanumerics() {
return this.passesRule(new AlphaNumericValidationRule());
}
containsOnlyAlphanumericsOrWhitespace() {
return this.passesRule(new AlphaNumericOrWhitespaceValidationRule());
}
isStrongPassword(minimumComplexityLevel) {
if (minimumComplexityLevel === 4) {
return this.passesRule(new StrongPasswordValidationRule());
}
return this.passesRule(new MediumPasswordValidationRule(minimumComplexityLevel));
}
containsOnly(regex) {
return this.passesRule(new ContainsOnlyValidationRule(regex));
}
matches(regex) {
return this.passesRule(new RegexValidationRule(regex));
}
passes(customFunction, threshold) {
return this.passesRule(new CustomFunctionValidationRule(customFunction, threshold));
}
passesRule(validationRule) {
this.validationRuleCollections[0].addValidationRule(validationRule);
this.checkLast();
return this.validationGroup;
}
checkLast() {
let validationProperty = this.validationGroup.validationProperties[this.validationGroup.validationProperties.length - 1];
validationProperty.validateCurrentValue(false);
}
withMessage(message) {
this.validationRuleCollections[0].withMessage(message);
this.checkLast();
return this.validationGroup;
}
if(conditionExpression) {
//IF is treated as a 'switch' with case 'true' and 'default'
let conditionalCollection = new SwitchCaseValidationRulesCollection(conditionExpression);
conditionalCollection.case(true);
this.validationRuleCollections[0].addValidationRuleCollection(conditionalCollection);
this.validationRuleCollections.unshift(conditionalCollection);
return this.validationGroup;
}
else() {
if (!this.validationRuleCollections[0].default) {
throw Error('Invalid statement: \'else\'');
}
this.validationRuleCollections[0].default();//slightly less object creation then 'case false'
return this.validationGroup;
}
endIf() {
if (!this.validationRuleCollections[0].default) {
throw Error('Invalid statement: \'endIf\'');
}
this.validationRuleCollections.shift(); //go up one level in the nested collections
this.checkLast();
return this.validationGroup;
}
switch(conditionExpression) {
let condition = conditionExpression;
if (condition === undefined) {
let observer = this.validationGroup.validationProperties[this.validationGroup.validationProperties.length - 1].observer;
condition = () => {
return observer.getValue();
};
}
let conditionalCollection = new SwitchCaseValidationRulesCollection(condition);
this.validationRuleCollections[0].addValidationRuleCollection(conditionalCollection);
this.validationRuleCollections.unshift(conditionalCollection);
return this.validationGroup;
}
case(caseLabel) {
if (!this.validationRuleCollections[0].default) {
throw Error('Invalid statement: \'case\'');
}
this.validationRuleCollections[0].case(caseLabel);
return this.validationGroup;
}
default() {
if (!this.validationRuleCollections[0].default) {
throw Error('Invalid statement: \'case\'');
}
this.validationRuleCollections[0].default();
return this.validationGroup;
}
endSwitch() {
if (!this.validationRuleCollections[0].default) {
throw Error('Invalid statement: \'endIf\'');
}
this.validationRuleCollections.shift(); //go up one level in the nested collections
this.checkLast();
return this.validationGroup;
}
}