|
234 | 234 | } |
235 | 235 |
|
236 | 236 | // Check field name |
237 | | - validateName() { |
238 | | - if (!this.query.hasOwnProperty("name")) { |
| 237 | + validateName(query) { |
| 238 | + if (!query.hasOwnProperty("name")) { |
239 | 239 | throw new errors.InvalidFieldDescriptionError("The field's name can't be empty/None."); |
240 | 240 | } |
241 | 241 | else { |
242 | | - let name = this.query["name"]; |
| 242 | + let name = query["name"]; |
243 | 243 | if (name.length > 80) { |
244 | 244 | throw new errors.InvalidFieldDescriptionError("The field's name have a very big content. (Max: 80 chars)"); |
245 | 245 | } |
246 | 246 | } |
247 | 247 | } |
248 | 248 |
|
249 | 249 | // Check field description |
250 | | - validateDescription() { |
251 | | - let description = this.query.description; |
| 250 | + validateDescription(query) { |
| 251 | + let description = query.description; |
252 | 252 | if (description.length > 80){ |
253 | 253 | throw new errors.InvalidFieldDescriptionError("The field's description have a very big content. (Max: 300chars)"); |
254 | 254 | } |
255 | 255 | } |
256 | 256 |
|
257 | 257 | // Check field type |
258 | | - validateFieldType() { |
| 258 | + validateFieldType(query) { |
259 | 259 | // The field should have a type property |
260 | | - if (!this.query.hasOwnProperty("type")){ |
| 260 | + if (!query.hasOwnProperty("type")){ |
261 | 261 | throw new errors.InvalidFieldError("The field should have a type."); |
262 | 262 | } |
263 | 263 | } |
264 | 264 |
|
265 | 265 | // If field is decimal check if it has decimal or decimal-time-series type |
266 | | - validateDecimalType() { |
| 266 | + validateDecimalType(query) { |
267 | 267 | let decimal_types = ["decimal", "decimal-time-series"]; |
268 | | - if (!decimal_types.includes(this.query["decimal-place"])) { |
| 268 | + if (!decimal_types.includes(query["decimal-place"])) { |
269 | 269 | throw new errors.InvalidFieldError("This field is only accepted on type 'decimal' or 'decimal-time-series'"); |
270 | 270 | } |
271 | 271 | } |
272 | 272 |
|
273 | 273 | // Check if string field is valid |
274 | | - checkStrTypeIntegrity() { |
275 | | - if (!this.query.hasOwnProperty("cardinality")){ |
| 274 | + checkStrTypeIntegrity(query) { |
| 275 | + if (!query.hasOwnProperty("cardinality")){ |
276 | 276 | throw new errors.InvalidFieldError("The field with type string should have 'cardinality' key."); |
277 | 277 | } |
278 | 278 | } |
279 | 279 |
|
280 | 280 | // Check if enumerated field is valid |
281 | | - validateEnumeratedType() { |
282 | | - if (!this.query.hasOwnProperty("range")){ |
| 281 | + validateEnumeratedType(query) { |
| 282 | + if (!query.hasOwnProperty("range")){ |
283 | 283 | throw new errors.InvalidFieldError("The 'enumerated' type needs of the 'range' parameter."); |
284 | 284 | } |
285 | 285 | } |
286 | 286 |
|
287 | 287 | // If field is valid this returns true |
288 | 288 | validator() { |
289 | | - this.validateName(); |
290 | | - this.validateFieldType(); |
291 | | - if (this.query["type"] == "string") { |
292 | | - this.checkStrTypeIntegrity(); |
| 289 | + if (this.query instanceof Array) { |
| 290 | + for (let i = 0; i < this.query.length; i++) { |
| 291 | + this.validateField(this.query[i]); |
| 292 | + } |
| 293 | + } else { |
| 294 | + this.validateField(this.query); |
293 | 295 | } |
294 | | - if (this.query["type"] == "enumerated") { |
295 | | - this.validateEnumeratedType(); |
| 296 | + |
| 297 | + return true; |
| 298 | + } |
| 299 | + |
| 300 | + validateField(query) { |
| 301 | + this.validateName(query); |
| 302 | + this.validateFieldType(query); |
| 303 | + if (query["type"] === "string") { |
| 304 | + this.checkStrTypeIntegrity(query); |
296 | 305 | } |
297 | | - if (this.query.hasOwnProperty("description")) { |
298 | | - this.validateDescription(); |
| 306 | + if (query["type"] === "enumerated") { |
| 307 | + this.validateEnumeratedType(query); |
299 | 308 | } |
300 | | - if (this.query.hasOwnProperty('decimal-place')) { |
301 | | - this.validateDecimalType(); |
| 309 | + if (query.hasOwnProperty("description")) { |
| 310 | + this.validateDescription(query); |
| 311 | + } |
| 312 | + if (query.hasOwnProperty("decimal-place")) { |
| 313 | + this.validateDecimalType(query); |
302 | 314 | } |
303 | | - return true; |
304 | 315 | } |
305 | 316 | } |
306 | 317 |
|
|
0 commit comments