Browse Source

validation unit tests

modified-method-api
ojizero 4 years ago
parent
commit
b472331a02
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 2
      src/validation.ts
  2. 49
      test/validation.spec.ts

2
src/validation.ts

@ -8,7 +8,7 @@ function isCustomValidator (spec: any): spec is Validator { @@ -8,7 +8,7 @@ function isCustomValidator (spec: any): spec is Validator {
return !!spec && !spec.isJoi && 'validate' in spec
}
export function ensureValidData (spec: SchemaLike | Validator | undefined, data: any[]) {
export function ensureValidData (spec: SchemaLike | Validator | undefined, data: any) {
if (!spec) return
if (isCustomValidator(spec)) {

49
test/validation.spec.ts

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
/// <reference path='typings/globals.d.ts' />
import { ensureValidData } from '../src/validation'
import Joi, { valid } from 'joi'
const joiSchema = Joi.object({
correctKey: Joi.strict().required()
}).required()
const customValidator = {
validate (data) {
return 'correctKey' in data
}
}
const validData = {
correctKey: 'correctValue'
}
const invalidData = {
incorrectKey: 'some value',
}
describe('ensureValidData', () => {
describe('Using Joi schemas', () => {
it('passes valid data', () => {
expect(() => ensureValidData(joiSchema, validData))
.to.not.throw()
})
it('rejects invalid data', () => {
expect(() => ensureValidData(joiSchema, invalidData))
.to.throw()
})
})
describe('Using custom validators', () => {
it('passes valid data', () => {
expect(() => ensureValidData(customValidator, validData))
.to.not.throw()
})
it('rejects invalid data', () => {
expect(() => ensureValidData(customValidator, invalidData))
.to.throw()
})
})
})
Loading…
Cancel
Save