Last active
August 16, 2023 13:37
-
-
Save tcha-tcho/e0bd1f984b9209b8c84a1f0eb0e26fbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const UserScheme = { | |
uid: { | |
type: "string" | |
,match: /[0-9].*/ | |
,required: true | |
,unique: true | |
,validate: "uid" | |
,trim: true | |
,max: 25 | |
} | |
,imei: { | |
type: "string" | |
,tab: "Instalado" | |
,group: "Principal" | |
,match: /[0-9].*/ | |
,required: true | |
,trim: true | |
,max: 50 | |
} | |
,date: { | |
type: "Date" | |
,validate: (data) => { | |
return (date instanceof Date && date < new Date()); | |
} | |
,tab: "Instalado" | |
,group: "Principal" | |
,line: 0 | |
} | |
,name: { | |
type: "string" | |
,tab: "Instalado" | |
,group: "Principal" | |
,line: 1 | |
} | |
,desc: { | |
type: "string" | |
,tab: "Instalado" | |
,group: "Principal" | |
,line: 1 | |
} | |
,deviceGroup: { | |
type: "string" | |
,options: ["a", "b", "c"] | |
,default: "a" | |
,tab: "Instalado" | |
,group: "Principal" | |
,line: 1 | |
} | |
}; | |
// Antes de salvar: | |
// nota que eu nao estou usando UserScheme, só Scheme. Porque aqui ele já está | |
// lidando com isso de forma genérica... | |
let invalid; | |
Object.keys(data).forEach(prop => { | |
if (Scheme[prop] && Scheme[prop].validate | |
&& UserScheme[prop].validate(data[prop])) { | |
invalid = true; | |
}; | |
if (Scheme[prop] && Scheme[prop].match | |
&& !data[prop].match(Scheme[prop].match)) { | |
invalid = true; | |
}; | |
//.... faz as outras validaçoes | |
}) | |
if (!invalid) // ... salva no banco. | |
// Na hora de renderizar ele usa as indiçoes no design | |
let tabs = {"default": {"default"/*group*/:{1:[]}/*line*/} }; | |
Object.keys(Scheme).forEach(prop => { | |
let item = Scheme[prop]; | |
if (!item.tab) item.tab = "default"; | |
if (!item.group) item.group = "default"; | |
if (!item.line) item.line = 0; // todo mundo junto | |
if (!tabs[item.tab]) tabs[item.tab] = {}; | |
if (!tabs[item.tab][item.group]) tabs[item.tab][item.group] = {}; | |
if (!tabs[item.tab][item.group][item.line]) { | |
tabs[item.tab][item.group][item.line] = []; | |
} | |
tabs[item.tab][item.group][item.line].push(item); | |
}); | |
// usamos o tabs para organizar os forms... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment