1.0.0 • Published 6 years ago
shopback-nodejs-test v1.0.0
const { Validator, InputType, OutputType } = require('shopback-nodejs-test');
const validator = new Validator();
// Skip predefined 1-index based rules
validator.skipRules([1,4,5]);
// Set input type. There are 2 types: FILE and STREAM. It's by default FILE
validator.setInput(InputType.STREAM);
// Set output type. There are 3 types: CONSOLE, FILE and STREAM. It's by default CONSOLE
validator.setOutput(OutputType.FILE);
try {
validator
.validate()
.then(async () => { await validator.getResult() })
.catch(err => { console.error(err) });
Requirements
Node.js 8.0 or greater
Installation
npm install shopback-nodejs-test
Predefined Rules
- Detect if there are any
<img />
tags withoutalt
attribute - Detect if there are any
<a />
tags withoutrel
attribute - Detect if there is any header that doesn’t have
<title>
tag - Detect if there is any header that doesn’t have
<meta name="descriptions" … />
tag - Detect if there is any header that doesn’t have
<meta name="keywords" … />
tag - Detect if there are more than 15
<strong>
tag in HTML - Detect if a HTML have more than one
<h1>
tag
API
Rule
const {
TagExistenceRule,
TagExistenceWithAttrRule,
TagLimitCountRule,
TagWithoutAttrCountRule
} = require('shopback-nodejs-test');
new TagExistenceRule('head', 'title');
new TagExistenceWithAttrRule('head', 'img', 'alt');
new TagExistenceWithAttrRule('head', 'meta', 'name', 'description');
new TagLimitCountRule('', 'strong', 15);
new TagLimitCountRule('', 'h1', 1);
new TagWithoutAttrCountRule('', 'img', 'alt');
new TagWithoutAttrCountRule('', 'a', 'rel');
// Add rules
validator.addRule(new TagExistenceRule('head', 'title'));
validator.addRule(new TagExistenceWithAttrRule('head', 'img', 'alt'));
Input
There are 2 input types: FILE and STREAM. By default FILE, only invole setInput method of Validator in case of changing the input type The input path is configured in the config.js file by input_path
const { InputType } = require('shopback-nodejs-test');
validator.setInput(InputType.FILE); // or InputType.STREAM.
Output
There are 3 output types: CONSOLE, FILE and STREAM. By default CONSOLE, only invole setOutput method of Validator in case of changing the output type The output path is configured in the config.js file by output_path
const { OutputType } = require('shopback-nodejs-test');
validator.setOutput(OutputType.FILE);
Validator
constructor(skipPredefinedRules)
All predefined rules is by default added (skipPredefinedRules = false)
const { Validator } = require('shopback-nodejs-test');
const validator = new Validator(true); // skip all predefined rules
skipRules(indexes)
Skip 1-indexed based rules
validator.skipRules([1,4,5]);
addRule(rule)
validator.addRule(new RuleExistTag('head', 'title'));
validate() && getResult()
try {
validator
.validate()
.then(async () => {
await validator.getResult()
});
} catch (err) {
console.error(err);
}
Custom Rules
const { Rule } = require('shopback-nodejs-test');
class NewRule extends Rule {
constructor(rootTag, ...params) {
super(rootTag);
...
}
validate() {
// Write logic
this.isValid = true; // or false
}
error() {
return !this.isValid ? `Error message` : '';
}
}
validator.addRule(new NewRule(rootTag, ...params));
1.0.0
6 years ago