feat: refs #8387 crudModel #3355

Open
robert wants to merge 11 commits from 8387-itemTagCrudModel into dev
4 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE `vn`.`tag`
ADD COLUMN IF NOT EXISTS `validationRegex` varchar(50) DEFAULT NULL;

View File

@ -10,4 +10,27 @@ module.exports = Self => {
return new UserError(`Tag value cannot be blank`); return new UserError(`Tag value cannot be blank`);
return err; return err;
}); });
Self.observe('before save', async ctx => {
let tagFk;
let value;
if (ctx.isNewInstance) {
tagFk = ctx.instance.tagFk;
value = ctx.instance.value;
} else {
tagFk = ctx.currentInstance.tagFk;
value = ctx.data.value;
}
const models = Self.app.models;
const validTag = await models.Tag.findOne({where: {id: tagFk}});
if (validTag.validationRegex) {
const regexString = validTag.validationRegex.replace(/\\\\/g, '\\');
const validExpresion = new RegExp(regexString);
if (value && !validExpresion.test(value))
throw new UserError('The value must be a number or a range of numbers');
}
});
}; };

View File

@ -30,6 +30,9 @@
"mysql": { "mysql": {
"columnName": "isQuantitatif" "columnName": "isQuantitatif"
} }
},
"validationRegex": {
"type": "string"
} }
}, },
"acls": [ "acls": [