Browse Source

validation unit tests

tags/v1.0.0-alpha.0
ojizero 1 year ago
parent
commit
b472331a02
No account linked to committer's email address
2 changed files with 50 additions and 1 deletions
  1. 1
    1
      src/validation.ts
  2. 49
    0
      test/validation.spec.ts

+ 1
- 1
src/validation.ts View File

@@ -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
- 0
test/validation.spec.ts View File

@@ -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