-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
73 lines (56 loc) · 1.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict'
var schemas = require('./schemas')
var validator = require('is-my-json-valid')
function ValidationError (errors) {
this.name = 'ValidationError'
this.errors = errors
}
ValidationError.prototype = Error.prototype
// is-my-json-valid does not provide meaningful error messages for external schemas
// this is a workaround
schemas.cache.properties.beforeRequest = schemas.cacheEntry
schemas.cache.properties.afterRequest = schemas.cacheEntry
schemas.page.properties.pageTimings = schemas.pageTimings
schemas.request.properties.cookies.items = schemas.cookie
schemas.request.properties.headers.items = schemas.record
schemas.request.properties.queryString.items = schemas.record
schemas.request.properties.postData = schemas.postData
schemas.response.properties.cookies.items = schemas.cookie
schemas.response.properties.headers.items = schemas.record
schemas.response.properties.content = schemas.content
schemas.entry.properties.request = schemas.request
schemas.entry.properties.response = schemas.response
schemas.entry.properties.cache = schemas.cache
schemas.entry.properties.timings = schemas.timings
schemas.log.properties.creator = schemas.creator
schemas.log.properties.browser = schemas.creator
schemas.log.properties.pages.items = schemas.page
schemas.log.properties.entries.items = schemas.entry
schemas.har.properties.log = schemas.log
var runner = function (schema, data, cb) {
var validate = validator(schema, {
greedy: true,
verbose: true,
schemas: schemas
})
var valid = false
if (data !== undefined) {
// execute is-my-json-valid
valid = validate(data)
}
// callback?
if (!cb) {
return validate.errors ? false : true
} else {
return cb(validate.errors ? new ValidationError(validate.errors) : null, valid)
}
return valid
}
module.exports = function (data, cb) {
return runner(schemas.har, data, cb)
}
Object.keys(schemas).map(function (name) {
module.exports[name] = function (data, cb) {
return runner(schemas[name], data, cb)
}
})