mcf-objects/src/types/array.js
2021-08-31 22:35:42 +02:00

35 lines
1.1 KiB
JavaScript

import MCF_BaseType from './base'
export default class MCF_Array extends MCF_BaseType {
constructor(definition) {
super(definition)
this.addValidations()
}
addValidations() {
this.validations.type = function(value) {
if (!Array.isArray(value)) {
throw new Error(`MCF-Objects ==> MCF_Array creation: the type of ${value} isn't "array"`)
}
}
for (const param in this.definition) {
switch (param) {
case 'maxLength':
this.validations.maxLength = function(value, definition) {
if (value.length > definition.maxLength) {
throw new Error(`MCF-Objects ==> MCF_Array creation: the length of the array "${value}" is higher than the maximum defined: ${definition.maxLength}`)
}
}
break
case 'minLength':
this.validations.minLength = function(value, definition) {
if (value.length < definition.minLength) {
throw new Error(`MCF-Objects ==> MCF_Array creation: the length of the array "${value}" is lower than the minimum defined: ${definition.minLength}`)
}
}
break
}
}
}
}