html-validator
Advanced tools
Comparing version 2.0.4 to 2.1.0
73
index.js
'use strict' | ||
function validator (options, callback) { | ||
var request = require('request') | ||
var validUrl = require('valid-url') | ||
var setupOptions = require('./lib/setupOptions') | ||
const request = require('request') | ||
const validUrl = require('valid-url') | ||
const setupOptions = require('./lib/setup-options') | ||
if (!options) { | ||
return callback(new Error('Missing required input: options object'), null) | ||
} | ||
module.exports = (options, callback) => { | ||
return new Promise((resolve, reject) => { | ||
if (!options) { | ||
let error = new Error('Missing required input: options object') | ||
if (callback) { | ||
return callback(error, null) | ||
} | ||
reject(error) | ||
} | ||
if (!options.url && !options.data) { | ||
return callback(new Error('Missing required params: url or data'), null) | ||
} | ||
if (options.url && !validUrl.isWebUri(options.url)) { | ||
return callback(new Error('Invalid url'), null) | ||
} | ||
var reqOpts = setupOptions(options) | ||
request(reqOpts, function (error, response, result) { | ||
if (error) { | ||
return callback(error, null) | ||
if (!options.url && !options.data) { | ||
let error = new Error('Missing required params: url or data') | ||
if (callback) { | ||
return callback(error, null) | ||
} | ||
reject(error) | ||
} | ||
if (response && response.statusCode !== 200) { | ||
return callback(new Error('Validator returned unexpected statuscode: ' + response.statusCode), null) | ||
if (options.url && !validUrl.isWebUri(options.url)) { | ||
let error = new Error('Invalid url') | ||
if (callback) { | ||
return callback(error, null) | ||
} | ||
reject(error) | ||
} | ||
var data = options.format === 'json' ? JSON.parse(result) : result | ||
const reqOpts = setupOptions(options) | ||
return callback(null, data) | ||
request(reqOpts, (error, response, result) => { | ||
if (error) { | ||
if (callback) { | ||
return callback(error, null) | ||
} | ||
reject(error) | ||
} | ||
if (response && response.statusCode !== 200) { | ||
let error = new Error('Validator returned unexpected statuscode: ' + response.statusCode) | ||
if (callback) { | ||
return callback(error, null) | ||
} | ||
reject(error) | ||
} | ||
const data = options.format === 'json' ? JSON.parse(result) : result | ||
if (callback) { | ||
return callback(null, data) | ||
} | ||
resolve(data) | ||
}) | ||
}) | ||
} | ||
module.exports = validator |
{ | ||
"name": "html-validator", | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"description": "Validate html using validator.w3.org/nu", | ||
@@ -13,9 +13,10 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=4.6.1" | ||
"node": ">=4.7.0" | ||
}, | ||
"scripts": { | ||
"setup": "npm install && node setup.js", | ||
"test": "standard && nsp check && tap --reporter=spec test/*.js", | ||
"coverage": "tap test/*.js --coverage", | ||
"coveralls": "tap --cov --coverage-report=lcov test/*.js && cat coverage/lcov.info | coveralls" | ||
"setup": "npm install", | ||
"test": "standard && nsp check && tap --reporter=spec test/**/*.js", | ||
"coverage": "tap test/**/*.js --coverage", | ||
"coveralls": "tap --cov --coverage-report=lcov test/**/*.js && cat coverage/lcov.info | coveralls", | ||
"standard-fix": "standard --fix" | ||
}, | ||
@@ -38,7 +39,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"coveralls": "2.11.13", | ||
"coveralls": "2.11.14", | ||
"nsp": "2.6.2", | ||
"standard": "8.4.0", | ||
"standard": "8.6.0", | ||
"tap": "8.0.0" | ||
} | ||
} |
106
README.md
@@ -43,5 +43,7 @@ [](https://travis-ci.org/zrrrzzt/html-validator) | ||
```javascript | ||
var validator = require('html-validator') | ||
var options = { | ||
#### Promise | ||
```JavaScript | ||
const validator = require('html-validator') | ||
const options = { | ||
url: 'http://url-to-validate.com', | ||
@@ -51,5 +53,24 @@ format: 'text' | ||
validator(options, function (err, data) { | ||
if (err) { | ||
throw err | ||
validator(options) | ||
.then((data) => { | ||
console.log(data) | ||
}) | ||
.catch((error) => { | ||
console.error(error) | ||
}) | ||
``` | ||
#### Callback | ||
```JavaScript | ||
const validator = require('html-validator') | ||
const options = { | ||
url: 'http://url-to-validate.com', | ||
format: 'text' | ||
} | ||
validator(options, (error, data) => { | ||
if (error) { | ||
console.error(error) | ||
} | ||
@@ -63,5 +84,6 @@ console.log(data) | ||
```javascript | ||
var validator = require('html-validator') | ||
var fs = require('fs') | ||
#### Promise | ||
```JavaScript | ||
const validator = require('html-validator') | ||
const fs = require('fs') | ||
var options = { | ||
@@ -71,3 +93,3 @@ format: 'text' | ||
fs.readFile( 'file-to-validate.html', 'utf8', function (err, html) { | ||
fs.readFile( 'file-to-validate.html', 'utf8', (err, html) => { | ||
if (err) { | ||
@@ -77,7 +99,32 @@ throw err; | ||
opts.data = html | ||
options.data = html | ||
validator(opts, function (error, data) { | ||
validator(options) | ||
.then((data) => { | ||
console.log(data) | ||
}) | ||
.catch((error) => { | ||
console.error(error) | ||
}) | ||
}) | ||
``` | ||
#### Callback | ||
```JavaScript | ||
const validator = require('html-validator') | ||
const fs = require('fs') | ||
var options = { | ||
format: 'text' | ||
} | ||
fs.readFile( 'file-to-validate.html', 'utf8', (err, html) => { | ||
if (err) { | ||
throw err; | ||
} | ||
options.data = html | ||
validator(options, (error, data) => { | ||
if (error) { | ||
throw error | ||
console.error(error) | ||
} | ||
@@ -87,3 +134,2 @@ | ||
}) | ||
}) | ||
@@ -94,5 +140,7 @@ ``` | ||
```javascript | ||
var validator = require('html-validator') | ||
var options = { | ||
#### Promise | ||
```JavaScript | ||
const validator = require('html-validator') | ||
const options = { | ||
url: 'http://url-to-validate.com', | ||
@@ -103,5 +151,24 @@ validator: 'http://html5.validator.nu', | ||
validator(options, function(error, data) { | ||
validator(options) | ||
.then((data) => { | ||
console.log(data) | ||
}) | ||
.catch((error) => { | ||
console.error(error) | ||
}) | ||
``` | ||
#### Callback | ||
```JavaScript | ||
const validator = require('html-validator') | ||
const options = { | ||
url: 'http://url-to-validate.com', | ||
validator: 'http://html5.validator.nu', | ||
format: 'text' | ||
}; | ||
validator(options, (error, data) => { | ||
if (error) { | ||
throw error | ||
console.error(error) | ||
} | ||
@@ -111,3 +178,2 @@ | ||
}) | ||
``` | ||
@@ -114,0 +180,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
16142
15
330
173
4
1