Browse Source

implemented simplified validation (test remaining)

tags/v1.0.0-alpha.2
ojizero 1 year ago
parent
commit
e02ce4630d
No account linked to committer's email address
6 changed files with 133 additions and 8 deletions
  1. 2
    2
      src/method.ts
  2. 58
    0
      src/simplified-joi.ts
  3. 10
    3
      src/validation.ts
  4. 2
    2
      test/method.spec.ts
  5. 50
    0
      test/simplified-joi.spec.ts
  6. 11
    1
      test/validation.spec.ts

+ 2
- 2
src/method.ts View File

@@ -6,8 +6,8 @@ import {
stringify as stringifyQuery,
} from 'querystring'

import { ValdiationSpec, ensureValidData } from './validation';
import { OutgoingHttpHeaders } from 'http';
import { OutgoingHttpHeaders } from 'http'
import { ValdiationSpec, ensureValidData } from './validation'

const applicationJson = 'application/json'


+ 58
- 0
src/simplified-joi.ts View File

@@ -0,0 +1,58 @@
import Joi, { SchemaLike } from 'joi'

const JOI_MAPPING: { [k: string]: SchemaLike } = {
string: Joi.string(),
number: Joi.number(),
symbol: Joi.symbol(),
object: Joi.object(),
array: Joi.array(),
}

function isObject (value: any): value is { [k: string]: any } {
return !!value && typeof value === 'object'
}

function isArray (value: any): value is Array<any> {
return !!value && Array.isArray(value)
}

function isJoiSchema (value: any): value is SchemaLike {
return !!value && value.isJoi
}

export function transformSchema (schema: any): SchemaLike {
if (isJoiSchema(schema)) return schema

if (isArray(schema)) {
schema = schema.map(transformSchema)

return Joi.array().ordered(schema).required()
}

if (isObject(schema)) {
schema = Object.entries(schema)
.reduce((acc, [key, value]) => ({
...acc,
[key]: transformSchema(value),
}), {})

return Joi.object(schema).required()
}

const [schemaName, schemaOptions = ''] = (schema as string).split('|', 2)

const isRaw = /raw/i
const isNotRequired = /notrequired/i

if (isRaw.test(schemaOptions)) return schemaName

schema = JOI_MAPPING[schemaName]

if (typeof schema === 'undefined') {
throw new Error(`Requested Joi validator ${schema} unsupported or undefined.`)
}

return isNotRequired.test(schemaOptions) ? schema : schema.required()
}

export default transformSchema

+ 10
- 3
src/validation.ts View File

@@ -1,4 +1,5 @@
import { SchemaLike, validate } from 'joi'
import transformSchema from './simplified-joi'

export interface Validator {
validate (data: any[] | {}): boolean
@@ -8,7 +9,13 @@ function isCustomValidator (spec: any): spec is Validator {
return !!spec && !spec.isJoi && 'validate' in spec
}

export function ensureValidData (spec: SchemaLike | Validator | undefined, data: any, name?: string) {
function isJoiSchema (spec: any): spec is SchemaLike {
return !!spec && spec.isJoi
}

export type ValdiationSpec = SchemaLike | Validator

export function ensureValidData (spec: ValdiationSpec | undefined, data: any, name?: string) {
if (!spec) return

if (isCustomValidator(spec)) {
@@ -17,6 +24,8 @@ export function ensureValidData (spec: SchemaLike | Validator | undefined, data:
if (valid) return
}

if (!isJoiSchema(spec)) spec = transformSchema(spec)

const { error } = validate(data, spec)

if (!error) return
@@ -25,5 +34,3 @@ export function ensureValidData (spec: SchemaLike | Validator | undefined, data:
}

export { SchemaLike }

export type ValdiationSpec = SchemaLike | Validator

+ 2
- 2
test/method.spec.ts View File

@@ -39,9 +39,9 @@ const mockGetMethodWithQueryString: MethodSpec = {
path: '/mock-path/:param',
accept: 'mock/type',
contentType: 'mock/type',
queryString: {
queryString: Joi.object({
some_arg: Joi.string().required()
}
}),
}

describe('Method', async () => {

+ 50
- 0
test/simplified-joi.spec.ts View File

@@ -0,0 +1,50 @@
/// <reference path='typings/globals.d.ts' />

import transformSchema from '../src/simplified-joi'
import Joi from 'joi';

describe('Transform simplified validation schema to Joi schemas', () => {
it.skip('should pass on `string` type', () => {
//
})

it.skip('should pass on `number` type', () => {
//
})

it.skip('should pass on `symbol` type', () => {
//
})

it('fails on undefined types', () => {
expect(() => transformSchema('wubalubadubdub'))
.to.throw()
})

it.skip('transforms array schemas', () => {
//
})

it.skip('transforms object schemas', () => {
//
})

it.skip('passes `notrequired` option to schemas', () => {
//
})

it('passes `raw` option to schemas', () => {
const value = 'something'
const rawSchema = `${value}|raw`

expect(transformSchema(rawSchema))
.to.deep.equal(value)
})

it('returns Joi schema unmodified if passed', () => {
const j = Joi.object().required()

expect(transformSchema(j))
.to.deep.equal(j)
})
})

+ 11
- 1
test/validation.spec.ts View File

@@ -22,7 +22,7 @@ const invalidData = {
incorrectKey: 'some value',
}

describe('ensureValidData', () => {
describe('Ensure valid data', () => {
describe('Using Joi schemas', () => {
it('passes valid data', () => {
expect(() => ensureValidData(joiSchema, validData))
@@ -46,4 +46,14 @@ describe('ensureValidData', () => {
.to.throw()
})
})

describe.skip('Using simplified schemas', () => {
it('passes valid data', () => {
//
})

it('rejects invalid data', () => {
//
})
})
})

Loading…
Cancel
Save