Add minor fixes
This commit is contained in:
parent
e4ab9caf94
commit
19554d6848
4 changed files with 76 additions and 70 deletions
|
@ -49,4 +49,4 @@ module.exports = {
|
||||||
'ignoreReadBeforeAssign': true,
|
'ignoreReadBeforeAssign': true,
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
};
|
}
|
||||||
|
|
2
dist/mcf-objects.js
vendored
2
dist/mcf-objects.js
vendored
File diff suppressed because one or more lines are too long
2
dist/mcf-objects.js.map
vendored
2
dist/mcf-objects.js.map
vendored
File diff suppressed because one or more lines are too long
140
src/index.js
140
src/index.js
|
@ -2,52 +2,52 @@
|
||||||
// ========================================
|
// ========================================
|
||||||
|
|
||||||
class MCF_Type {
|
class MCF_Type {
|
||||||
constructor(definition){
|
constructor(definition) {
|
||||||
this.definition = definition;
|
this.definition = definition;
|
||||||
this.validations = {};
|
this.validations = {};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setValue(value){
|
setValue(value) {
|
||||||
this.validate(value);
|
this.validate(value);
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
getValue(){
|
getValue() {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
validate(value){
|
validate(value) {
|
||||||
for (let validation in this.validations){
|
for (let validation in this.validations) {
|
||||||
this.validations[validation](value, this.definition);
|
this.validations[validation](value, this.definition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class MCF_String extends MCF_Type {
|
class MCF_String extends MCF_Type {
|
||||||
constructor(definition){
|
constructor(definition) {
|
||||||
super(definition);
|
super(definition);
|
||||||
this.addValidations();
|
this.addValidations();
|
||||||
}
|
}
|
||||||
|
|
||||||
addValidations(){
|
addValidations() {
|
||||||
this.validations.type = function(value) {
|
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"`);
|
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) {
|
switch (param) {
|
||||||
case 'maxLength':
|
case 'maxLength':
|
||||||
this.validations.maxLength = function(value, definition){
|
this.validations.maxLength = function(value, definition) {
|
||||||
if (value.length > definition.maxLength){
|
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}`);
|
throw new Error(`MCF_String creation: the length of the string "${value}" is higher than the maximum defined: ${definition.maxLength}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case 'minLength':
|
case 'minLength':
|
||||||
this.validations.minLength = function(value, definition){
|
this.validations.minLength = function(value, definition) {
|
||||||
if (value.length < definition.minLength){
|
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}`);
|
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{
|
class MCF_Number extends MCF_Type {
|
||||||
constructor(definition){
|
constructor(definition) {
|
||||||
super(definition);
|
super(definition);
|
||||||
this.addValidations();
|
this.addValidations();
|
||||||
}
|
}
|
||||||
|
|
||||||
addValidations(){
|
addValidations() {
|
||||||
this.validations.type = function(value) {
|
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"`);
|
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) {
|
switch (param) {
|
||||||
case 'max':
|
case 'max':
|
||||||
this.validations.max = function(value, definition){
|
this.validations.max = function(value, definition) {
|
||||||
if (value > definition.max){
|
if (value > definition.max) {
|
||||||
throw new Error(`MCF_Number creation: the value ${value} is higher than the maximum defined: ${definition.max}`);
|
throw new Error(`MCF_Number creation: the value ${value} is higher than the maximum defined: ${definition.max}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case 'min':
|
case 'min':
|
||||||
this.validations.min = function(value, definition){
|
this.validations.min = function(value, definition) {
|
||||||
if (value < definition.min){
|
if (value < definition.min) {
|
||||||
throw new Error(`MCF_Number creation: the value ${value} is lower than the minimum defined: ${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{
|
class MCF_Boolean extends MCF_Type {
|
||||||
constructor(definition){
|
constructor(definition) {
|
||||||
super(definition);
|
super(definition);
|
||||||
this.addValidations();
|
this.addValidations();
|
||||||
}
|
}
|
||||||
|
|
||||||
addValidations(){
|
addValidations() {
|
||||||
this.validations.type = function(value) {
|
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"`);
|
throw new Error(`MCF_Boolean creation: the type of the value ${value} isn't "boolean"`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class MCF_Array extends MCF_Type {
|
class MCF_Array extends MCF_Type {
|
||||||
constructor(definition){
|
constructor(definition) {
|
||||||
super(definition);
|
super(definition);
|
||||||
this.addValidations();
|
this.addValidations();
|
||||||
}
|
}
|
||||||
|
|
||||||
addValidations(){
|
addValidations() {
|
||||||
this.validations.type = function(value) {
|
this.validations.type = function(value) {
|
||||||
if (!Array.isArray(value)) {
|
if (!Array.isArray(value)) {
|
||||||
throw new Error(`MCF_Array creation: the type of ${value} isn't "array"`);
|
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) {
|
switch (param) {
|
||||||
case 'maxLength':
|
case 'maxLength':
|
||||||
this.validations.maxLength = function(value, definition){
|
this.validations.maxLength = function(value, definition) {
|
||||||
if (value.length > definition.maxLength){
|
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}`);
|
throw new Error(`MCF_Array creation: the length of the array "${value}" is higher than the maximum defined: ${definition.maxLength}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case 'minLength':
|
case 'minLength':
|
||||||
this.validations.minLength = function(value, definition){
|
this.validations.minLength = function(value, definition) {
|
||||||
if (value.length < definition.minLength){
|
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}`);
|
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{
|
class MCF_Enum extends MCF_Type {
|
||||||
constructor(definition){
|
constructor(definition) {
|
||||||
super(definition);
|
super(definition);
|
||||||
this.addValidations();
|
this.addValidations();
|
||||||
}
|
}
|
||||||
|
|
||||||
addValidations(){
|
addValidations() {
|
||||||
this.validations.isAvailable = function(value, definition){
|
this.validations.isAvailable = function(value, definition) {
|
||||||
const valueIsAvailable = definition.availableValues.reduce(function(isAvailable, availableValue){
|
const valueIsAvailable = definition.availableValues.reduce(function(isAvailable, availableValue) {
|
||||||
if (value === availableValue) return true;
|
if (value === availableValue) return true;
|
||||||
return isAvailable;
|
return isAvailable;
|
||||||
}, false);
|
}, 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()}`);
|
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 {
|
class MCF_DefinedObject {
|
||||||
constructor(definition){
|
constructor(definition) {
|
||||||
this.definition = definition;
|
this.definition = definition;
|
||||||
this.validations = {};
|
this.validations = {};
|
||||||
this.fields = MCF_TypeFactory.buildDefinedObject(this);
|
this.fields = MCF_TypeFactory.buildDefinedObject(this);
|
||||||
|
this.validateValue(definition.value)
|
||||||
this.setValue(definition.value)
|
this.setValue(definition.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
getDefinition(){
|
getDefinition() {
|
||||||
return this.definition;
|
return this.definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
getValue(){
|
getValue() {
|
||||||
let value = {};
|
let value = {};
|
||||||
for (let field in this.fields){
|
for (let field in this.fields) {
|
||||||
value[field] = this.fields[field].value;
|
value[field] = this.fields[field].value;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
setValue(value){
|
validateValue(value){
|
||||||
for (let field in this.definition.fields){
|
for (let field in this.definition.fields) {
|
||||||
if (value[field] === undefined || value === null){
|
if (value[field] === undefined || value === null) {
|
||||||
if (this.definition.fields[field].required === true){
|
if (this.definition.fields[field].required === true) {
|
||||||
throw new Error(`The field ${field} is required`);
|
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;
|
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`);
|
throw new Error(`The field ${field} can't be undefined`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (let field in value){
|
setValue(value) {
|
||||||
if (value[field] === undefined){
|
for (let valueKey in value) {
|
||||||
this.fields[field].value = undefined;
|
if (value[valueKey] === undefined) {
|
||||||
|
this.fields[valueKey].value = undefined;
|
||||||
} else {
|
} else {
|
||||||
this.fields[field].setValue(value[field]);
|
this.fields[valueKey].setValue(value[valueKey]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setPropertiesValue(value){
|
setPropertiesValue(value) {
|
||||||
for (let field in value){
|
for (let field in value) {
|
||||||
this.fields[field].setValue(value[field]);
|
this.fields[field].setValue(value[field]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileDefinition = {
|
const fileDefinition = {
|
||||||
filename: {TYPE: 'string', required: true},
|
filename: { TYPE: 'string' },
|
||||||
extension: {TYPE: 'string', required: true},
|
extension: { TYPE: 'string' },
|
||||||
path: {TYPE: 'string', required: true},
|
mimeType: { TYPE: 'string' },
|
||||||
content: {TYPE: 'string'},
|
path: { TYPE: 'string' },
|
||||||
|
content: { TYPE: 'string' },
|
||||||
}
|
}
|
||||||
|
|
||||||
class MCF_File extends MCF_DefinedObject {
|
class MCF_File extends MCF_DefinedObject {
|
||||||
constructor(params) {
|
constructor(params) {
|
||||||
super({
|
const fields = (params.fields) ? params.fields : {}
|
||||||
fields: fileDefinition,
|
const value = (params.value) ? params.value : params
|
||||||
value: params,
|
super({
|
||||||
})
|
fields: {...fileDefinition, ...fields},
|
||||||
}
|
value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
MCF_DefinedObject as definedObject,
|
MCF_DefinedObject as definedObject,
|
||||||
MCF_File as mcfFile,
|
MCF_File as mcfFile,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue