forked from nuagenetworks/js-bambou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NUAbstractModel.js
140 lines (129 loc) · 5.47 KB
/
NUAbstractModel.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
import NUAttribute from './NUAttribute';
import NUObject from './NUObject';
const doValidate = (validator, entity, attrObj, formValues) => {
const validationError = validator.validate(entity, attrObj, formValues);
if (validationError) {
entity.validationErrors.set(validator.name, validationError);
}
}
/*
This class models the base Entity
*/
export default class NUAbstractModel extends NUObject {
constructor() {
super();
this._validationErrors = new Map();
this._validators = new Map();
this.registerAttributeValidators();
}
/*
Populates individual properties of 'this' Entity object from the JSON object received
*/
buildFromJSON(JSONObject) {
const attributeDescriptors = this.constructor.attributeDescriptors;
Object.entries(attributeDescriptors).forEach(([localName, attributeObj]) => {
if (attributeObj.remoteName in JSONObject) {
const value = JSONObject[attributeObj.remoteName];
if (attributeObj.attributeType === NUAttribute.ATTR_TYPE_INTEGER || attributeObj.attributeType === NUAttribute.ATTR_TYPE_FLOAT) {
this[localName] = (!value && value !== 0) ? null : isNaN(value) ? value : Number(value);
} else if (attributeObj.attributeType === NUAttribute.ATTR_TYPE_OBJECT && attributeObj.subType && attributeObj.subType !== NUAttribute.ATTR_TYPE_JSON && value) {
//if subType is ATTR_TYPE_JSON, NUEntity instance not applicable
const subtypeEntity = new attributeObj.subType();
this[localName] = subtypeEntity.buildFromJSON(value);
} else if (attributeObj.attributeType === NUAttribute.ATTR_TYPE_LIST && attributeObj.subType && typeof attributeObj.subType !== 'string') {
this[localName] = value ? value.map(item => {
if (attributeObj.subType === NUAttribute.ATTR_TYPE_JSON) {
//list of JSON objects
return item;
} else {
const subtypeEntity = new attributeObj.subType();
return subtypeEntity.buildFromJSON(item);
}
}) : null;
} else {
this[localName] = value;
}
}
});
return this;
}
/*
Return JSON representation of 'this' object
*/
buildJSON() {
return JSON.stringify(this.toObject());
}
toString() {
return this.buildJSON();
}
toObject(props = {}) {
const {isInspect = false} = props;
const attributeDescriptors = this.constructor.attributeDescriptors;
const obj = {};
Object.entries(attributeDescriptors).forEach(([localName, attributeObj]) => {
if (isInspect && attributeObj.isInternal) {
return;
}
let value = this[localName];
if (value) {
if (attributeObj.attributeType === NUAttribute.ATTR_TYPE_OBJECT && attributeObj.subType) {
value = value.toObject();
} else if (attributeObj.attributeType === NUAttribute.ATTR_TYPE_ENUM && typeof value === 'object') {
value = value.name;
} else if (attributeObj.attributeType === NUAttribute.ATTR_TYPE_LIST && attributeObj.subType && typeof attributeObj.subType !== 'string') {
value = value.map(item => item.toObject())
}
}
obj[attributeObj.remoteName] = value;
});
return obj;
}
/*
Register each NUAttribute as validator
*/
registerAttributeValidators() {
Object.values(this.constructor.attributeDescriptors).forEach((attributeObj) => {
this._validators.set(attributeObj.name, attributeObj);
});
}
/*
Register custom validator
*/
registerValidator(...args) {
if (args.length === 2 && args[1]) {
//if the second argument is true, the new validator will be appended to the list of existing validators
const currentValidator = this._validators.get(args[0].name) || [];
if (Array.isArray(currentValidator)) {
this._validators.set(args[0].name, [...currentValidator, args[0]]);
} else {
this._validators.set(args[0].name, [currentValidator, args[0]]);
}
} else {
this._validators.set(args[0].name, args[0]);
}
}
isValid(formValues) {
this.validationErrors.clear();
this.checkErrors(formValues);
return (this.validationErrors.size === 0);
}
checkErrors(formValues) {
const entity = this;
entity._validators.forEach((validator, attributeName) => {
const attrObj = this.constructor.attributeDescriptors[attributeName];
if (Array.isArray((validator))) {
validator.forEach(validatorItem => {
doValidate(validatorItem, entity, attrObj, formValues);
})
} else {
doValidate(validator, entity, attrObj, formValues);
}
});
}
getDefaults() {
return Object.entries(this).reduce((acc, [key, value]) => {
return (value && key !== '_validationErrors' && key !== '_validators')
? { ...acc, [key.substring(1)]: value } : acc;
}, {});
}
}