Skip to content

Commit

Permalink
Option ignoreMessage to filter on substr or regex
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Jun 26, 2021
1 parent 39677c8 commit bd3643b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ width=800 alt=screenshot>

## 3) Options
### validate()
| Name (key) | Type | Default | Description |
| :------------ | :---------------------- | :------------------------------- | :---------------------------------------- |
| `html` | **string** | `null` | HTML string to validate. |
| `filename` | **string** | `null` | HTML file to validate. |
| `website` | **string** | `null` | URL of website to validate. |
| `checkUrl` | **string** | `'https://validator.w3.org/nu/'` | W3C validation API endpoint. |
| `ignoreLevel` | `'info'` or `'warning'` | `null` | Skip unwanted messages.* |
| `output` | `'json'` or `'html'` | `'json'` | Get results as an array or as a web page. |
| Name (key) | Type | Default | Description |
| :-------------- | :----------------------- | :------------------------------- | :------------------------------------------------------------ |
| `html` | **string** | `null` | HTML string to validate. |
| `filename` | **string** | `null` | HTML file to validate. |
| `website` | **string** | `null` | URL of website to validate. |
| `checkUrl` | **string** | `'https://validator.w3.org/nu/'` | W3C validation API endpoint. |
| `ignoreMessage` | **string** or **RegExp** | `null` | Skip messages containing a string or matching an expression.* |
| `ignoreLevel` | `'info'` or `'warning'` | `null` | Skip unwanted messages.* |
| `output` | `'json'` or `'html'` | `'json'` | Get results as an array or as a web page. |

*The `ignoreLevel` option only works for `'json'` output. 
*The `ignoreMessage` and `ignoreLevel` options only work for `'json'` output. 
Option value `'warning'` also skips `'info'`.

### reporter()
Expand Down
40 changes: 40 additions & 0 deletions spec/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,43 @@ describe('Option ignoreLevel set to "warning"', () => {
});

});

////////////////////////////////////////////////////////////////////////////////////////////////////
describe.only('Option ignoreMessage', () => {
// Example validation messgaes:
// warning: 'Section lacks heading. Consider using “h2”-“h6” elements to add identifying headings to all sections.',
// error: 'Element “blockquote” not allowed as child of element “span” in this context. (Suppressing further errors from this subtree.)',

it('as a substring can skip "Section lacks heading" messages', (done) => {
const handleData = (data) => {
const actual = {
validates: data.validates,
messages: data.messages.map(message => message.type),
};
const expected = {
validates: false,
messages: ['error'],
};
assertDeepStrictEqual(actual, expected, done);
};
const options = { filename: 'spec/html/invalid.html', ignoreMessage: 'Section lacks heading' };
w3cHtmlValidator.validate(options).then(handleData);
});

it('can skip messages matching a regular expression', (done) => {
const handleData = (data) => {
const actual = {
validates: data.validates,
messages: data.messages.map(message => message.type),
};
const expected = {
validates: false,
messages: ['info'],
};
assertDeepStrictEqual(actual, expected, done);
};
const options = { filename: 'spec/html/invalid.html', ignoreMessage: /^Element .blockquote./ };
w3cHtmlValidator.validate(options).then(handleData);
});

});
29 changes: 19 additions & 10 deletions w3c-html-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import log from 'fancy-log';
import request from 'superagent';

export type ValidatorOptions = {
html?: string, //example: '<!doctype html><html><head><title>Home</title></html>''
filename?: string, //example: 'docs/index.html'
website?: string //example: 'https://pretty-print-json.js.org/'
checkUrl?: string,
ignoreLevel?: 'info' | 'warning', //skip unwanted messages ('warning' also skips 'info')
output?: ValidatorResultsOutput,
html?: string, //example: '<!doctype html><html><head><title>Home</title></html>''
filename?: string, //example: 'docs/index.html'
website?: string //example: 'https://pretty-print-json.js.org/'
checkUrl?: string,
ignoreLevel?: 'info' | 'warning', //skip unwanted messages ('warning' also skips 'info')
ignoreMessage?: string | RegExp, //matcher to skip unwanted messages
output?: ValidatorResultsOutput,
};
export type ValidatorResultsMessage = {
// type subType
Expand Down Expand Up @@ -55,9 +56,10 @@ const w3cHtmlValidator = {

validate(options: ValidatorOptions): Promise<ValidatorResults> {
const defaults = {
checkUrl: 'https://validator.w3.org/nu/',
ignoreLevel: null,
output: 'json',
checkUrl: 'https://validator.w3.org/nu/',
ignoreLevel: null,
ignoreMessage: null,
output: 'json',
};
const settings = { ...defaults, ...options };
if (!settings.html && !settings.filename && !settings.website)
Expand Down Expand Up @@ -89,8 +91,15 @@ const w3cHtmlValidator = {
settings.ignoreLevel === 'info' && !!subType;
const aboveIgnoreLevel = (message: ValidatorResultsMessage): boolean =>
!settings.ignoreLevel || message.type !== 'info' || aboveInfo(message.subType);
const skipSubstr = (title: string) =>
typeof settings.ignoreMessage === 'string' && title.includes(settings.ignoreMessage);
const skipRegEx = (title: string) =>
settings.ignoreMessage?.constructor.name === 'RegExp' &&
(<RegExp>settings.ignoreMessage).test(title);
const isImportant = (message: ValidatorResultsMessage): boolean =>
aboveIgnoreLevel(message) && !skipSubstr(message.message) && !skipRegEx(message.message);
if (json)
response.body.messages = response.body.messages?.filter(aboveIgnoreLevel) ?? [];
response.body.messages = response.body.messages?.filter(isImportant) ?? [];
return response;
};
const toValidatorResults = (response: request.Response): ValidatorResults => ({
Expand Down

0 comments on commit bd3643b

Please sign in to comment.