mjml-validator
Advanced tools
Comparing version 4.0.0-alpha.4 to 4.0.0-beta.1
@@ -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. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13635
234
55
3
+ Addedmjml-core@^4.0.0-beta.1
+ Added@babel/runtime@7.25.7(transitive)
+ Added@isaacs/cliui@8.0.2(transitive)
+ Added@one-ini/wasm@0.1.1(transitive)
+ Added@pkgjs/parseargs@0.11.0(transitive)
+ Addedabbrev@2.0.0(transitive)
+ Addedansi-colors@4.1.3(transitive)
+ Addedansi-regex@5.0.16.1.0(transitive)
+ Addedansi-styles@4.3.06.2.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedboolbase@1.0.0(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedcamel-case@3.0.0(transitive)
+ Addedcheerio@1.0.0-rc.12(transitive)
+ Addedcheerio-select@2.1.0(transitive)
+ Addedclean-css@4.2.4(transitive)
+ Addedcliui@8.0.1(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcommander@10.0.12.20.36.2.1(transitive)
+ Addedconfig-chain@1.1.13(transitive)
+ Addedcross-spawn@7.0.3(transitive)
+ Addedcss-select@5.1.0(transitive)
+ Addedcss-what@6.1.0(transitive)
+ Addeddetect-node@2.1.0(transitive)
+ Addeddom-serializer@1.4.12.0.0(transitive)
+ Addeddomelementtype@2.3.0(transitive)
+ Addeddomhandler@3.3.04.3.15.0.3(transitive)
+ Addeddomutils@2.8.03.1.0(transitive)
+ Addedeastasianwidth@0.2.0(transitive)
+ Addededitorconfig@1.0.4(transitive)
+ Addedemoji-regex@8.0.09.2.2(transitive)
+ Addedentities@2.2.04.5.0(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedescape-goat@3.0.0(transitive)
+ Addedforeground-child@3.3.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedglob@10.4.5(transitive)
+ Addedhe@1.2.0(transitive)
+ Addedhtml-minifier@4.0.0(transitive)
+ Addedhtmlparser2@5.0.18.0.29.1.0(transitive)
+ Addedini@1.3.8(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjackspeak@3.4.3(transitive)
+ Addedjs-beautify@1.15.1(transitive)
+ Addedjs-cookie@3.0.5(transitive)
+ Addedjuice@10.0.1(transitive)
+ Addedlower-case@1.1.4(transitive)
+ Addedlru-cache@10.4.3(transitive)
+ Addedmensch@0.3.4(transitive)
+ Addedmime@2.6.0(transitive)
+ Addedminimatch@9.0.19.0.5(transitive)
+ Addedminipass@7.1.2(transitive)
+ Addedmjml-core@4.15.3(transitive)
+ Addedmjml-migrate@4.15.3(transitive)
+ Addedmjml-parser-xml@4.15.3(transitive)
+ Addedmjml-validator@4.15.3(transitive)
+ Addedno-case@2.3.2(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addednopt@7.2.1(transitive)
+ Addednth-check@2.1.1(transitive)
+ Addedpackage-json-from-dist@1.0.1(transitive)
+ Addedparam-case@2.1.1(transitive)
+ Addedparse5@7.1.2(transitive)
+ Addedparse5-htmlparser2-tree-adapter@7.0.0(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-scurry@1.11.1(transitive)
+ Addedproto-list@1.2.4(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
+ Addedrelateurl@0.2.7(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@4.1.0(transitive)
+ Addedslick@1.12.2(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addedstring-width@4.2.35.1.2(transitive)
+ Addedstrip-ansi@6.0.17.1.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addeduglify-js@3.19.3(transitive)
+ Addedupper-case@1.1.3(transitive)
+ Addedvalid-data-url@3.0.1(transitive)
+ Addedweb-resource-inliner@6.0.1(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwrap-ansi@7.0.08.1.0(transitive)
+ Addedy18n@5.0.8(transitive)
+ Addedyargs@17.7.2(transitive)
+ Addedyargs-parser@21.1.1(transitive)