You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.1 KiB

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