har-validator
Advanced tools
Comparing version 3.2.0 to 3.3.0
@@ -7,3 +7,3 @@ 'use strict'; | ||
exports.validator = validator; | ||
exports.default = har; | ||
exports.har = har; | ||
exports.cache = cache; | ||
@@ -26,6 +26,10 @@ exports.cacheEntry = cacheEntry; | ||
var _schemas = require('./schemas'); | ||
var _harSchema = require('har-schema'); | ||
var schemas = _interopRequireWildcard(_schemas); | ||
var schemas = _interopRequireWildcard(_harSchema); | ||
var _ajv = require('ajv'); | ||
var _ajv2 = _interopRequireDefault(_ajv); | ||
var _error = require('./error'); | ||
@@ -35,6 +39,2 @@ | ||
var _isMyJsonValid = require('is-my-json-valid'); | ||
var _isMyJsonValid2 = _interopRequireDefault(_isMyJsonValid); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -52,9 +52,10 @@ | ||
// validator config | ||
var validate = (0, _isMyJsonValid2.default)(schema, { | ||
greedy: true, | ||
verbose: true, | ||
var ajv = new _ajv2.default({ | ||
allErrors: true, | ||
schemas: schemas | ||
}); | ||
// execute is-my-json-valid | ||
var validate = ajv.compile(schema); | ||
// execute validation | ||
valid = validate(data); | ||
@@ -61,0 +62,0 @@ |
#!/usr/bin/env node | ||
'use strict'; | ||
var _promise = require('./promise'); | ||
var validate = _interopRequireWildcard(_promise); | ||
var _chalk = require('chalk'); | ||
@@ -24,16 +28,8 @@ | ||
var _package = require('../package.json'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _package2 = _interopRequireDefault(_package); | ||
var _promise = require('./promise'); | ||
var schemas = _interopRequireWildcard(_promise); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
_commander2.default.usage('[options] <files...>').option('-s, --schema [name]', 'validate schema name (log, request, response, etc ...)').parse(process.argv); | ||
_commander2.default.version(_package2.default.version).usage('[options] <files...>').option('-s, --schema [name]', 'validate schema name (log, request, response, etc ...)').parse(process.argv); | ||
if (!_commander2.default.args.length) { | ||
@@ -43,2 +39,4 @@ _commander2.default.help(); | ||
var schema = _commander2.default.schema || 'har'; | ||
_commander2.default.args.map(function (fileName) { | ||
@@ -51,7 +49,9 @@ var file = _chalk2.default.yellow.italic(_path2.default.basename(fileName)); | ||
}); | ||
}).then(JSON.parse).then(_commander2.default.schema && _commander2.default.schema !== 'har' ? schemas[_commander2.default.schema] : schemas.default).then(function (data) { | ||
return console.log('%s [%s] is valid', _chalk2.default.green('✓'), file); | ||
}).then(JSON.parse).then(validate[schema]).then(function (data) { | ||
return console.log('%s [%s] is a valid %s', _chalk2.default.green('✓'), file, _chalk2.default.magenta(schema)); | ||
}).catch(function (err) { | ||
console.error('[%s]', file); | ||
if (err instanceof SyntaxError) { | ||
return console.error('%s [%s] failed to read JSON: %s', _chalk2.default.red('✖'), file, _chalk2.default.red(err.message)); | ||
return console.error('%s failed to read JSON: %s', _chalk2.default.red('✖'), _chalk2.default.red(err.message)); | ||
} | ||
@@ -61,3 +61,3 @@ | ||
err.errors.forEach(function (details) { | ||
return console.error('%s [%s] failed validation: (%s: %s) %s', _chalk2.default.red('✖'), file, _chalk2.default.cyan.italic(details.field), _chalk2.default.magenta.italic(details.value), _chalk2.default.red(details.message)); | ||
return console.error('%s validation failed on %s: %s', _chalk2.default.red('✖'), _chalk2.default.cyan.italic(details.dataPath || '.'), _chalk2.default.red(details.message)); | ||
}); | ||
@@ -67,4 +67,4 @@ return; | ||
console.error('%s [%s] an unknown error has occured: %s', _chalk2.default.red('✖'), file, _chalk2.default.red(err.message)); | ||
console.error('%s an unknown error has occured: %s', _chalk2.default.red('✖'), _chalk2.default.red(err.message)); | ||
}); | ||
}); |
@@ -8,4 +8,13 @@ 'use strict'; | ||
function HARError(errors) { | ||
this.name = 'HARError'; | ||
var message = 'validation failed'; | ||
this.name = this.constructor.name; | ||
this.message = message; | ||
this.errors = errors; | ||
if (typeof Error.captureStackTrace === 'function') { | ||
Error.captureStackTrace(this, this.constructor); | ||
} else { | ||
this.stack = new Error(message).stack; | ||
} | ||
} | ||
@@ -12,0 +21,0 @@ |
@@ -7,3 +7,3 @@ 'use strict'; | ||
exports.validator = validator; | ||
exports.default = har; | ||
exports.har = har; | ||
exports.cache = cache; | ||
@@ -25,6 +25,10 @@ exports.cacheEntry = cacheEntry; | ||
var _schemas = require('./schemas'); | ||
var _harSchema = require('har-schema'); | ||
var schemas = _interopRequireWildcard(_schemas); | ||
var schemas = _interopRequireWildcard(_harSchema); | ||
var _ajv = require('ajv'); | ||
var _ajv2 = _interopRequireDefault(_ajv); | ||
var _error = require('./error'); | ||
@@ -34,6 +38,2 @@ | ||
var _isMyJsonValid = require('is-my-json-valid'); | ||
var _isMyJsonValid2 = _interopRequireDefault(_isMyJsonValid); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -48,9 +48,10 @@ | ||
// validator config | ||
var validate = (0, _isMyJsonValid2.default)(schema, { | ||
greedy: true, | ||
verbose: true, | ||
var ajv = new _ajv2.default({ | ||
allErrors: true, | ||
schemas: schemas | ||
}); | ||
// execute is-my-json-valid | ||
var validate = ajv.compile(schema); | ||
// execute validation | ||
validate(data); | ||
@@ -57,0 +58,0 @@ |
@@ -7,3 +7,3 @@ 'use strict'; | ||
exports.validator = validator; | ||
exports.default = har; | ||
exports.har = har; | ||
exports.cache = cache; | ||
@@ -26,6 +26,10 @@ exports.cacheEntry = cacheEntry; | ||
var _schemas = require('./schemas'); | ||
var _harSchema = require('har-schema'); | ||
var schemas = _interopRequireWildcard(_schemas); | ||
var schemas = _interopRequireWildcard(_harSchema); | ||
var _ajv = require('ajv'); | ||
var _ajv2 = _interopRequireDefault(_ajv); | ||
var _error = require('./error'); | ||
@@ -35,6 +39,2 @@ | ||
var _isMyJsonValid = require('is-my-json-valid'); | ||
var _isMyJsonValid2 = _interopRequireDefault(_isMyJsonValid); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -49,9 +49,10 @@ | ||
// validator config | ||
let validate = (0, _isMyJsonValid2.default)(schema, { | ||
greedy: true, | ||
verbose: true, | ||
let ajv = new _ajv2.default({ | ||
allErrors: true, | ||
schemas: schemas | ||
}); | ||
// execute is-my-json-valid | ||
let validate = ajv.compile(schema); | ||
// execute validation | ||
valid = validate(data); | ||
@@ -58,0 +59,0 @@ |
#!/usr/bin/env node | ||
'use strict'; | ||
var _promise = require('./promise'); | ||
var validate = _interopRequireWildcard(_promise); | ||
var _chalk = require('chalk'); | ||
@@ -24,16 +28,8 @@ | ||
var _package = require('../package.json'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _package2 = _interopRequireDefault(_package); | ||
var _promise = require('./promise'); | ||
var schemas = _interopRequireWildcard(_promise); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
_commander2.default.usage('[options] <files...>').option('-s, --schema [name]', 'validate schema name (log, request, response, etc ...)').parse(process.argv); | ||
_commander2.default.version(_package2.default.version).usage('[options] <files...>').option('-s, --schema [name]', 'validate schema name (log, request, response, etc ...)').parse(process.argv); | ||
if (!_commander2.default.args.length) { | ||
@@ -43,2 +39,4 @@ _commander2.default.help(); | ||
let schema = _commander2.default.schema || 'har'; | ||
_commander2.default.args.map(fileName => { | ||
@@ -49,14 +47,16 @@ let file = _chalk2.default.yellow.italic(_path2.default.basename(fileName)); | ||
_fs2.default.readFile(fileName, (err, data) => err === null ? resolve(data) : reject(err)); | ||
}).then(JSON.parse).then(_commander2.default.schema && _commander2.default.schema !== 'har' ? schemas[_commander2.default.schema] : schemas.default).then(data => console.log('%s [%s] is valid', _chalk2.default.green('✓'), file)).catch(err => { | ||
}).then(JSON.parse).then(validate[schema]).then(data => console.log('%s [%s] is a valid %s', _chalk2.default.green('✓'), file, _chalk2.default.magenta(schema))).catch(err => { | ||
console.error('[%s]', file); | ||
if (err instanceof SyntaxError) { | ||
return console.error('%s [%s] failed to read JSON: %s', _chalk2.default.red('✖'), file, _chalk2.default.red(err.message)); | ||
return console.error('%s failed to read JSON: %s', _chalk2.default.red('✖'), _chalk2.default.red(err.message)); | ||
} | ||
if (err instanceof _error2.default) { | ||
err.errors.forEach(details => console.error('%s [%s] failed validation: (%s: %s) %s', _chalk2.default.red('✖'), file, _chalk2.default.cyan.italic(details.field), _chalk2.default.magenta.italic(details.value), _chalk2.default.red(details.message))); | ||
err.errors.forEach(details => console.error('%s validation failed on %s: %s', _chalk2.default.red('✖'), _chalk2.default.cyan.italic(details.dataPath || '.'), _chalk2.default.red(details.message))); | ||
return; | ||
} | ||
console.error('%s [%s] an unknown error has occured: %s', _chalk2.default.red('✖'), file, _chalk2.default.red(err.message)); | ||
console.error('%s an unknown error has occured: %s', _chalk2.default.red('✖'), _chalk2.default.red(err.message)); | ||
}); | ||
}); |
@@ -8,4 +8,13 @@ 'use strict'; | ||
function HARError(errors) { | ||
this.name = 'HARError'; | ||
let message = 'validation failed'; | ||
this.name = this.constructor.name; | ||
this.message = message; | ||
this.errors = errors; | ||
if (typeof Error.captureStackTrace === 'function') { | ||
Error.captureStackTrace(this, this.constructor); | ||
} else { | ||
this.stack = new Error(message).stack; | ||
} | ||
} | ||
@@ -12,0 +21,0 @@ |
@@ -7,3 +7,3 @@ 'use strict'; | ||
exports.validator = validator; | ||
exports.default = har; | ||
exports.har = har; | ||
exports.cache = cache; | ||
@@ -25,6 +25,10 @@ exports.cacheEntry = cacheEntry; | ||
var _schemas = require('./schemas'); | ||
var _harSchema = require('har-schema'); | ||
var schemas = _interopRequireWildcard(_schemas); | ||
var schemas = _interopRequireWildcard(_harSchema); | ||
var _ajv = require('ajv'); | ||
var _ajv2 = _interopRequireDefault(_ajv); | ||
var _error = require('./error'); | ||
@@ -34,6 +38,2 @@ | ||
var _isMyJsonValid = require('is-my-json-valid'); | ||
var _isMyJsonValid2 = _interopRequireDefault(_isMyJsonValid); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -46,9 +46,10 @@ | ||
// validator config | ||
let validate = (0, _isMyJsonValid2.default)(schema, { | ||
greedy: true, | ||
verbose: true, | ||
let ajv = new _ajv2.default({ | ||
allErrors: true, | ||
schemas: schemas | ||
}); | ||
// execute is-my-json-valid | ||
let validate = ajv.compile(schema); | ||
// execute validation | ||
validate(data); | ||
@@ -55,0 +56,0 @@ |
@@ -7,3 +7,3 @@ 'use strict'; | ||
exports.validator = validator; | ||
exports.default = har; | ||
exports.har = har; | ||
exports.cache = cache; | ||
@@ -26,6 +26,10 @@ exports.cacheEntry = cacheEntry; | ||
var _schemas = require('./schemas'); | ||
var _harSchema = require('har-schema'); | ||
var schemas = _interopRequireWildcard(_schemas); | ||
var schemas = _interopRequireWildcard(_harSchema); | ||
var _ajv = require('ajv'); | ||
var _ajv2 = _interopRequireDefault(_ajv); | ||
var _error = require('./error'); | ||
@@ -35,6 +39,2 @@ | ||
var _isMyJsonValid = require('is-my-json-valid'); | ||
var _isMyJsonValid2 = _interopRequireDefault(_isMyJsonValid); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -49,9 +49,10 @@ | ||
// validator config | ||
let validate = (0, _isMyJsonValid2.default)(schema, { | ||
greedy: true, | ||
verbose: true, | ||
let ajv = new _ajv2.default({ | ||
allErrors: true, | ||
schemas: schemas | ||
}); | ||
// execute is-my-json-valid | ||
let validate = ajv.compile(schema); | ||
// execute validation | ||
valid = validate(data); | ||
@@ -58,0 +59,0 @@ |
#!/usr/bin/env node | ||
'use strict'; | ||
var _promise = require('./promise'); | ||
var validate = _interopRequireWildcard(_promise); | ||
var _chalk = require('chalk'); | ||
@@ -24,16 +28,8 @@ | ||
var _package = require('../package.json'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _package2 = _interopRequireDefault(_package); | ||
var _promise = require('./promise'); | ||
var schemas = _interopRequireWildcard(_promise); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
_commander2.default.usage('[options] <files...>').option('-s, --schema [name]', 'validate schema name (log, request, response, etc ...)').parse(process.argv); | ||
_commander2.default.version(_package2.default.version).usage('[options] <files...>').option('-s, --schema [name]', 'validate schema name (log, request, response, etc ...)').parse(process.argv); | ||
if (!_commander2.default.args.length) { | ||
@@ -43,2 +39,4 @@ _commander2.default.help(); | ||
let schema = _commander2.default.schema || 'har'; | ||
_commander2.default.args.map(fileName => { | ||
@@ -49,14 +47,16 @@ let file = _chalk2.default.yellow.italic(_path2.default.basename(fileName)); | ||
_fs2.default.readFile(fileName, (err, data) => err === null ? resolve(data) : reject(err)); | ||
}).then(JSON.parse).then(_commander2.default.schema && _commander2.default.schema !== 'har' ? schemas[_commander2.default.schema] : schemas.default).then(data => console.log('%s [%s] is valid', _chalk2.default.green('✓'), file)).catch(err => { | ||
}).then(JSON.parse).then(validate[schema]).then(data => console.log('%s [%s] is a valid %s', _chalk2.default.green('✓'), file, _chalk2.default.magenta(schema))).catch(err => { | ||
console.error('[%s]', file); | ||
if (err instanceof SyntaxError) { | ||
return console.error('%s [%s] failed to read JSON: %s', _chalk2.default.red('✖'), file, _chalk2.default.red(err.message)); | ||
return console.error('%s failed to read JSON: %s', _chalk2.default.red('✖'), _chalk2.default.red(err.message)); | ||
} | ||
if (err instanceof _error2.default) { | ||
err.errors.forEach(details => console.error('%s [%s] failed validation: (%s: %s) %s', _chalk2.default.red('✖'), file, _chalk2.default.cyan.italic(details.field), _chalk2.default.magenta.italic(details.value), _chalk2.default.red(details.message))); | ||
err.errors.forEach(details => console.error('%s validation failed on %s: %s', _chalk2.default.red('✖'), _chalk2.default.cyan.italic(details.dataPath || '.'), _chalk2.default.red(details.message))); | ||
return; | ||
} | ||
console.error('%s [%s] an unknown error has occured: %s', _chalk2.default.red('✖'), file, _chalk2.default.red(err.message)); | ||
console.error('%s an unknown error has occured: %s', _chalk2.default.red('✖'), _chalk2.default.red(err.message)); | ||
}); | ||
}); |
@@ -8,4 +8,13 @@ 'use strict'; | ||
function HARError(errors) { | ||
this.name = 'HARError'; | ||
let message = 'validation failed'; | ||
this.name = this.constructor.name; | ||
this.message = message; | ||
this.errors = errors; | ||
if (typeof Error.captureStackTrace === 'function') { | ||
Error.captureStackTrace(this, this.constructor); | ||
} else { | ||
this.stack = new Error(message).stack; | ||
} | ||
} | ||
@@ -12,0 +21,0 @@ |
@@ -7,3 +7,3 @@ 'use strict'; | ||
exports.validator = validator; | ||
exports.default = har; | ||
exports.har = har; | ||
exports.cache = cache; | ||
@@ -25,6 +25,10 @@ exports.cacheEntry = cacheEntry; | ||
var _schemas = require('./schemas'); | ||
var _harSchema = require('har-schema'); | ||
var schemas = _interopRequireWildcard(_schemas); | ||
var schemas = _interopRequireWildcard(_harSchema); | ||
var _ajv = require('ajv'); | ||
var _ajv2 = _interopRequireDefault(_ajv); | ||
var _error = require('./error'); | ||
@@ -34,6 +38,2 @@ | ||
var _isMyJsonValid = require('is-my-json-valid'); | ||
var _isMyJsonValid2 = _interopRequireDefault(_isMyJsonValid); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -46,9 +46,10 @@ | ||
// validator config | ||
let validate = (0, _isMyJsonValid2.default)(schema, { | ||
greedy: true, | ||
verbose: true, | ||
let ajv = new _ajv2.default({ | ||
allErrors: true, | ||
schemas: schemas | ||
}); | ||
// execute is-my-json-valid | ||
let validate = ajv.compile(schema); | ||
// execute validation | ||
validate(data); | ||
@@ -55,0 +56,0 @@ |
{ | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"name": "har-validator", | ||
@@ -19,2 +19,3 @@ "description": "Extremely fast HTTP Archive (HAR) validator using JSON Schema", | ||
"cli", | ||
"ajv", | ||
"http", | ||
@@ -38,5 +39,7 @@ "archive", | ||
"compile": "babel -q src", | ||
"test": "BABEL_ENV=test tap test/*.js --reporter spec --node-arg=--require --node-arg=babel-register", | ||
"test": "BABEL_ENV=test tap test --reporter spec --node-arg=--require --node-arg=babel-register", | ||
"test-one": "BABEL_ENV=test tap --reporter spec --node-arg=--require --node-arg=babel-register", | ||
"test-cli": "BABEL_ENV=test babel-node src/bin.js", | ||
"pretest": "snazzy && echint", | ||
"coverage": "BABEL_ENV=test tap test/*.js --reporter silent --coverage --nyc-arg=--require --nyc-arg=babel-register", | ||
"coverage": "BABEL_ENV=test tap test --reporter silent --coverage --nyc-arg=--require --nyc-arg=babel-register", | ||
"codeclimate": "BABEL_ENV=test tap --coverage-report=text-lcov | codeclimate-test-reporter", | ||
@@ -73,6 +76,7 @@ "semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
"dependencies": { | ||
"ajv": "^4.9.0", | ||
"chalk": "^1.1.3", | ||
"commander": "^2.9.0", | ||
"is-my-json-valid": "^2.15.0" | ||
"har-schema": "^0.1.0" | ||
} | ||
} |
@@ -26,3 +26,3 @@ # HAR Validator [![version][npm-version]][npm-url] [![License][npm-license]][license-url] | ||
*/ | ||
const validator = require('har-validator/lib/node7') | ||
const validate = require('har-validator/lib/node7') | ||
@@ -32,20 +32,10 @@ /* | ||
*/ | ||
const validator = require('har-validator/lib/node6') | ||
const validate = require('har-validator/lib/node6') | ||
/* | ||
* Node 4 (Default) | ||
* Note: additional ES2015 polyfills may be required | ||
*/ | ||
var validator = require('har-validator') | ||
var validate = require('har-validator') | ||
``` | ||
```bash | ||
# to use in cli | ||
npm install --global har-validator | ||
# to use as a module | ||
npm install --save har-validator | ||
``` | ||
## CLI Usage | ||
@@ -68,5 +58,5 @@ | ||
```shell | ||
har-validator har.json | ||
$ har-validator har.json | ||
har-validator --schema request request.json | ||
$ har-validator --schema=request request.json | ||
``` | ||
@@ -73,0 +63,0 @@ |
@@ -1,4 +0,4 @@ | ||
import * as schemas from './schemas' | ||
import * as schemas from 'har-schema' | ||
import Ajv from 'ajv' | ||
import HARError from './error' | ||
import JSONValidator from 'is-my-json-valid' | ||
@@ -10,9 +10,10 @@ export function validator (schema, data = {}, cb) { | ||
// validator config | ||
let validate = JSONValidator(schema, { | ||
greedy: true, | ||
verbose: true, | ||
let ajv = new Ajv({ | ||
allErrors: true, | ||
schemas: schemas | ||
}) | ||
// execute is-my-json-valid | ||
let validate = ajv.compile(schema) | ||
// execute validation | ||
valid = validate(data) | ||
@@ -28,3 +29,3 @@ | ||
export default function har (data, cb) { | ||
export function har (data, cb) { | ||
return validator(schemas.har, data, cb) | ||
@@ -31,0 +32,0 @@ } |
#!/usr/bin/env node | ||
import * as validate from './promise' | ||
import chalk from 'chalk' | ||
import cmd from 'commander' | ||
import program from 'commander' | ||
import fs from 'fs' | ||
import HARError from './error' | ||
import path from 'path' | ||
import pkg from '../package.json' | ||
import validate, * as schemas from './promise' | ||
cmd | ||
.version(pkg.version) | ||
program | ||
.usage('[options] <files...>') | ||
@@ -17,7 +15,9 @@ .option('-s, --schema [name]', 'validate schema name (log, request, response, etc ...)') | ||
if (!cmd.args.length) { | ||
cmd.help() | ||
if (!program.args.length) { | ||
program.help() | ||
} | ||
cmd.args.map((fileName) => { | ||
let schema = program.schema || 'har' | ||
program.args.map((fileName) => { | ||
let file = chalk.yellow.italic(path.basename(fileName)) | ||
@@ -30,16 +30,18 @@ | ||
.then(JSON.parse) | ||
.then(cmd.schema && cmd.schema !== 'har' ? schemas[cmd.schema] : validate) | ||
.then((data) => console.log('%s [%s] is valid', chalk.green('✓'), file)) | ||
.then(validate[schema]) | ||
.then((data) => console.log('%s [%s] is a valid %s', chalk.green('✓'), file, chalk.magenta(schema))) | ||
.catch((err) => { | ||
console.error('[%s]', file) | ||
if (err instanceof SyntaxError) { | ||
return console.error('%s [%s] failed to read JSON: %s', chalk.red('✖'), file, chalk.red(err.message)) | ||
return console.error('%s failed to read JSON: %s', chalk.red('✖'), chalk.red(err.message)) | ||
} | ||
if (err instanceof HARError) { | ||
err.errors.forEach((details) => console.error('%s [%s] failed validation: (%s: %s) %s', chalk.red('✖'), file, chalk.cyan.italic(details.field), chalk.magenta.italic(details.value), chalk.red(details.message))) | ||
err.errors.forEach((details) => console.error('%s validation failed on %s: %s', chalk.red('✖'), chalk.cyan.italic(details.dataPath || '.'), chalk.red(details.message))) | ||
return | ||
} | ||
console.error('%s [%s] an unknown error has occured: %s', chalk.red('✖'), file, chalk.red(err.message)) | ||
console.error('%s an unknown error has occured: %s', chalk.red('✖'), chalk.red(err.message)) | ||
}) | ||
}) |
export default function HARError (errors) { | ||
this.name = 'HARError' | ||
let message = 'validation failed' | ||
this.name = this.constructor.name | ||
this.message = message | ||
this.errors = errors | ||
if (typeof Error.captureStackTrace === 'function') { | ||
Error.captureStackTrace(this, this.constructor) | ||
} else { | ||
this.stack = (new Error(message)).stack | ||
} | ||
} | ||
HARError.prototype = Error.prototype |
@@ -1,4 +0,4 @@ | ||
import * as schemas from './schemas' | ||
import * as schemas from 'har-schema' | ||
import Ajv from 'ajv' | ||
import HARError from './error' | ||
import JSONValidator from 'is-my-json-valid' | ||
@@ -8,9 +8,10 @@ export function validator (schema, data = {}) { | ||
// validator config | ||
let validate = JSONValidator(schema, { | ||
greedy: true, | ||
verbose: true, | ||
let ajv = new Ajv({ | ||
allErrors: true, | ||
schemas: schemas | ||
}) | ||
// execute is-my-json-valid | ||
let validate = ajv.compile(schema) | ||
// execute validation | ||
validate(data) | ||
@@ -22,3 +23,3 @@ | ||
export default function har (data) { | ||
export function har (data) { | ||
return validator(schemas.har, data) | ||
@@ -25,0 +26,0 @@ } |
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
41823
4
23
1079
94
+ Addedajv@^4.9.0
+ Addedhar-schema@^0.1.0
+ Addedajv@4.11.8(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedco@4.6.0(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhar-schema@0.1.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedjson-stable-stringify@1.1.1(transitive)
+ Addedjsonify@0.0.1(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedset-function-length@1.2.2(transitive)
- Removedis-my-json-valid@^2.15.0
- Removedgenerate-function@2.3.1(transitive)
- Removedgenerate-object-property@1.2.0(transitive)
- Removedis-my-ip-valid@1.0.1(transitive)
- Removedis-my-json-valid@2.20.6(transitive)
- Removedis-property@1.0.2(transitive)
- Removedjsonpointer@5.0.1(transitive)
- Removedxtend@4.0.2(transitive)