# html-validator

> Validate html using validator.w3.org/nu og html-validate

Latest version **6.0.1** (published 2022-05-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install html-validator
pnpm add html-validator
yarn add html-validator
bun add html-validator
```

## Health

**Score 38/100 (D)** — status: abandoned.

Positive: has types package; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 6.0.1 |
| Published | 2022-05-14 |
| First published | 2014-03-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/html-validator) |
| Module format | CommonJS |
| Node | >=14.19.2 |
| Dependencies | 3 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 (+24 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 73 |
| Author | Geir Gåsodden |
| Maintainers | davidtheclark, zrrrzzt |
| Keywords | html-validator, html-validate, validation, validator.w3.org |

## Links

- npm: https://www.npmjs.com/package/html-validator
- Repository: https://github.com/zrrrzzt/html-validator
- Homepage: https://github.com/zrrrzzt/html-validator#readme
- Issues: https://github.com/zrrrzzt/html-validator/issues
- npm.io page: https://npm.io/package/html-validator

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) 0.27.2
- [valid-url](https://npm.io/package/valid-url.md) 1.0.9
- [html-validate](https://npm.io/package/html-validate.md) 7.0.0

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 6.0.1 (latest) — 2022-05-14
- 6.0.0 — 2021-11-21
- 5.1.18 — 2021-11-20
- 5.1.17 — 2020-09-25
- 5.1.16 — 2020-08-28
- 5.1.15 — 2020-08-16
- 5.1.14 — 2020-05-22
- 5.1.13 — 2020-05-22
- 5.1.12 — 2020-05-01
- 5.0.3 — 2020-05-01
- 5.1.11 — 2020-05-01
- 5.0.2 — 2020-05-01
- 5.1.10 — 2020-05-01
- 5.1.9 — 2020-04-29
- 5.1.8 — 2020-04-12
- … 47 more at https://npm.io/package/html-validator/versions

## README

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)

# html-validator

A [Node.js](https://nodejs.org/) module for validating html using [validator.w3.org/nu](https://validator.w3.org/nu/) or [html-validate](https://html-validate.org/)

Requires Node >= 14.18.1

## Webpack

If you are using Webpack you might have som problems due to `html-validate`.
You can stick to the 5.0.3 version of this module or try some of the workarounds [described here](https://html-validate.org/dev/running-in-browser.html)

## Module

Supports the following modes from Validator.nu [Web Service Interface](https://github.com/validator/validator/wiki/Service-%C2%BB-HTTP-interface)
- Document URL as a GET parameter; the service retrieves the document by URL over HTTP or HTTPS.
- Document POSTed as the HTTP entity body; parameters in query string as with GET.

From html-validate it will only validate against the WHATWG standards.

### Installation

```sh
$ npm i html-validator
```
### Usage

Create an options object.

**format** This is the formatting of the returned data. It supports json (default), html, xhtml, xml, gnu and text for W3C and json for WHATWG.

**validator** You can override the default validator (W3C) as long as it exposes the same REST interface. To use WHATWG just pass `WHATWG` as a string

**url**/**data** The url to the page you want to validate or the data you want validated.

**ignore** String or array of strings or rules (when using WHATWG) you want the checker to remove in the response

**isLocal** Set this to true if you want to validate local urls

**isFragment** Set this to true if your data input is not a complete document


#### W3C (default)

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    url: 'http://url-to-validate.com',
    format: 'text'
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```
**data** The html you want to validate

```JavaScript
(async () => {
  const validator = require('html-validator')
  const { readFileSync } = require('fs')
  const options = {
    url: 'http://url-to-validate.com',
    format: 'text',
    data: readFileSync('file-to-validate.html', 'utf8')
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

**validator** You can override the default validator as long as it exposes the same REST interface. Or use the WHATWG option for validating your files locally

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    url: 'http://url-to-validate.com',
    validator: 'http://html5.validator.nu',
    format: 'text'
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    url: 'http://url-to-validate.com',
    validator: 'WHATWG',
    format: 'text'
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

or run the w3c validator locally with docker `$ docker run -d -p 8888:8888 validator/validator` and

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    url: 'http://url-to-validate.com',
    validator: 'http://localhost:8888',
    format: 'text'
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

**ignore** String or array of strings you want the checker to remove in the response. Requires format = text

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    url: 'http://url-to-validate.com',
    format: 'text',
    ignore: 'Error: Stray end tag “div”.'
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

**headers** Object of headers to pass in with the url request

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    url: 'http://url-to-validate.com',
    format: 'text',
    headers: {foo:"bar"}
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

**isLocal** Set this to true if you want to validate local urls. Will lookup page and send data to validator

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    url: 'http://url-to-validate.com',
    format: 'text',
    isLocal: true
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

**isFragment** Set this to true if you want to validate something that is not a complete document

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    data: '<p>This is a fragment</p>',
    isFragment: true
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

#### WHATWG

Using this option will validate your files locally and will probably speed up your tests in addition to not sending data over the wire for validation.

You can follow all the examples from W3C as long as you changes the options validator property to `WHATWG`

```JavaScript
(async () => {
  const validator = require('html-validator')
  const options = {
    validator: 'WHATWG',
    data: '<p>This is a fragment</p>',
    isFragment: true
  }
  
  try {
    const result = await validator(options)
    console.log(result)
  } catch (error) {
    console.error(error)
  }
})()
```

## Related

- [site-validator-cli](https://github.com/p1ho/site-validator-cli) CLI for validating a whole site or multiple pages
- [html-validator-cli](https://github.com/zrrrzzt/html-validator-cli) CLI for this module
- [html-validate](https://www.npmjs.com/package/html-validate) offline HTML5 validator

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/html-validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
