forked from nuagenetworks/js-bambou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNUAttribute.js
221 lines (195 loc) · 9.12 KB
/
NUAttribute.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
import NUObject from './NUObject';
import NUAttributeValidationError from './NUAttributeValidationError';
import NUException from './NUException';
import { getLogger } from "./Logger";
const dataTypeMismatchError = (attrObj, attrValue, validateSubType) => (
new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid data type',
`Data type should be ${validateSubType ? attrObj.subType : attrObj.attributeType}`)
);
/*
This class models an attribute object
*/
export default class NUAttribute extends NUObject {
static ATTR_TYPE_BOOLEAN = 'boolean';
static ATTR_TYPE_ENUM = 'enum';
static ATTR_TYPE_FLOAT = 'float';
static ATTR_TYPE_INTEGER = 'integer';
static ATTR_TYPE_LIST = 'list';
static ATTR_TYPE_LONG = 'long';
static ATTR_TYPE_STRING = 'string';
static ATTR_TYPE_OBJECT = 'object';
static ATTR_TYPE_TIMESTAMP = 'long';
static ATTR_TYPE_JSON = 'JSON';
constructor(obj) {
super();
if (!obj.localName || !obj.attributeType) {
throw new NUException(`Invalid localName and/or attributeType localName: ${obj.localName}, attributeType: ${obj.attributeType}`);
}
const remName = (!obj.remoteName) ? obj.localName : obj.remoteName;
this.defineProperties({
attributeType: obj.attributeType,
canOrder: !!obj.canOrder,
canSearch: !!obj.canSearch,
choices: obj.choices || null,
description: obj.description,
displayName: (!obj.displayName) ? obj.localName : obj.displayName,
hasChoices: !(obj.choices === null),
isEmail: !!obj.isEmail,
isIdentifier: !!obj.isIdentifier,
isLogin: !!obj.isLogin,
isPassword: !!obj.isPassword,
isCreateOnly: !!obj.isCreateOnly,
isReadOnly: !!obj.isReadOnly,
isEditable: !(!!obj.isCreateOnly || !!obj.isReadOnly),
isRequired: !!obj.isRequired,
isUnique: !!obj.isUnique,
name: obj.localName,
maxLength: obj.maxLength || -1,
minLength: obj.minLength || -1,
remoteName: remName,
subType: obj.subType,
userlabel: obj.userlabel,
isInternal: obj.isInternal,
minValue: obj.minValue,
maxValue: obj.maxValue,
isTransient: obj.isTransient
});
}
isValueSet(attrValue) {
return ((this.attributeType === NUAttribute.ATTR_TYPE_STRING && attrValue) ||
(this.attributeType !== NUAttribute.ATTR_TYPE_STRING && attrValue !== undefined && attrValue !== null));
}
/*
Invoked from NUEntity.
'this' would correspond to the NUEntity object being validated.
NUAttribute object on which the validation needs to be executed is passed as args[0].
*/
validate(entity, attrObj, formValues) {
if (attrObj) {
const attrValue = entity && entity[attrObj.name];
//if STRING use !attrValue to check if value provided. For all other attribute types use !undefined and !null
if (attrObj.isRequired && !attrObj.isValueSet(attrValue)) {
return new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid input', 'This value is mandatory');
}
if (attrValue !== undefined && attrValue !== null) {
//if value provided
switch (attrObj.attributeType) {
case NUAttribute.ATTR_TYPE_STRING:
return attrObj.validateStringValue(attrValue, attrObj);
case NUAttribute.ATTR_TYPE_ENUM:
return attrObj.validateEnumValue(attrValue, attrObj);
case NUAttribute.ATTR_TYPE_LIST:
return attrObj.validateListValues(attrValue, attrObj);
case NUAttribute.ATTR_TYPE_OBJECT:
return attrObj.validateObjectValue(attrValue, attrObj);
case NUAttribute.ATTR_TYPE_INTEGER:
case NUAttribute.ATTR_TYPE_FLOAT:
case NUAttribute.ATTR_TYPE_LONG:
case NUAttribute.ATTR_TYPE_TIMESTAMP:
return attrObj.validateNumberValue(attrValue, attrObj);
default:
}
}
}
return null;
}
validateListValues(listValues, attrObj) {
if (typeof listValues !== 'object') {
return dataTypeMismatchError(attrObj, listValues);
}
for (const listElementValue of listValues) {
let err = false;
if (typeof listElementValue === 'object') {
//validate type list of embedded objects
if (attrObj.subType && attrObj.subType !== NUAttribute.ATTR_TYPE_JSON && attrObj.subType.getClassName) {
if (listElementValue.getClassName && listElementValue.getClassName() !== attrObj.subType.getClassName()) {
return dataTypeMismatchError(attrObj, listElementValue, true)
}
} else {
getLogger().warn(`Invalid subType for attribute: ${attrObj.name}, subType: ${attrObj.subType}`);
}
}
switch (attrObj.subType) {
case NUAttribute.ATTR_TYPE_STRING:
err = attrObj.validateStringValue(listElementValue, attrObj, true);
if (err) return err;
break;
case NUAttribute.ATTR_TYPE_ENUM:
err = attrObj.validateEnumValue(listElementValue, attrObj, true);
if (err) return err;
break;
case NUAttribute.ATTR_TYPE_INTEGER:
case NUAttribute.ATTR_TYPE_FLOAT:
err = attrObj.validateNumberValue(listElementValue, attrObj, true);
if (err) return err;
break;
default:
}
}
return null;
}
validateStringValue(attrValue, attrObj, validateSubType) {
if (typeof attrValue !== (validateSubType ? attrObj.subType : attrObj.attributeType)) {
return dataTypeMismatchError(attrObj, attrValue, validateSubType);
}
if (attrObj.minLength > -1 && attrValue.length < attrObj.minLength) {
return new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid length',
`Minimum length should be ${attrObj.minLength}, but is ${attrValue.length}`);
}
if (attrObj.maxLength > -1 && attrValue.length > attrObj.maxLength) {
return new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid length',
`Maximum length should be ${attrObj.maxLength}, but is ${attrValue.length}`);
}
return null;
}
validateEnumValue(attrValue, attrObj, validateSubType) {
if (attrObj.choices) {
var choiceValues = attrObj.choices.map(function(choice) { return choice.name; });
if (choiceValues.indexOf(attrValue) === -1) {
return new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid input',
`Allowed values are ${choiceValues}, but value provided is ${attrValue}`);
}
}
return null;
}
validateObjectValue(attrValue, attrObj) {
if (attrObj.subType) {
if (attrObj.subType === NUAttribute.ATTR_TYPE_JSON) {
if (typeof attrValue !== 'object') {
//attrValue not null and not of type object
return new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid subType',
`Expected subType is ${NUAttribute.ATTR_TYPE_JSON}`);
}
} else {
if (!attrValue instanceof attrObj.subType) {
return new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid subType',
`Expected subType is ${attrObj.subType.getClassName()}`);
}
}
}
return null;
}
validateNumberValue(attrValue, attrObj, validateSubType) {
const expectedType = validateSubType ? attrObj.subType : attrObj.attributeType;
if (isNaN(attrValue) || (expectedType === NUAttribute.ATTR_TYPE_INTEGER && (attrValue % 1 !== 0))) {
return dataTypeMismatchError(attrObj, attrValue, validateSubType);
}
if (!isNaN(attrObj.minValue) && attrValue < attrObj.minValue) {
return new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid value',
`Minimum accepted value is ${attrObj.minValue}`);
}
if (!isNaN(attrObj.maxValue) && attrValue > attrObj.maxValue) {
return new NUAttributeValidationError(attrObj.localName, attrObj.remoteName,
'Invalid value',
`Maximum accepted value is ${attrObj.maxValue}`);
}
}
}