asarfuckery/appasar/canary/node_modules/har-validator/lib/promise.js
2019-01-17 19:00:12 +01:00

95 lines
1.7 KiB
JavaScript

var Ajv = require('ajv')
var HARError = require('./error')
var schemas = require('har-schema')
var ajv
function validate (name, data) {
data = data || {}
// validator config
ajv = ajv || new Ajv({
allErrors: true,
schemas: schemas
})
var validate = ajv.getSchema(name + '.json')
return new Promise(function (resolve, reject) {
var valid = validate(data)
!valid ? reject(new HARError(validate.errors)) : resolve(data)
})
}
exports.afterRequest = function (data) {
return validate('afterRequest', data)
}
exports.beforeRequest = function (data) {
return validate('beforeRequest', data)
}
exports.browser = function (data) {
return validate('browser', data)
}
exports.cache = function (data) {
return validate('cache', data)
}
exports.content = function (data) {
return validate('content', data)
}
exports.cookie = function (data) {
return validate('cookie', data)
}
exports.creator = function (data) {
return validate('creator', data)
}
exports.entry = function (data) {
return validate('entry', data)
}
exports.har = function (data) {
return validate('har', data)
}
exports.header = function (data) {
return validate('header', data)
}
exports.log = function (data) {
return validate('log', data)
}
exports.page = function (data) {
return validate('page', data)
}
exports.pageTimings = function (data) {
return validate('pageTimings', data)
}
exports.postData = function (data) {
return validate('postData', data)
}
exports.query = function (data) {
return validate('query', data)
}
exports.request = function (data) {
return validate('request', data)
}
exports.response = function (data) {
return validate('response', data)
}
exports.timings = function (data) {
return validate('timings', data)
}