-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
validation-result.js
68 lines (66 loc) · 1.79 KB
/
validation-result.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
export class ValidationResult {
constructor() {
this.isValid = true;
this.properties = {};
}
addProperty(name) {
if (!this.properties[name]) {
this.properties[name] = new ValidationResultProperty(this);
}
return this.properties[name];
}
checkValidity() {
for (let propertyName in this.properties) {
if (!this.properties[propertyName].isValid) {
this.isValid = false;
return;
}
}
this.isValid = true;
}
clear() {
this.isValid = true;
}
}
export class ValidationResultProperty {
constructor(group) {
this.group = group;
this.onValidateCallbacks = [];
this.clear();
}
clear() {
this.isValid = true;
this.isDirty = false;
this.message = '';
this.failingRule = null;
this.latestValue = null;
this.notifyObserversOfChange();
}
onValidate(onValidateCallback) {
this.onValidateCallbacks.push(onValidateCallback);
}
notifyObserversOfChange() {
for (let i = 0; i < this.onValidateCallbacks.length; i++) {
let callback = this.onValidateCallbacks[i];
callback(this);
}
}
setValidity(validationResponse, shouldBeDirty) {
let notifyObservers = (!this.isDirty && shouldBeDirty)
|| (this.isValid !== validationResponse.isValid)
|| (this.message !== validationResponse.message);
if (shouldBeDirty) {
this.isDirty = true;
}
this.message = validationResponse.message;
this.failingRule = validationResponse.failingRule;
this.isValid = validationResponse.isValid; //Set isValid last in case someone has observed 'isValid'
this.latestValue = validationResponse.latestValue;
if (this.isValid !== this.group.isValid) {
this.group.checkValidity();
}
if (notifyObservers) {
this.notifyObserversOfChange();
}
}
}