UNPKG

2.18 kBJavaScriptView Raw
1var Ajv = require('ajv')
2var HARError = require('./error')
3var schemas = require('har-schema')
4
5var ajv
6
7function createAjvInstance () {
8 var ajv = new Ajv({
9 allErrors: true
10 })
11 ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'))
12 ajv.addSchema(schemas)
13
14 return ajv
15}
16
17function validate (name, data, next) {
18 data = data || {}
19
20 // validator config
21 ajv = ajv || createAjvInstance()
22
23 var validate = ajv.getSchema(name + '.json')
24
25 var valid = validate(data)
26
27 // callback?
28 if (typeof next === 'function') {
29 return next(!valid ? new HARError(validate.errors) : null, valid)
30 }
31
32 return valid
33}
34
35exports.afterRequest = function (data, next) {
36 return validate('afterRequest', data, next)
37}
38
39exports.beforeRequest = function (data, next) {
40 return validate('beforeRequest', data, next)
41}
42
43exports.browser = function (data, next) {
44 return validate('browser', data, next)
45}
46
47exports.cache = function (data, next) {
48 return validate('cache', data, next)
49}
50
51exports.content = function (data, next) {
52 return validate('content', data, next)
53}
54
55exports.cookie = function (data, next) {
56 return validate('cookie', data, next)
57}
58
59exports.creator = function (data, next) {
60 return validate('creator', data, next)
61}
62
63exports.entry = function (data, next) {
64 return validate('entry', data, next)
65}
66
67exports.har = function (data, next) {
68 return validate('har', data, next)
69}
70
71exports.header = function (data, next) {
72 return validate('header', data, next)
73}
74
75exports.log = function (data, next) {
76 return validate('log', data, next)
77}
78
79exports.page = function (data, next) {
80 return validate('page', data, next)
81}
82
83exports.pageTimings = function (data, next) {
84 return validate('pageTimings', data, next)
85}
86
87exports.postData = function (data, next) {
88 return validate('postData', data, next)
89}
90
91exports.query = function (data, next) {
92 return validate('query', data, next)
93}
94
95exports.request = function (data, next) {
96 return validate('request', data, next)
97}
98
99exports.response = function (data, next) {
100 return validate('response', data, next)
101}
102
103exports.timings = function (data, next) {
104 return validate('timings', data, next)
105}