WIP - 21.08.31

This commit is contained in:
Daniel Garcia 2021-08-31 22:35:42 +02:00
parent 7ea0be8551
commit b095bed5ca
19 changed files with 2648 additions and 2338 deletions

65
src/definedObject.js Normal file
View file

@ -0,0 +1,65 @@
import MCF_TypeFactory from './types/typeFactory'
export default class MCF_DefinedObject {
constructor(definition) {
this.definition = definition
this.validations = {}
this.fields = MCF_TypeFactory.buildDefinedObject(this)
this.validateValue(definition.values)
this.setValue(definition.values)
}
getDefinition() {
return this.definition
}
getValue() {
const value = {}
for (const field in this.fields) {
value[field] = this.fields[field].value
}
return value
}
validateValue(value) {
for (const field in this.definition.fields) {
if (value[field] === undefined || value === null) {
if (this.definition.fields[field].required === true) {
throw new Error(`The field ${field} is required`)
}
if (this.definition.fields[field].defaultValue !== undefined) {
value[field] = this.definition.fields[field].defaultValue
}
if (this.definition.fields[field].null === false) {
throw new Error(`The field ${field} can't be undefined`)
}
}
}
}
setValue(value) {
for (const valueKey in value) {
if (value[valueKey] === undefined) {
this.fields[valueKey].value = undefined
} else {
if (this.fields[valueKey] === undefined) {
throw new Error(`The field ${valueKey} doesn't exist in the current DefinedObject: ${JSON.stringify(Object.keys(this.fields), null, 2)}`)
}
this.fields[valueKey].setValue(value[valueKey])
}
}
}
updateArrayField(field, arrayItem, mustBeIncluded) {
const fieldArray = this.getValue()[field] || []
if (!fieldArray.includes(arrayItem) && mustBeIncluded) {
fieldArray.push(arrayItem)
}
if (fieldArray.includes(arrayItem) && !mustBeIncluded) {
fieldArray.splice(fieldArray.indexOf(arrayItem), 1)
}
const newValue = {}
newValue[field] = fieldArray
this.setValue(newValue)
}
}

View file

@ -8,263 +8,10 @@
//TODO: Hacer una función que permita cambiar un atributo de un mcf_component a partir del id del componente, el nombre del atributo y el nuevo valor del atributo
//TODO: Hacer una función que permita encontrar un mcf_component a partir de su id
// --== OBJECTS LIBRARY ==--
// ========================================
class MCF_Type {
constructor(definition) {
this.definition = definition;
this.validations = {};
}
setValue(value) {
this.validate(value);
this.value = value;
}
getValue() {
return this.value;
}
validate(value) {
for (let validation in this.validations) {
this.validations[validation](value, this.definition);
}
}
}
class MCF_String extends MCF_Type {
constructor(definition) {
super(definition);
this.addValidations();
}
addValidations() {
this.validations.type = function(value) {
if (typeof(value) !== "string") {
throw new Error(`MCF_String creation: the type of ${value} isn't "string"`);
}
};
for (let param in this.definition) {
switch (param) {
case 'maxLength':
this.validations.maxLength = function(value, definition) {
if (value.length > definition.maxLength) {
throw new Error(`MCF_String creation: the length of the string "${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_String creation: the length of the string "${value}" is lower than the minimum defined: ${definition.minLength}`);
}
};
break;
}
}
}
}
class MCF_Number extends MCF_Type {
constructor(definition) {
super(definition);
this.addValidations();
}
addValidations() {
this.validations.type = function(value) {
if (typeof(value) !== "number") {
throw new Error(`MCF_Number creation: the type of the value ${value} isn't "number"`);
}
};
for (let param in this.definition) {
switch (param) {
case 'max':
this.validations.max = function(value, definition) {
if (value > definition.max) {
throw new Error(`MCF_Number creation: the value ${value} is higher than the maximum defined: ${definition.max}`);
}
};
break;
case 'min':
this.validations.min = function(value, definition) {
if (value < definition.min) {
throw new Error(`MCF_Number creation: the value ${value} is lower than the minimum defined: ${definition.min}`);
}
};
break;
}
}
}
}
class MCF_Boolean extends MCF_Type {
constructor(definition) {
super(definition);
this.addValidations();
}
addValidations() {
this.validations.type = function(value) {
if (typeof(value) !== "boolean") {
throw new Error(`MCF_Boolean creation: the type of the value ${value} isn't "boolean"`);
}
};
}
}
class MCF_Array extends MCF_Type {
constructor(definition) {
super(definition);
this.addValidations();
}
addValidations() {
this.validations.type = function(value) {
if (!Array.isArray(value)) {
throw new Error(`MCF_Array creation: the type of ${value} isn't "array"`);
}
};
for (let param in this.definition) {
switch (param) {
case 'maxLength':
this.validations.maxLength = function(value, definition) {
if (value.length > definition.maxLength) {
throw new Error(`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_Array creation: the length of the array "${value}" is lower than the minimum defined: ${definition.minLength}`);
}
};
break;
}
}
}
}
class MCF_Enum extends MCF_Type {
constructor(definition) {
super(definition);
this.addValidations();
}
addValidations() {
this.validations.isAvailable = function(value, definition) {
const valueIsAvailable = definition.availableValues.reduce(function(isAvailable, availableValue) {
if (value === availableValue) return true;
return isAvailable;
}, false);
if (!valueIsAvailable) {
throw new Error(`MCF_Enum creation: the value "${value}" isn't one of the next values, included in the field definition: ${definition.availableValues.toString()}`);
}
}
}
}
class MCF_TypeFactory {
static buildDefinedObject(definedObject) {
const objectDefinition = definedObject.getDefinition();
let fields = objectDefinition.fields;
let dictionary = {};
for (let field in fields) {
switch (fields[field]['TYPE']) {
case 'string':
dictionary[field] = new MCF_String(fields[field]);
break;
case 'number':
dictionary[field] = new MCF_Number(fields[field]);
break;
case 'boolean':
dictionary[field] = new MCF_Boolean(fields[field]);
break;
case 'enum':
dictionary[field] = new MCF_Enum(fields[field]);
break;
case 'array':
dictionary[field] = new MCF_Array(fields[field]);
break;
}
}
return dictionary;
}
}
class MCF_DefinedObject {
constructor(definition) {
this.definition = definition;
this.validations = {};
this.fields = MCF_TypeFactory.buildDefinedObject(this);
this.validateValue(definition.value)
this.setValue(definition.value)
}
getDefinition() {
return this.definition;
}
getValue() {
let value = {};
for (let field in this.fields) {
value[field] = this.fields[field].value;
}
return value;
}
validateValue(value) {
for (let field in this.definition.fields) {
if (value[field] === undefined || value === null) {
if (this.definition.fields[field].required === true) {
throw new Error(`The field ${field} is required`);
}
if (this.definition.fields[field].defaultValue !== undefined) {
value[field] = this.definition.fields[field].defaultValue;
}
if (this.definition.fields[field].null === false) {
throw new Error(`The field ${field} can't be undefined`);
}
}
}
}
setValue(value) {
for (let valueKey in value) {
if (value[valueKey] === undefined) {
this.fields[valueKey].value = undefined;
} else {
this.fields[valueKey].setValue(value[valueKey]);
}
}
}
setPropertiesValue(value) {
for (let field in value) {
this.fields[field].setValue(value[field]);
}
}
}
const fileDefinition = {
filename: { TYPE: 'string' },
extension: { TYPE: 'string' },
mimeType: { TYPE: 'string' },
path: { TYPE: 'string' },
content: { TYPE: 'string' },
}
class MCF_File extends MCF_DefinedObject {
constructor(params) {
const fields = (params.fields) ? params.fields : {}
const value = (params.value) ? params.value : params
super({
fields: {...fileDefinition, ...fields },
value: value,
})
}
}
import MCF_DefinedObject from './definedObject'
import MCF_File from './objects/file'
export {
MCF_DefinedObject as definedObject,
MCF_File as mcfFile,
};
MCF_DefinedObject as DefinedObject,
MCF_File as File,
}

17
src/objects/file.js Normal file
View file

@ -0,0 +1,17 @@
import MCF_DefinedObject from '../definedObject'
const fileFields = {
name: { TYPE: 'string', required: true },
extension: { TYPE: 'string' },
mimeType: { TYPE: 'string' },
content: { TYPE: 'string', defaultValue: '' },
}
export default class MCF_File extends MCF_DefinedObject {
constructor(definition) {
super({
fields: { ...fileFields, ...definition.fields },
values: definition.values ? definition.values : definition,
})
}
}

35
src/types/array.js Normal file
View file

@ -0,0 +1,35 @@
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
}
}
}
}

22
src/types/base.js Normal file
View file

@ -0,0 +1,22 @@
export default class MCF_BaseType {
constructor(definition) {
this.definition = definition
this.validations = {}
}
setValue(value) {
this.validate(value)
this.value = value
}
getValue() {
return this.value
}
validate(value) {
for (const validation in this.validations) {
this.validations[validation](value, this.definition)
}
}
}

16
src/types/boolean.js Normal file
View file

@ -0,0 +1,16 @@
import MCF_BaseType from './base'
export default class MCF_Boolean extends MCF_BaseType {
constructor(definition) {
super(definition)
this.addValidations()
}
addValidations() {
this.validations.type = function(value) {
if (typeof(value) !== 'boolean') {
throw new Error(`MCF-Objects ==> MCF_Boolean creation: the type of the value ${value} isn't "boolean"`)
}
}
}
}

20
src/types/enum.js Normal file
View file

@ -0,0 +1,20 @@
import MCF_BaseType from './base'
export default class MCF_Enum extends MCF_BaseType {
constructor(definition) {
super(definition)
this.addValidations()
}
addValidations() {
this.validations.isAvailable = function(value, definition) {
const valueIsAvailable = definition.availableValues.reduce(function(isAvailable, availableValue) {
if (value === availableValue) return true
return isAvailable
}, false)
if (!valueIsAvailable) {
throw new Error(`MCF-Objects ==> MCF_Enum creation: the value "${value}" isn't one of the next values, included in the field definition: ${definition.availableValues.toString()}`)
}
}
}
}

34
src/types/number.js Normal file
View file

@ -0,0 +1,34 @@
import MCF_BaseType from './base'
export default class MCF_Number extends MCF_BaseType {
constructor(definition) {
super(definition)
this.addValidations()
}
addValidations() {
this.validations.type = function(value) {
if (typeof(value) !== 'number') {
throw new Error(`MCF-Objects ==> MCF_Number creation: the type of the value ${value} isn't "number"`)
}
}
for (const param in this.definition) {
switch (param) {
case 'max':
this.validations.max = function(value, definition) {
if (value > definition.max) {
throw new Error(`MCF-Objects ==> MCF_Number creation: the value ${value} is higher than the maximum defined: ${definition.max}`)
}
}
break
case 'min':
this.validations.min = function(value, definition) {
if (value < definition.min) {
throw new Error(`MCF-Objects ==> MCF_Number creation: the value ${value} is lower than the minimum defined: ${definition.min}`)
}
}
break
}
}
}
}

35
src/types/string.js Normal file
View file

@ -0,0 +1,35 @@
import MCF_BaseType from './base'
export default class MCF_String extends MCF_BaseType {
constructor(definition) {
super(definition)
this.addValidations()
}
addValidations() {
this.validations.type = function(value) {
if (typeof(value) !== 'string') {
throw new Error(`MCF-Objects ==> MCF_String creation: the type of ${value} isn't "string"`)
}
}
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_String creation: the length of the string "${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_String creation: the length of the string "${value}" is lower than the minimum defined: ${definition.minLength}`)
}
}
break
}
}
}
}

36
src/types/typeFactory.js Normal file
View file

@ -0,0 +1,36 @@
import MCF_String from './string'
import MCF_Number from './number'
import MCF_Boolean from './boolean'
import MCF_Array from './array'
import MCF_Enum from './enum'
export default class MCF_TypeFactory {
static buildDefinedObject(definedObject) {
const objectDefinition = definedObject.getDefinition()
const fields = objectDefinition.fields
const dictionary = {}
for (const field in fields) {
switch (fields[field]['TYPE']) {
case 'string':
dictionary[field] = new MCF_String(fields[field])
break
case 'number':
dictionary[field] = new MCF_Number(fields[field])
break
case 'boolean':
dictionary[field] = new MCF_Boolean(fields[field])
break
case 'enum':
dictionary[field] = new MCF_Enum(fields[field])
break
case 'array':
dictionary[field] = new MCF_Array(fields[field])
break
default:
throw new Error(`MCF-Objects ==> The object type ${fields[field]['TYPE']} does not exist. Check the field definition in the object definition ${objectDefinition}`)
}
}
return dictionary
}
}