async.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var Ajv = require('ajv')
  2. var HARError = require('./error')
  3. var schemas = require('har-schema')
  4. var ajv
  5. function validate (name, data, next) {
  6. data = data || {}
  7. // validator config
  8. ajv = ajv || new Ajv({
  9. allErrors: true,
  10. schemas: schemas
  11. })
  12. var validate = ajv.getSchema(name + '.json')
  13. var valid = validate(data)
  14. // callback?
  15. if (typeof next === 'function') {
  16. return next(!valid ? new HARError(validate.errors) : null, valid)
  17. }
  18. return valid
  19. }
  20. exports.afterRequest = function (data, next) {
  21. return validate('afterRequest', data, next)
  22. }
  23. exports.beforeRequest = function (data, next) {
  24. return validate('beforeRequest', data, next)
  25. }
  26. exports.browser = function (data, next) {
  27. return validate('browser', data, next)
  28. }
  29. exports.cache = function (data, next) {
  30. return validate('cache', data, next)
  31. }
  32. exports.content = function (data, next) {
  33. return validate('content', data, next)
  34. }
  35. exports.cookie = function (data, next) {
  36. return validate('cookie', data, next)
  37. }
  38. exports.creator = function (data, next) {
  39. return validate('creator', data, next)
  40. }
  41. exports.entry = function (data, next) {
  42. return validate('entry', data, next)
  43. }
  44. exports.har = function (data, next) {
  45. return validate('har', data, next)
  46. }
  47. exports.header = function (data, next) {
  48. return validate('header', data, next)
  49. }
  50. exports.log = function (data, next) {
  51. return validate('log', data, next)
  52. }
  53. exports.page = function (data, next) {
  54. return validate('page', data, next)
  55. }
  56. exports.pageTimings = function (data, next) {
  57. return validate('pageTimings', data, next)
  58. }
  59. exports.postData = function (data, next) {
  60. return validate('postData', data, next)
  61. }
  62. exports.query = function (data, next) {
  63. return validate('query', data, next)
  64. }
  65. exports.request = function (data, next) {
  66. return validate('request', data, next)
  67. }
  68. exports.response = function (data, next) {
  69. return validate('response', data, next)
  70. }
  71. exports.timings = function (data, next) {
  72. return validate('timings', data, next)
  73. }