Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error messages #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Initial custom errors implementation
bergie committed Nov 23, 2016
commit 70bd389b3ccd99bc0b4795d05132b1d52a34a4b3
13 changes: 9 additions & 4 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@

yaml = require 'js-yaml'
path = require 'path'
fs = require 'fs'

path = require 'path'
fs = require 'fs'

schemaPath = './schema'

exports.listSchemas = () ->
@@ -25,3 +21,12 @@ exports.getExamples = (name) ->
filepath = path.join __dirname, './examples', name+'.yml'
content = fs.readFileSync filepath, { encoding: 'utf-8' }
return yaml.safeLoad content

exports.enableCustomErrors = (validator) ->
unless typeof validator.setErrorReporter is 'function'
throw 'Your validator doesn\'t support custom error messages'
validator.setErrorReporter (error, data, schema) ->
return error.message unless schema.messages
lastSchemaPath = error.schemaPath.split('/').pop()
return error.message unless schema.messages[lastSchemaPath]
schema.messages[lastSchemaPath]
12 changes: 12 additions & 0 deletions schemata/base.yaml
Original file line number Diff line number Diff line change
@@ -9,31 +9,43 @@ definitions:
pattern: "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
example: 01234567-89ab-cdef-0123-456789abcdef
type: string
messages:
pattern: 'Must be a properly-formed UUID'
url:
description: Unique Resource Locator
format: uri
example: http://thegrid.io
type: string
messages:
format: 'Must be a properly-formed URL'
email:
description: Email address
format: email
example: '[email protected]'
type: string
messages:
format: 'Must be a properly-formed email'
hostname:
description: Hostname
format: hostname
example: 'blog.thegrid.io'
type: string
messages:
format: 'Must be a properly-formed hostname'
hexcolor:
description: "#RGB color"
format: rgbhexcolor
pattern: "^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$"
example: "#aa33cc"
type: string
messages:
pattern: 'Must be a RGB hex string'
site:
type: string
example: "the-domains/the-grid"
pattern: "^[a-z0-9-_\\.]+\/[a-z0-9-_\\.]+$"
messages:
pattern: 'Must be a valid GitHub repository name'
sites:
type: array
description: Collection of websites associated with the resource
33 changes: 33 additions & 0 deletions spec/custom-errors.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
tv4 = require 'tv4'
tv4Formats = require 'tv4-formats'
lib = require '../index'
chai = require 'chai' if not chai

describe 'Custom errors', ->
schemas = {}
validator = null
before ->
validator = tv4.freshApi()
validator.addFormat tv4Formats
lib.enableCustomErrors validator
lib.listSchemas().forEach (schemaName) ->
schema = lib.getSchema schemaName
validator.addSchema schema.id, schema
schemas[schemaName] = schema
after ->
validator.dropSchemas()
validator.reset()

describe 'invalid repo name', ->
it 'should produce a custom error', (done) ->
baseSchema = schemas['base']
chai.expect(baseSchema.definitions.site.messages).to.be.an 'object'
chai.expect(baseSchema.definitions.site.messages.pattern).to.exist
result = validator.validateMultiple
name: 'The Grid'
repo: 'foo'
path: '/foo'
, 'site.json'
chai.expect(result.valid).to.equal false
chai.expect(result.errors[0].message).to.equal baseSchema.definitions.site.messages.pattern
done()