mjml-validator
Advanced tools
Comparing version
@@ -12,2 +12,6 @@ 'use strict'; | ||
var _lodash = require('lodash'); | ||
var _mjmlCore = require('mjml-core'); | ||
var _rules = require('./rules'); | ||
@@ -21,7 +25,10 @@ | ||
var MJMLRulesCollection = rules; | ||
var MJMLRulesCollection = {}; | ||
(0, _lodash.mapKeys)(rules, function (func, name) { | ||
return registerRule(func, name, { components: _mjmlCore.components }); | ||
}); | ||
function registerRule(rule, name) { | ||
if (typeof rule !== 'function' || rule.length !== 1) { | ||
return (0, _warning2.default)(false, 'Your rule must be a function and must have one parameter which is the element to validate'); | ||
if (typeof rule !== 'function' || rule.length !== 2) { | ||
return (0, _warning2.default)(false, 'Your rule must be a function and must have two parameters which are the element to validate and the components object imported from mjml-core'); | ||
} | ||
@@ -28,0 +35,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -9,3 +9,4 @@ Object.defineProperty(exports, "__esModule", { | ||
var line = element.line, | ||
tagName = element.tagName; | ||
tagName = element.tagName, | ||
absoluteFilePath = element.absoluteFilePath; | ||
@@ -17,5 +18,20 @@ | ||
tagName: tagName, | ||
formattedMessage: "Line " + line + " (" + tagName + ") \u2014 " + message | ||
formattedMessage: 'Line ' + line + ' of ' + absoluteFilePath + formatInclude(element) + ' (' + tagName + ') \u2014 ' + message | ||
}; | ||
} | ||
module.exports = exports["default"]; | ||
function formatInclude(element) { | ||
var includedIn = element.includedIn; | ||
if (!(includedIn && includedIn.length)) return ''; | ||
var formattedIncluded = includedIn.slice().reverse().map(function (_ref) { | ||
var line = _ref.line, | ||
file = _ref.file; | ||
return 'line ' + line + ' of file ' + file; | ||
}).join(', itself included at '); | ||
return ', included at ' + formattedIncluded; | ||
} | ||
module.exports = exports['default']; |
@@ -16,2 +16,6 @@ 'use strict'; | ||
var _keys = require('lodash/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _dependencies = require('../dependencies'); | ||
@@ -56,5 +60,17 @@ | ||
return (0, _ruleError2.default)(childTagName + ' cannot be used inside ' + tagName + ', only inside: ' + parentDependencies.join(', '), child); | ||
if (parentDependencies.some(function (dep) { | ||
return dep instanceof RegExp && dep.test(childTagName); | ||
})) { | ||
return true; | ||
} | ||
var allowedDependencies = (0, _keys2.default)(_dependencies2.default).filter(function (key) { | ||
return (0, _includes2.default)(_dependencies2.default[key], childTagName) || _dependencies2.default[key].some(function (dep) { | ||
return dep instanceof RegExp && dep.test(childTagName); | ||
}); | ||
}); | ||
return (0, _ruleError2.default)(childTagName + ' cannot be used inside ' + tagName + ', only inside: ' + allowedDependencies.join(', '), child); | ||
})); | ||
} | ||
module.exports = exports['default']; |
@@ -8,2 +8,4 @@ 'use strict'; | ||
var _lodash = require('lodash'); | ||
var _ruleError = require('./ruleError'); | ||
@@ -15,2 +17,5 @@ | ||
// Tags that have no associated components but are allowed even so | ||
var componentLessTags = ['mj-all']; | ||
function validateTag(element, _ref) { | ||
@@ -21,2 +26,4 @@ var components = _ref.components; | ||
if ((0, _lodash.includes)(componentLessTags, tagName)) return null; | ||
var Component = components[tagName]; | ||
@@ -23,0 +30,0 @@ |
{ | ||
"name": "mjml-validator", | ||
"description": "mjml-validator", | ||
"version": "4.0.0-alpha.4", | ||
"version": "4.0.0-beta.1", | ||
"main": "lib/index.js", | ||
@@ -21,4 +21,5 @@ "repository": { | ||
"lodash": "^4.17.4", | ||
"mjml-core": "^4.0.0-beta.1", | ||
"warning": "^3.0.0" | ||
} | ||
} |
# Validating MJML | ||
MJML provides a validation layer that helps you building your email. It can detect if you misplaced or mispelled a MJML component, or if you used any unauthorised attribute on a specific component. It supports 3 levels of validation: | ||
* skip: your document is rendered without going through validation | ||
* soft: your document is going through validation and is rendered, even if it has errors | ||
* strict: your document is going through validation and is not rendered if it has any error | ||
* `skip`: your document is rendered without going through validation | ||
* `soft`: your document is going through validation and is rendered, even if it has errors | ||
* `strict`: your document is going through validation and is not rendered if it has any error | ||
By default, the level is set to `soft`. | ||
@@ -17,3 +18,3 @@ | ||
```bash | ||
$> mjml --validate template.mjml | ||
mjml --validate template.mjml | ||
``` | ||
@@ -24,3 +25,3 @@ | ||
```bash | ||
$> mjml -l skip -r template.mjml | ||
mjml -l skip -r template.mjml | ||
``` | ||
@@ -33,4 +34,4 @@ | ||
`strict` will raise a `MJMLValidationError` exception. This object has 2 methods: | ||
- `getErrors` returns an array of objects with `line`, `message`, `tagName` as well as a `formattedMessage` which contains the `line`, `message` and `tagName` concatenated in a sentence. | ||
- `getMessages` returns an array of `formattedMessage`. | ||
* `getErrors` returns an array of objects with `line`, `message`, `tagName` as well as a `formattedMessage` which contains the `line`, `message` and `tagName` concatenated in a sentence. | ||
* `getMessages` returns an array of `formattedMessage`. | ||
@@ -42,6 +43,8 @@ When using `soft`, no exception will be raised. You can get the errors using the object returned by the `render` method `errors`. It is the same object returned by `getErrors` on strict mode. | ||
Add the following key to the json: | ||
```json | ||
{ | ||
... | ||
"validators": [ | ||
"validateTag", | ||
"validateTag", | ||
"validateAttribute", | ||
@@ -52,4 +55,5 @@ "validChildren", | ||
} | ||
``` | ||
"validateTag", "validateAttribute" and "validChildren" are the default validation rules. | ||
If you want to add custom rules, you can add the path to the file where the validtion function resides. | ||
`"validateTag"`, `"validateAttribute"` and `"validChildren"` are the default validation rules. | ||
If you want to add custom rules, you can add the path to the file where the validtion function resides. |
13635
12.77%234
15.84%55
10%3
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added