Skip to content

Commit

Permalink
Merge pull request #9 from dionmaicon/verification_types_to_models
Browse files Browse the repository at this point in the history
Add types verification
  • Loading branch information
dionmaicon committed Dec 12, 2019
2 parents 41a4712 + b1eeebb commit 68910e4
Show file tree
Hide file tree
Showing 3 changed files with 375 additions and 11 deletions.
17 changes: 6 additions & 11 deletions js/bootstrap/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const Form = class {
getTemplate() {
let capitalizedName = capitalize(this.modelName);

let template =
`<template>
let template = `<template>
<div id="${this.modelName}Form" class="form">
<form @submit.prevent="handleSubmit">
templateStructString
Expand All @@ -27,10 +26,9 @@ const Form = class {
let countFiles = 0;
//Template Struct
for (let property in this.model) {
if (property.includes("hidden_fields")) continue; //if property includes 'hide'in your text this loop skip once
if (property.includes("hidden_fields")) continue;

if (this.model.hasOwnProperty(property)) {
// types: number, text, select, currency, radio, checkbox, oneToOne, object, html
templateStruct += `\t<div class="form-group">\n\t\t<label for="${property}">${property}</label>\n`;

if (this.model[property].type == "select") {
Expand All @@ -46,7 +44,7 @@ const Form = class {
} else if (this.model[property].type == "textarea") {
templateStruct += `\t<textarea id="${property}" style="width: 100%" v-model="${this.modelName}.${property}" rows="10">You text here...</textarea>\n\n`;
} else if (this.model[property].type == "radio") {
templateStruct += "<br />";
templateStruct += "<br />";
for (let option of this.model[property].options) {
templateStruct += `\t<input type="radio" id="${option.id}" value="${option.value}" v-model="${this.modelName}.${property}">\n`;
templateStruct += `\t<label for="${option.id}">${option.value}</label><br>\n\n`;
Expand Down Expand Up @@ -106,9 +104,6 @@ const Form = class {
class="form-control"
v-model.lazy="${this.modelName}.${property}"
/>\n`;
} else if (this.model[property].type == "object") {
templateStruct += `\t<input id="${property}" class="form-control" v-model="${this.modelName}.${property}.${this.model[property].attribute}">
\n`;
} else {
templateStruct += `\t<input id="${property}" class="form-control" `;
for (let htmlProp in this.model[property]) {
Expand Down Expand Up @@ -266,8 +261,6 @@ const Form = class {
this.model[property].options
)},\n`;
dataScript += `${property}: '',\n`;
} else if (this.model[property].type == "object") {
dataScript += `${property}: {},\n`;
} else if (this.model[property].type == "checkbox") {
dataScript += `${property}: [],\n`;
} else if (this.model[property].type == "oneToOne") {
Expand Down Expand Up @@ -300,7 +293,9 @@ const Form = class {
this.model[property].type == "oneToMany"
) {
let capitalizedRelationName = capitalize(this.model[property].model);
let pluralizedAndCapitalizedRelationName = pluralize(capitalizedRelationName);
let pluralizedAndCapitalizedRelationName = pluralize(
capitalizedRelationName
);
relationsImport += `
import { getAll${pluralizedAndCapitalizedRelationName} } from "@/services/${this.model[property].model}";
`;
Expand Down
147 changes: 147 additions & 0 deletions js/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const ONE_TO_ONE = "oneToOne";
const ONE_TO_MANY = "oneToMany";
const TEXT = "text";
const SELECT = "select";
const TEXTAREA = "textarea";
const NUMBER = "number";
const CURRENCY = "currency";
const HTML = "html";
const RADIO = "radio";
const CHECKBOX = "checkbox";
const FILE = "file";

const HTML5_TYPES = [
"color",
"date",
"datetime-local",
"email",
"hidden",
"image",
"month",
"password",
"range",
"reset",
"search",
"submit",
"tel",
"time",
"url",
"week"
];

const Types = class {
constructor() {}

isValid() {}

static get ONE_TO_ONE() {
return ONE_TO_ONE;
}

static get ONE_TO_MANY() {
return ONE_TO_MANY;
}

static get TEXT() {
return TEXT;
}

static get SELECT() {
return SELECT;
}

static get TEXTAREA() {
return TEXTAREA;
}

static get NUMBER() {
return NUMBER;
}

static get CURRENCY() {
return CURRENCY;
}

static get HTML() {
return HTML;
}

static get RADIO() {
return RADIO;
}

static get CHECKBOX() {
return CHECKBOX;
}

static get FILE() {
return FILE;
}

static get validTypes() {
return HTML5_TYPES.concat([
ONE_TO_ONE,
ONE_TO_MANY,
TEXT,
SELECT,
TEXTAREA,
NUMBER,
CURRENCY,
HTML,
RADIO,
CHECKBOX,
FILE
]);
}

static isValid(value) {
let types = this.validTypes;
return types.find(type => type == value);
}

static modelIsValid(model) {
let response = {
message: "",
success: true
};

for (let prop in model) {
if (!model.hasOwnProperty(prop)) continue;
let object = model[prop];
switch (model[prop].type) {
case Types.ONE_TO_ONE:
case Types.ONE_TO_MANY:
if (!object.attribute) {
response.message += `Model type: ${prop} needs attribute property.\n`;
response.success = false;
}
if (!object.model) {
response.message += `Model type: ${prop} needs model property.\n`;
response.success = false;
}
break;
case Types.RADIO:
case Types.CHECKBOX:
case Types.SELECT:
if (!object.options) {
response.message += `Model type: ${prop} needs options property.\n`;
response.success = false;
}
break;
}
if (!this.isValid(model[prop].type)) {
response.message += `Model type: ${prop} is invalid.\n`;
response.success = false;
}
}

if (!response.success) {
return response;
} else {
response.message = "Model is valid.";
return response;
}
}
};

module.exports = Types;

0 comments on commit 68910e4

Please sign in to comment.