Some code formatting
This commit is contained in:
parent
f2b7d4c8df
commit
0b0929aa7b
1 changed files with 17 additions and 9 deletions
|
@ -357,44 +357,52 @@ function filterKeysAndValues(keysIn, valuesIn, method = 'include', prefixes = []
|
||||||
}
|
}
|
||||||
|
|
||||||
function testForMjmlErrors(keys, values) {
|
function testForMjmlErrors(keys, values) {
|
||||||
|
|
||||||
|
let errors = [];
|
||||||
let testLayout = '<mjml><mj-body><mj-container>{{{body}}}</mj-container></mj-body></mjml>';
|
let testLayout = '<mjml><mj-body><mj-container>{{{body}}}</mj-container></mj-body></mjml>';
|
||||||
let isInvalidMjml = (template, layout = testLayout) => {
|
|
||||||
|
let hasMjmlError = (template, layout = testLayout) => {
|
||||||
let source = layout.replace(/\{\{\{body\}\}\}/g, template);
|
let source = layout.replace(/\{\{\{body\}\}\}/g, template);
|
||||||
let compiled;
|
let compiled;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
compiled = mjml.mjml2html(source);
|
compiled = mjml.mjml2html(source);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compiled.errors.length) {
|
if (compiled.errors.length) {
|
||||||
return compiled.errors[0].message || compiled.errors[0];
|
return compiled.errors[0].message || compiled.errors[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
let errors = [];
|
|
||||||
|
|
||||||
keys.forEach((key, index) => {
|
keys.forEach((key, index) => {
|
||||||
if (key.startsWith('mail_') || key.startsWith('web_')) {
|
if (key.startsWith('mail_') || key.startsWith('web_')) {
|
||||||
|
|
||||||
let template = values[index];
|
let template = values[index];
|
||||||
let err = isInvalidMjml(template);
|
let err = hasMjmlError(template);
|
||||||
|
|
||||||
err && errors.push(key + ': ' + (err.message || err));
|
err && errors.push(key + ': ' + (err.message || err));
|
||||||
if (key === 'mail_confirm_html' && !template.includes('{{confirmUrl}}')) {
|
key === 'mail_confirm_html' && !template.includes('{{confirmUrl}}') && errors.push(key + ': Missing {{confirmUrl}}');
|
||||||
errors.push(key + ': Missing {{confirmUrl}}');
|
|
||||||
}
|
|
||||||
} else if (key === 'layout') {
|
} else if (key === 'layout') {
|
||||||
|
|
||||||
let layout = values[index];
|
let layout = values[index];
|
||||||
let err = isInvalidMjml('', layout);
|
let err = hasMjmlError('', layout);
|
||||||
|
|
||||||
err && errors.push('layout: ' + (err.message || err));
|
err && errors.push('layout: ' + (err.message || err));
|
||||||
!layout.includes('{{{body}}}') && errors.push('layout: {{{body}}} not found');
|
!layout.includes('{{{body}}}') && errors.push('layout: {{{body}}} not found');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
errors.forEach((err, index) => {
|
errors.forEach((err, index) => {
|
||||||
errors[index] = (index + 1) + ') ' + err;
|
errors[index] = (index + 1) + ') ' + err;
|
||||||
});
|
});
|
||||||
return 'Please Fix These Errors:\n\n' + errors.join('\n');
|
return 'Please fix these MJML errors:\n\n' + errors.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue