Files
shields/core/base-service/validate.spec.js
T
chris48sandGitHub c762d971b4 upgrade Joi and related packages (#4060)
* update dependencies

* (core) children --> keys

* (core) fix/update BaseService validate

* (core) update error messages in tests

* (core) only Joi.attempt if we've got a Joi schema

* (core) allow 'expected' to be a regex

* (services) pass 2 schema to .alternatives()

* (services) functions --> Joi schema

* (services) update expected error message

* (services) explicit check for color: undefined

* re-bump joi

* (services) wrap another regex

* (core/services) remove use of array arguments

* (core/services) when --> conditional

* (services) remove more array arguments

* fix spelling in var name

* DRY up sonar helper
2019-09-23 17:36:56 +01:00

128 lines
3.5 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { expect } = require('chai')
const sinon = require('sinon')
const trace = require('./trace')
const { InvalidParameter } = require('./errors')
const validate = require('./validate')
describe('validate', function() {
const schema = Joi.object({
requiredString: Joi.string().required(),
}).required()
let sandbox
beforeEach(function() {
sandbox = sinon.createSandbox()
})
afterEach(function() {
sandbox.restore()
})
beforeEach(function() {
sandbox.stub(trace, 'logTrace')
})
const ErrorClass = InvalidParameter
const prettyErrorMessage = 'parameter does not match schema'
const traceErrorMessage = 'Params did not match schema'
const traceSuccessMessage = 'Params after validation'
const options = {
ErrorClass,
prettyErrorMessage,
traceErrorMessage,
traceSuccessMessage,
}
context('schema is not provided', function() {
it('throws the expected programmer error', function() {
try {
validate(options, { requiredString: 'bar' }, undefined)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Error)
expect(e.message).to.equal('A Joi schema is required')
}
})
})
context('data matches schema', function() {
it('logs the data', function() {
validate(options, { requiredString: 'bar' }, schema)
expect(trace.logTrace).to.be.calledWithMatch(
'validate',
sinon.match.string,
traceSuccessMessage,
{ requiredString: 'bar' },
{ deep: true }
)
})
})
context('data does not match schema', function() {
it('logs the data and throws the expected error', function() {
try {
validate(
options,
{ requiredString: ['this', "shouldn't", 'work'] },
schema
)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidParameter)
expect(e.message).to.equal(
'Invalid Parameter: "requiredString" must be a string'
)
expect(e.prettyMessage).to.equal(prettyErrorMessage)
}
expect(trace.logTrace).to.be.calledWithMatch(
'validate',
sinon.match.string,
traceErrorMessage,
'"requiredString" must be a string'
)
})
context('with includeKeys: true', function() {
it('includes keys in the error text', function() {
try {
validate(
{ ...options, includeKeys: true },
{
requiredString: ['this', "shouldn't", 'work'],
},
schema
)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidParameter)
expect(e.message).to.equal(
'Invalid Parameter: "requiredString" must be a string'
)
expect(e.prettyMessage).to.equal(
`${prettyErrorMessage}: requiredString`
)
}
})
})
})
it('allowAndStripUnknownKeys', function() {
try {
validate(
{ ...options, allowAndStripUnknownKeys: false, includeKeys: true },
{ requiredString: 'bar', extra: 'nonsense', more: 'bogus' },
schema
)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidParameter)
expect(e.message).to.equal(
'Invalid Parameter: "extra" is not allowed. "more" is not allowed'
)
expect(e.prettyMessage).to.equal(`${prettyErrorMessage}: extra, more`)
}
})
})