1
0
mirror of https://github.com/dap-ps/discover.git synced 2025-02-08 07:25:07 +00:00
discover/back-end/inputs/template-parser.js

28 lines
823 B
JavaScript
Raw Normal View History

2019-06-03 21:01:42 +03:00
const TemplateValidationError = require('./template-validation-error');
class TemplatesParser {
static parse(data, template) {
let filteredInput = {};
Object.keys(template).forEach((property) => {
if (template[property].constructor.name === 'Object') {
TemplatesParser.parse(data[property], template[property]);
}
// Checks if the filter is required according to template
if (template[property] && !data[property] && data[property] != false) {
throw new TemplateValidationError(`${property} field is required`);
}
if (data[property]) {
filteredInput[property] = data[property];
}
});
return filteredInput;
}
}
module.exports = TemplatesParser;