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;
}
@ -71,23 +70,27 @@ Checkjson.schema.properties.enum = (str, enumvalues) => {
// to check a value for a pattern
// Checkjson.schema.properties.pattern(value, properties[p].pattern)
/**
*
* @param {string} str to test
*
* @param {string} str to test
* @param {string} format keyworkd existing in Checkjson.schema.properties.format
* @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])
}
Checkjson.testformat = (str, 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 = {
"date-time": /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d{1,3}/,
stringalphaonly: /^[A-Za-z0-9]{3,}$/,
time: /[0-2]\d:[0-5]\d:[0-5]\d\.\d{1,3}/,
timehhmm:/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/,
timehhmm: /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/,
date: /\d{4}-[01]\d-[0-3]\d/,
duration: / /,
email:
@ -103,7 +106,7 @@ Checkjson.schema.properties.format = {
ipv4: /^([09]{1,3}.){3}.([09]{1,3})$/,
ipv6: /^((([09A-Fa-f]{1,4}:){7}[09A-Fa-f]{1,4})|(([09A-Fa-f]{1,4}:){6}:[09A-Fa-f]{1,4})|(([09A-Fa-f]{1,4}:){5}:([09A-Fa-f]{1,4}:)?[09A-Fa-f]{1,4})|(([09A-Fa-f]{1,4}:){4}:([09A-Fa-f]{1,4}:){0,2}[09A-Fa-f]{1,4})|(([09A-Fa-f]{1,4}:){3}:([09A-Fa-f]{1,4}:){0,3}[09A-Fa-f]{1,4})|(([09A-Fa-f]{1,4}:){2}:([09A-Fa-f]{1,4}:){0,4}[09A-Fa-f]{1,4})|(([09A-Fa-f]{1,4}:){6}((b((25[05])|(1d{2})|(2[04]d)|(d{1,2}))b).){3}(b((25[05])|(1d{2})|(2[04]d)|(d{1,2}))b))|(([09A-Fa-f]{1,4}:){0,5}:((b((25[05])|(1d{2})|(2[04]d)|(d{1,2}))b).){3}(b((25[05])|(1d{2})|(2[04]d)|(d{1,2}))b))|(::([09A-Fa-f]{1,4}:){0,5}((b((25[05])|(1d{2})|(2[04]d)|(d{1,2}))b).){3}(b((25[05])|(1d{2})|(2[04]d)|(d{1,2}))b))|([09A-Fa-f]{1,4}::([09A-Fa-f]{1,4}:){0,5}[09A-Fa-f]{1,4})|(::([09A-Fa-f]{1,4}:){0,6}[09A-Fa-f]{1,4})|(([09A-Fa-f]{1,4}:){1,7}:))$/,
telephonefr: /^0[1-9][0-9]{8}$/,
imgbase64:/data:image\/(png|jpg|jpeg|svg);base64,(?:[A-Za-z0-9+\/]+)/,
imgbase64: /data:image\/(png|jpg|jpeg|svg);base64,(?:[A-Za-z0-9+\/]+)/,
telephoneinter: /^\+*(\d{3})*[0-9,\-]{8,}/,
password:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&.])[A-Za-z\d$@$!%*?&.{}:|\s]{8,}/,
@ -132,27 +135,25 @@ Checkjson.schema.validation = (schema) => {
data: { propertie: p, type: properties[p].type },
});
}
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",
msg: "externalrefnotload",
data: { propertie: p, ref: properties[p]["$ref"]},
});
}
//case type=="object" with properties
if (properties[p].properties){
const checksub = Checkjson.schema.validation(properties[p])
if (checksub.status!=200){
multimsg = multimsg.concat(checksub.multimsg)
}
}
// if not $ref or no properties then any object is accepted
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",
msg: "externalrefnotload",
data: { propertie: p, ref: properties[p]["$ref"] },
});
}
//case type=="object" with properties
if (properties[p].properties) {
const checksub = Checkjson.schema.validation(properties[p]);
if (checksub.status != 200) {
multimsg = multimsg.concat(checksub.multimsg);
}
}
// if not $ref or no properties then any object is accepted
}
if (
properties[p].format &&
!Checkjson.schema.properties.format[properties[p].format]
@ -200,26 +201,28 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
console.log('---------')
console.log(data)
*/
const propertiescheck=(properties,subdata)=>{
const propertiescheck = (properties, subdata) => {
// properties ={prop1:{type,format},prop2:{type:object,...}}
// 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=>{
if (!propertielist.includes(kdata)){
const propertielist = Object.keys(properties);
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){
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 )
//type can be a list of string; number, array, boolean, object, null
//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
};//end propertiescheck()
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) {
@ -367,7 +373,7 @@ Checkjson.schema.data = (schema, data, withschemacheck) => {
res.msg = "validcheck";
}
if (schema.apxid) {
res.data={apxid : data[schema.apxid],itm:data};
res.data = { apxid: data[schema.apxid], itm: data };
}
return res;
};

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,/,
""