1 | var Ajv = require('ajv')
|
---|
2 | var HARError = require('./error')
|
---|
3 | var schemas = require('har-schema')
|
---|
4 |
|
---|
5 | var ajv
|
---|
6 |
|
---|
7 | function 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 |
|
---|
17 | function 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 |
|
---|
35 | exports.afterRequest = function (data, next) {
|
---|
36 | return validate('afterRequest', data, next)
|
---|
37 | }
|
---|
38 |
|
---|
39 | exports.beforeRequest = function (data, next) {
|
---|
40 | return validate('beforeRequest', data, next)
|
---|
41 | }
|
---|
42 |
|
---|
43 | exports.browser = function (data, next) {
|
---|
44 | return validate('browser', data, next)
|
---|
45 | }
|
---|
46 |
|
---|
47 | exports.cache = function (data, next) {
|
---|
48 | return validate('cache', data, next)
|
---|
49 | }
|
---|
50 |
|
---|
51 | exports.content = function (data, next) {
|
---|
52 | return validate('content', data, next)
|
---|
53 | }
|
---|
54 |
|
---|
55 | exports.cookie = function (data, next) {
|
---|
56 | return validate('cookie', data, next)
|
---|
57 | }
|
---|
58 |
|
---|
59 | exports.creator = function (data, next) {
|
---|
60 | return validate('creator', data, next)
|
---|
61 | }
|
---|
62 |
|
---|
63 | exports.entry = function (data, next) {
|
---|
64 | return validate('entry', data, next)
|
---|
65 | }
|
---|
66 |
|
---|
67 | exports.har = function (data, next) {
|
---|
68 | return validate('har', data, next)
|
---|
69 | }
|
---|
70 |
|
---|
71 | exports.header = function (data, next) {
|
---|
72 | return validate('header', data, next)
|
---|
73 | }
|
---|
74 |
|
---|
75 | exports.log = function (data, next) {
|
---|
76 | return validate('log', data, next)
|
---|
77 | }
|
---|
78 |
|
---|
79 | exports.page = function (data, next) {
|
---|
80 | return validate('page', data, next)
|
---|
81 | }
|
---|
82 |
|
---|
83 | exports.pageTimings = function (data, next) {
|
---|
84 | return validate('pageTimings', data, next)
|
---|
85 | }
|
---|
86 |
|
---|
87 | exports.postData = function (data, next) {
|
---|
88 | return validate('postData', data, next)
|
---|
89 | }
|
---|
90 |
|
---|
91 | exports.query = function (data, next) {
|
---|
92 | return validate('query', data, next)
|
---|
93 | }
|
---|
94 |
|
---|
95 | exports.request = function (data, next) {
|
---|
96 | return validate('request', data, next)
|
---|
97 | }
|
---|
98 |
|
---|
99 | exports.response = function (data, next) {
|
---|
100 | return validate('response', data, next)
|
---|
101 | }
|
---|
102 |
|
---|
103 | exports.timings = function (data, next) {
|
---|
104 | return validate('timings', data, next)
|
---|
105 | }
|
---|