diff --git a/.eslintrc.js b/.eslintrc.js index 1ddbb4c..d5fe488 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -49,4 +49,4 @@ module.exports = { 'ignoreReadBeforeAssign': true, }], }, -}; +} diff --git a/dist/mcf-objects.js b/dist/mcf-objects.js index b6883d0..c861f4a 100644 --- a/dist/mcf-objects.js +++ b/dist/mcf-objects.js @@ -1,2 +1,2 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.mcfObjects=e():t.mcfObjects=e()}(window,(function(){return function(t){var e={};function n(i){if(e[i])return e[i].exports;var r=e[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(i,r,function(e){return t[e]}.bind(null,r));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";function i(t){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&o(t,e)}function o(t,e){return(o=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function a(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,i=c(t);if(e){var r=c(this).constructor;n=Reflect.construct(i,arguments,r)}else n=i.apply(this,arguments);return u(this,n)}}function u(t,e){return!e||"object"!==i(e)&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function c(t){return(c=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function f(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function l(t,e){for(var n=0;ne.maxLength)throw new Error('MCF_String creation: the length of the string "'.concat(t,'" is higher than the maximum defined: ').concat(e.maxLength))};break;case"minLength":this.validations.minLength=function(t,e){if(t.lengthe.max)throw new Error("MCF_Number creation: the value ".concat(t," is higher than the maximum defined: ").concat(e.max))};break;case"min":this.validations.min=function(t,e){if(te.maxLength)throw new Error('MCF_Array creation: the length of the array "'.concat(t,'" is higher than the maximum defined: ').concat(e.maxLength))};break;case"minLength":this.validations.minLength=function(t,e){if(t.lengthe.maxLength)throw new Error('MCF_String creation: the length of the string "'.concat(t,'" is higher than the maximum defined: ').concat(e.maxLength))};break;case"minLength":this.validations.minLength=function(t,e){if(t.lengthe.max)throw new Error("MCF_Number creation: the value ".concat(t," is higher than the maximum defined: ").concat(e.max))};break;case"min":this.validations.min=function(t,e){if(te.maxLength)throw new Error('MCF_Array creation: the length of the array "'.concat(t,'" is higher than the maximum defined: ').concat(e.maxLength))};break;case"minLength":this.validations.minLength=function(t,e){if(t.length definition.maxLength){\n throw new Error(`MCF_String creation: the length of the string \"${value}\" is higher than the maximum defined: ${definition.maxLength}`);\n }\n };\n break;\n case 'minLength':\n this.validations.minLength = function(value, definition){\n if (value.length < definition.minLength){\n throw new Error(`MCF_String creation: the length of the string \"${value}\" is lower than the minimum defined: ${definition.minLength}`);\n }\n };\n break;\n }\n }\n }\n}\nclass MCF_Number extends MCF_Type{\n constructor(definition){\n super(definition);\n this.addValidations();\n }\n\n addValidations(){\n this.validations.type = function(value) {\n if (typeof (value) !== \"number\") {\n throw new Error(`MCF_Number creation: the type of the value ${value} isn't \"number\"`);\n }\n };\n\n for (let param in this.definition){\n switch (param) {\n case 'max':\n this.validations.max = function(value, definition){\n if (value > definition.max){\n throw new Error(`MCF_Number creation: the value ${value} is higher than the maximum defined: ${definition.max}`);\n }\n };\n break;\n case 'min':\n this.validations.min = function(value, definition){\n if (value < definition.min){\n throw new Error(`MCF_Number creation: the value ${value} is lower than the minimum defined: ${definition.min}`);\n }\n };\n break;\n }\n }\n }\n}\nclass MCF_Boolean extends MCF_Type{\n constructor(definition){\n super(definition);\n this.addValidations();\n }\n\n addValidations(){\n this.validations.type = function(value) {\n if (typeof (value) !== \"boolean\") {\n throw new Error(`MCF_Boolean creation: the type of the value ${value} isn't \"boolean\"`);\n }\n };\n }\n}\nclass MCF_Array extends MCF_Type {\n constructor(definition){\n super(definition);\n this.addValidations();\n }\n\n addValidations(){\n this.validations.type = function(value) {\n if (!Array.isArray(value)) {\n throw new Error(`MCF_Array creation: the type of ${value} isn't \"array\"`);\n }\n };\n\n for (let param in this.definition){\n switch (param) {\n case 'maxLength':\n this.validations.maxLength = function(value, definition){\n if (value.length > definition.maxLength){\n throw new Error(`MCF_Array creation: the length of the array \"${value}\" is higher than the maximum defined: ${definition.maxLength}`);\n }\n };\n break;\n case 'minLength':\n this.validations.minLength = function(value, definition){\n if (value.length < definition.minLength){\n throw new Error(`MCF_Array creation: the length of the array \"${value}\" is lower than the minimum defined: ${definition.minLength}`);\n }\n };\n break;\n }\n }\n }\n}\nclass MCF_Enum extends MCF_Type{\n constructor(definition){\n super(definition);\n this.addValidations();\n }\n\n addValidations(){\n this.validations.isAvailable = function(value, definition){\n const valueIsAvailable = definition.availableValues.reduce(function(isAvailable, availableValue){\n if (value === availableValue) return true;\n return isAvailable;\n }, false);\n if (!valueIsAvailable){\n throw new Error(`MCF_Enum creation: the value \"${value}\" isn't one of the next values, included in the field definition: ${definition.availableValues.toString()}`);\n }\n }\n }\n}\nclass MCF_TypeFactory {\n static buildDefinedObject(definedObject) {\n const objectDefinition = definedObject.getDefinition();\n let fields = objectDefinition.fields;\n let dictionary = {};\n for (let field in fields) {\n switch (fields[field]['TYPE']) {\n case 'string':\n dictionary[field] = new MCF_String(fields[field]);\n break;\n case 'number':\n dictionary[field] = new MCF_Number(fields[field]);\n break;\n case 'boolean':\n dictionary[field] = new MCF_Boolean(fields[field]);\n break;\n case 'enum':\n dictionary[field] = new MCF_Enum(fields[field]);\n break;\n case 'array':\n dictionary[field] = new MCF_Array(fields[field]);\n break;\n }\n }\n return dictionary;\n }\n}\nclass MCF_DefinedObject {\n constructor(definition){\n this.definition = definition;\n this.validations = {};\n this.fields = MCF_TypeFactory.buildDefinedObject(this);\n this.setValue(definition.value)\n }\n\n getDefinition(){\n return this.definition;\n }\n\n getValue(){\n let value = {};\n for (let field in this.fields){\n value[field] = this.fields[field].value;\n }\n return value;\n }\n\n setValue(value){\n for (let field in this.definition.fields){\n if (value[field] === undefined || value === null){\n if (this.definition.fields[field].required === true){\n throw new Error(`The field ${field} is required`);\n }\n if (this.definition.fields[field].defaultValue !== undefined){\n value[field] = this.definition.fields[field].defaultValue;\n }\n if (this.definition.fields[field].null === false){\n throw new Error(`The field ${field} can't be undefined`);\n }\n }\n }\n\n for (let field in value){\n if (value[field] === undefined){\n this.fields[field].value = undefined;\n } else {\n this.fields[field].setValue(value[field]);\n }\n }\n }\n\n setPropertiesValue(value){\n for (let field in value){\n this.fields[field].setValue(value[field]);\n }\n }\n}\n\nconst fileDefinition = {\n filename: {TYPE: 'string', required: true},\n extension: {TYPE: 'string', required: true},\n path: {TYPE: 'string', required: true},\n content: {TYPE: 'string'},\n}\n\nclass MCF_File extends MCF_DefinedObject {\n constructor(params) {\n super({\n fields: fileDefinition,\n value: params,\n })\n }\n}\n\nexport {\n MCF_DefinedObject as definedObject,\n MCF_File as mcfFile,\n};\n"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack://mcfObjects/webpack/universalModuleDefinition","webpack://mcfObjects/webpack/bootstrap","webpack://mcfObjects/./src/index.js"],"names":["root","factory","exports","module","define","amd","window","installedModules","__webpack_require__","moduleId","i","l","modules","call","m","c","d","name","getter","o","Object","defineProperty","enumerable","get","r","Symbol","toStringTag","value","t","mode","__esModule","ns","create","key","bind","n","object","property","prototype","hasOwnProperty","p","s","MCF_Type","definition","this","validations","validate","validation","MCF_String","addValidations","param","type","Error","maxLength","length","minLength","MCF_Number","max","min","MCF_Boolean","MCF_Array","Array","isArray","MCF_Enum","isAvailable","availableValues","reduce","availableValue","toString","MCF_TypeFactory","definedObject","fields","getDefinition","dictionary","field","MCF_DefinedObject","buildDefinedObject","validateValue","setValue","undefined","required","defaultValue","valueKey","fileDefinition","filename","TYPE","extension","mimeType","path","content","MCF_File","params"],"mappings":"CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,IACQ,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,GACe,iBAAZC,QACdA,QAAoB,WAAID,IAExBD,EAAiB,WAAIC,IARvB,CASGK,QAAQ,WACX,O,YCTE,IAAIC,EAAmB,GAGvB,SAASC,EAAoBC,GAG5B,GAAGF,EAAiBE,GACnB,OAAOF,EAAiBE,GAAUP,QAGnC,IAAIC,EAASI,EAAiBE,GAAY,CACzCC,EAAGD,EACHE,GAAG,EACHT,QAAS,IAUV,OANAU,EAAQH,GAAUI,KAAKV,EAAOD,QAASC,EAAQA,EAAOD,QAASM,GAG/DL,EAAOQ,GAAI,EAGJR,EAAOD,QA0Df,OArDAM,EAAoBM,EAAIF,EAGxBJ,EAAoBO,EAAI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,SAAU,CAAEF,KAAM,UAClBG,KAAM,CAAEH,KAAM,UACdI,QAAS,CAAEJ,KAAM,WAGfK,E,8BACF,WAAYC,GAAQ,UAClB,IAAMpB,EAAUoB,EAAOpB,OAAUoB,EAAOpB,OAAS,GAC3C5C,EAASgE,EAAOhE,MAASgE,EAAOhE,MAAQgE,EAF5B,mBAGV,CACFpB,OAAQ,EAAF,KAAMY,GAAmBZ,GAC/B5C,MAAOA,I,UANIgD","file":"mcf-objects.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"mcfObjects\"] = factory();\n\telse\n\t\troot[\"mcfObjects\"] = factory();\n})(window, function() {\nreturn "," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n","// --== OBJECTS LIBRARY ==--\n// ========================================\n\nclass MCF_Type {\n constructor(definition) {\n this.definition = definition;\n this.validations = {};\n\n }\n\n setValue(value) {\n this.validate(value);\n this.value = value;\n }\n\n getValue() {\n return this.value;\n }\n\n validate(value) {\n for (let validation in this.validations) {\n this.validations[validation](value, this.definition);\n }\n }\n}\nclass MCF_String extends MCF_Type {\n constructor(definition) {\n super(definition);\n this.addValidations();\n }\n\n addValidations() {\n this.validations.type = function(value) {\n if (typeof(value) !== \"string\") {\n throw new Error(`MCF_String creation: the type of ${value} isn't \"string\"`);\n }\n };\n\n for (let param in this.definition) {\n switch (param) {\n case 'maxLength':\n this.validations.maxLength = function(value, definition) {\n if (value.length > definition.maxLength) {\n throw new Error(`MCF_String creation: the length of the string \"${value}\" is higher than the maximum defined: ${definition.maxLength}`);\n }\n };\n break;\n case 'minLength':\n this.validations.minLength = function(value, definition) {\n if (value.length < definition.minLength) {\n throw new Error(`MCF_String creation: the length of the string \"${value}\" is lower than the minimum defined: ${definition.minLength}`);\n }\n };\n break;\n }\n }\n }\n}\nclass MCF_Number extends MCF_Type {\n constructor(definition) {\n super(definition);\n this.addValidations();\n }\n\n addValidations() {\n this.validations.type = function(value) {\n if (typeof(value) !== \"number\") {\n throw new Error(`MCF_Number creation: the type of the value ${value} isn't \"number\"`);\n }\n };\n\n for (let param in this.definition) {\n switch (param) {\n case 'max':\n this.validations.max = function(value, definition) {\n if (value > definition.max) {\n throw new Error(`MCF_Number creation: the value ${value} is higher than the maximum defined: ${definition.max}`);\n }\n };\n break;\n case 'min':\n this.validations.min = function(value, definition) {\n if (value < definition.min) {\n throw new Error(`MCF_Number creation: the value ${value} is lower than the minimum defined: ${definition.min}`);\n }\n };\n break;\n }\n }\n }\n}\nclass MCF_Boolean extends MCF_Type {\n constructor(definition) {\n super(definition);\n this.addValidations();\n }\n\n addValidations() {\n this.validations.type = function(value) {\n if (typeof(value) !== \"boolean\") {\n throw new Error(`MCF_Boolean creation: the type of the value ${value} isn't \"boolean\"`);\n }\n };\n }\n}\nclass MCF_Array extends MCF_Type {\n constructor(definition) {\n super(definition);\n this.addValidations();\n }\n\n addValidations() {\n this.validations.type = function(value) {\n if (!Array.isArray(value)) {\n throw new Error(`MCF_Array creation: the type of ${value} isn't \"array\"`);\n }\n };\n\n for (let param in this.definition) {\n switch (param) {\n case 'maxLength':\n this.validations.maxLength = function(value, definition) {\n if (value.length > definition.maxLength) {\n throw new Error(`MCF_Array creation: the length of the array \"${value}\" is higher than the maximum defined: ${definition.maxLength}`);\n }\n };\n break;\n case 'minLength':\n this.validations.minLength = function(value, definition) {\n if (value.length < definition.minLength) {\n throw new Error(`MCF_Array creation: the length of the array \"${value}\" is lower than the minimum defined: ${definition.minLength}`);\n }\n };\n break;\n }\n }\n }\n}\nclass MCF_Enum extends MCF_Type {\n constructor(definition) {\n super(definition);\n this.addValidations();\n }\n\n addValidations() {\n this.validations.isAvailable = function(value, definition) {\n const valueIsAvailable = definition.availableValues.reduce(function(isAvailable, availableValue) {\n if (value === availableValue) return true;\n return isAvailable;\n }, false);\n if (!valueIsAvailable) {\n throw new Error(`MCF_Enum creation: the value \"${value}\" isn't one of the next values, included in the field definition: ${definition.availableValues.toString()}`);\n }\n }\n }\n}\nclass MCF_TypeFactory {\n static buildDefinedObject(definedObject) {\n const objectDefinition = definedObject.getDefinition();\n let fields = objectDefinition.fields;\n let dictionary = {};\n for (let field in fields) {\n switch (fields[field]['TYPE']) {\n case 'string':\n dictionary[field] = new MCF_String(fields[field]);\n break;\n case 'number':\n dictionary[field] = new MCF_Number(fields[field]);\n break;\n case 'boolean':\n dictionary[field] = new MCF_Boolean(fields[field]);\n break;\n case 'enum':\n dictionary[field] = new MCF_Enum(fields[field]);\n break;\n case 'array':\n dictionary[field] = new MCF_Array(fields[field]);\n break;\n }\n }\n return dictionary;\n }\n}\nclass MCF_DefinedObject {\n constructor(definition) {\n this.definition = definition;\n this.validations = {};\n this.fields = MCF_TypeFactory.buildDefinedObject(this);\n this.validateValue(definition.value)\n this.setValue(definition.value)\n }\n\n getDefinition() {\n return this.definition;\n }\n\n getValue() {\n let value = {};\n for (let field in this.fields) {\n value[field] = this.fields[field].value;\n }\n return value;\n }\n\n validateValue(value){\n for (let field in this.definition.fields) {\n if (value[field] === undefined || value === null) {\n if (this.definition.fields[field].required === true) {\n throw new Error(`The field ${field} is required`);\n }\n if (this.definition.fields[field].defaultValue !== undefined) {\n value[field] = this.definition.fields[field].defaultValue;\n }\n if (this.definition.fields[field].null === false) {\n throw new Error(`The field ${field} can't be undefined`);\n }\n }\n }\n }\n\n setValue(value) {\n for (let valueKey in value) {\n if (value[valueKey] === undefined) {\n this.fields[valueKey].value = undefined;\n } else {\n this.fields[valueKey].setValue(value[valueKey]);\n }\n }\n }\n\n setPropertiesValue(value) {\n for (let field in value) {\n this.fields[field].setValue(value[field]);\n }\n }\n}\n\nconst fileDefinition = {\n filename: { TYPE: 'string' },\n extension: { TYPE: 'string' },\n mimeType: { TYPE: 'string' },\n path: { TYPE: 'string' },\n content: { TYPE: 'string' },\n}\n\nclass MCF_File extends MCF_DefinedObject {\n constructor(params) {\n const fields = (params.fields) ? params.fields : {}\n const value = (params.value) ? params.value : params\n super({\n fields: {...fileDefinition, ...fields},\n value: value,\n })\n }\n}\n\nexport {\n MCF_DefinedObject as definedObject,\n MCF_File as mcfFile,\n};\n"],"sourceRoot":""} \ No newline at end of file diff --git a/src/index.js b/src/index.js index e0b0488..7879df7 100644 --- a/src/index.js +++ b/src/index.js @@ -2,52 +2,52 @@ // ======================================== class MCF_Type { - constructor(definition){ + constructor(definition) { this.definition = definition; this.validations = {}; } - setValue(value){ + setValue(value) { this.validate(value); this.value = value; } - getValue(){ + getValue() { return this.value; } - validate(value){ - for (let validation in this.validations){ + validate(value) { + for (let validation in this.validations) { this.validations[validation](value, this.definition); } } } class MCF_String extends MCF_Type { - constructor(definition){ + constructor(definition) { super(definition); this.addValidations(); } - addValidations(){ + addValidations() { this.validations.type = function(value) { - if (typeof (value) !== "string") { + if (typeof(value) !== "string") { throw new Error(`MCF_String creation: the type of ${value} isn't "string"`); } }; - for (let param in this.definition){ + for (let param in this.definition) { switch (param) { case 'maxLength': - this.validations.maxLength = function(value, definition){ - if (value.length > definition.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){ + 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}`); } }; @@ -56,31 +56,31 @@ class MCF_String extends MCF_Type { } } } -class MCF_Number extends MCF_Type{ - constructor(definition){ +class MCF_Number extends MCF_Type { + constructor(definition) { super(definition); this.addValidations(); } - addValidations(){ + addValidations() { this.validations.type = function(value) { - if (typeof (value) !== "number") { + 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){ + for (let param in this.definition) { switch (param) { case 'max': - this.validations.max = function(value, definition){ - if (value > definition.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){ + 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}`); } }; @@ -89,45 +89,45 @@ class MCF_Number extends MCF_Type{ } } } -class MCF_Boolean extends MCF_Type{ - constructor(definition){ +class MCF_Boolean extends MCF_Type { + constructor(definition) { super(definition); this.addValidations(); } - addValidations(){ + addValidations() { this.validations.type = function(value) { - if (typeof (value) !== "boolean") { + 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){ + constructor(definition) { super(definition); this.addValidations(); } - 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){ + for (let param in this.definition) { switch (param) { case 'maxLength': - this.validations.maxLength = function(value, definition){ - if (value.length > definition.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){ + 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}`); } }; @@ -136,19 +136,19 @@ class MCF_Array extends MCF_Type { } } } -class MCF_Enum extends MCF_Type{ - constructor(definition){ +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){ + 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){ + 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()}`); } } @@ -182,73 +182,79 @@ class MCF_TypeFactory { } } class MCF_DefinedObject { - constructor(definition){ + constructor(definition) { this.definition = definition; this.validations = {}; this.fields = MCF_TypeFactory.buildDefinedObject(this); + this.validateValue(definition.value) this.setValue(definition.value) } - getDefinition(){ + getDefinition() { return this.definition; } - getValue(){ + getValue() { let value = {}; - for (let field in this.fields){ + for (let field in this.fields) { value[field] = this.fields[field].value; } return value; } - setValue(value){ - for (let field in this.definition.fields){ - if (value[field] === undefined || value === null){ - if (this.definition.fields[field].required === true){ + 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){ + if (this.definition.fields[field].defaultValue !== undefined) { value[field] = this.definition.fields[field].defaultValue; } - if (this.definition.fields[field].null === false){ + if (this.definition.fields[field].null === false) { throw new Error(`The field ${field} can't be undefined`); } } } + } - for (let field in value){ - if (value[field] === undefined){ - this.fields[field].value = undefined; + setValue(value) { + for (let valueKey in value) { + if (value[valueKey] === undefined) { + this.fields[valueKey].value = undefined; } else { - this.fields[field].setValue(value[field]); + this.fields[valueKey].setValue(value[valueKey]); } } } - setPropertiesValue(value){ - for (let field in value){ + setPropertiesValue(value) { + for (let field in value) { this.fields[field].setValue(value[field]); } } } const fileDefinition = { - filename: {TYPE: 'string', required: true}, - extension: {TYPE: 'string', required: true}, - path: {TYPE: 'string', required: true}, - content: {TYPE: 'string'}, + filename: { TYPE: 'string' }, + extension: { TYPE: 'string' }, + mimeType: { TYPE: 'string' }, + path: { TYPE: 'string' }, + content: { TYPE: 'string' }, } class MCF_File extends MCF_DefinedObject { - constructor(params) { - super({ - fields: fileDefinition, - value: params, - }) - } + constructor(params) { + const fields = (params.fields) ? params.fields : {} + const value = (params.value) ? params.value : params + super({ + fields: {...fileDefinition, ...fields}, + value: value, + }) + } } export { - MCF_DefinedObject as definedObject, - MCF_File as mcfFile, + MCF_DefinedObject as definedObject, + MCF_File as mcfFile, };