unexpected icon indicating copy to clipboard operation
unexpected copied to clipboard

unexpected-json-schema

Open whatyoubendoing opened this issue 9 years ago • 5 comments

Below works

import expect from 'unexpected'
import tv4 from 'tv4'
import fs from 'fs'

expect.addAssertion('<any> [not] to match json schema <string>', (expect, subject, path) => {
  return expect.promise((run) => {
    fs.readFile(`${__dirname}/schema/${path}.json`, { encoding: 'utf8' }, run((err, data) => {
      if (err) throw err
      const result = tv4.validate(subject, JSON.parse(data))
      expect.subjectOutput = function (output) {
        output.text(tv4.error.schemaPath)
      }
      expect.argsOutput = function (output) {
        output.text(tv4.error.message)
      }
      return expect(result, '[not] to be true')
    }))
  })
})

whatyoubendoing avatar Dec 15 '15 22:12 whatyoubendoing

That looks really sweet! You should turn that into a plugin.

papandreou avatar Dec 16 '15 09:12 papandreou

I wonder if it would be possible to have a json schema to unexpected assertion converter, which would add all the benefits of diffing and error messages from unexpected

Munter avatar Dec 16 '15 10:12 Munter

@Munter That's a great idea. The tv4 implementation seems rather involved, though, so it's probably not an easy task.

papandreou avatar Dec 16 '15 12:12 papandreou

@b3njamin awesome idea.

sunesimonsen avatar Dec 27 '15 21:12 sunesimonsen

@b3njamin do you want one of use to make the plugin, or do you want the honour youself?

sunesimonsen avatar Dec 27 '15 21:12 sunesimonsen