1
0
forked from apxtri/apxtri

fix cud in case of empty value, it removes it

This commit is contained in:
philc 2024-09-24 14:15:22 +02:00
parent 84200b70b0
commit 7b5df014be
2 changed files with 62 additions and 49 deletions

View File

@ -12,7 +12,8 @@ Checkjson.schema.properties = {};
Checkjson.schema.properties.type = {};
Checkjson.schema.properties.type.string = (str) => typeof str === "string";
Checkjson.schema.properties.type.array = (val) => Array.isArray(val);
Checkjson.schema.properties.type.object = (val) => typeof val === 'object' && val !== null && !Array.isArray(val);
Checkjson.schema.properties.type.object = (val) =>
typeof val === "object" && val !== null && !Array.isArray(val);
Checkjson.schema.properties.type.number = (n) => typeof n === "number";
Checkjson.schema.properties.type.boolean = (n) => typeof n === "boolean";
Checkjson.schema.properties.type.integer = (n) =>
@ -61,9 +62,7 @@ Checkjson.schema.properties.enum = (str, enumvalues) => {
} else if (tribeId) {
//enumvalues is a reference of objectname.key
const { tribeId, obj, keyid } = enumvalues.split(".");
return fs.existsSync(
`../../../${tribeId}/schema/${obj}/itm/${keyid}.json`
);
return fs.existsSync(`../../../${tribeId}/schema/${obj}/itm/${keyid}.json`);
} else {
return true;
}
@ -77,10 +76,14 @@ Checkjson.schema.properties.enum = (str, enumvalues) => {
* @return null if format does not exist, true or false
*/
Checkjson.testformat = (str, format) => {
if (!Checkjson.schema.properties.format[format]) { return null}
return Checkjson.schema.properties.pattern(str, Checkjson.schema.properties.format[format])
if (!Checkjson.schema.properties.format[format]) {
return null;
}
return Checkjson.schema.properties.pattern(
str,
Checkjson.schema.properties.format[format]
);
};
// see format https://json-schema.org/understanding-json-schema/reference/string.html#format
// to check a just value with a format use Checkjson.testformat=(value, format)
Checkjson.schema.properties.format = {
@ -132,10 +135,8 @@ Checkjson.schema.validation = (schema) => {
data: { propertie: p, type: properties[p].type },
});
}
if (
properties[p].type &&
typeof properties[p].type === "object"){
if (properties[p]['$ref']){
if (properties[p].type && typeof properties[p].type === "object") {
if (properties[p]["$ref"]) {
//This is manage by Odmdb.schema to load recursively complex schema
multimsg.push({
ref: "Checkjson",
@ -145,9 +146,9 @@ Checkjson.schema.validation = (schema) => {
}
//case type=="object" with properties
if (properties[p].properties) {
const checksub = Checkjson.schema.validation(properties[p])
const checksub = Checkjson.schema.validation(properties[p]);
if (checksub.status != 200) {
multimsg = multimsg.concat(checksub.multimsg)
multimsg = multimsg.concat(checksub.multimsg);
}
}
// if not $ref or no properties then any object is accepted
@ -205,21 +206,23 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
// subdata={prop1,prop2}
// Return [] => no error, else 1 item per error {msg,ref:checkjson,data}
const propertielist = Object.keys(properties);
Object.keys(subdata).forEach(kdata=>{
Object.keys(subdata).forEach((kdata) => {
if (!propertielist.includes(kdata)) {
delete subdata[kdata];
}
})
let multimsg=[]
});
let multimsg = [];
propertielist.forEach((p) => {
//type is mandatory in a propertie
if (subdata[p]) {
if (properties[p].properties) {
//means it is a subobject
multimsg=multimsg.concat(propertiescheck(properties[p].properties,subdata[p]))
multimsg = multimsg.concat(
propertiescheck(properties[p].properties, subdata[p])
);
}
//type can be a list of string; number, array, boolean, object, null
console.log(p,properties[p].type )
//console.log(p,properties[p].type )
const typlist =
properties[p].type && typeof properties[p].type === "string"
? [properties[p].type]
@ -332,7 +335,10 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
}
if (
properties[p].pattern &&
!Checkjson.schema.properties.pattern(subdata[p], properties[p].pattern)
!Checkjson.schema.properties.pattern(
subdata[p],
properties[p].pattern
)
) {
multimsg.push({
ref: "Checkjson",
@ -348,14 +354,14 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
});
}
});
return multimsg
return multimsg;
}; //end propertiescheck()
if (withschemacheck) {
const validschema = Checkjson.schema.validation(schema);
if (validschema.status != 200) return validschema;
}
let multi=propertiescheck(schema.properties,data)
let multi = propertiescheck(schema.properties, data);
const res = {};
if (multi.length > 0) {

View File

@ -663,16 +663,23 @@ Odmdb.cud = (objectPathname, crud, itm, role, runindex = true) => {
if (keynotallow.length > 0) {
feedbackinfo.keynotallow = keynotallow;
}
//console.log('itmstore',itmtostore)
//console.log("itm",itm)
//console.log(accessright)
accessright.U.forEach(async (p) => {
itmtostore[p] = itm[p];
});
itmtostore.dt_update = dayjs().toISOString();
}
Object.keys(itmtostore).forEach((k) => {
// remove undefined itmtostore because empty =>
if (!itmtostore[k]) delete itmtostore[k];
});
Object.keys(itmtostore).forEach((k) => {
//Manage base64 case image to convert ans store in webp
if (k.includes("imgbase64_") && itmtostore[k] != "") {
fs.ensureDirSync(`${objectPathname}/img/`);
console.log("check this for k:",k," itmtostore[k]:",itmtostore[k])
//console.log("check this for k:", k, " itmtostore[k]:", itmtostore[k]);
const imgb64 = itmtostore[k].replace(
/^data:image\/(png|png|gif|bmp|jpg|jpeg);base64,/,
""