Browse Source

implemented simplified validation (test remaining)

modified-method-api
ojizero 4 years ago
parent
commit
e02ce4630d
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 4
      src/method.ts
  2. 58
      src/simplified-joi.ts
  3. 13
      src/validation.ts
  4. 4
      test/method.spec.ts
  5. 50
      test/simplified-joi.spec.ts
  6. 12
      test/validation.spec.ts

4
src/method.ts

@ -6,8 +6,8 @@ import { @@ -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
src/simplified-joi.ts

@ -0,0 +1,58 @@ @@ -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

13
src/validation.ts

@ -1,4 +1,5 @@ @@ -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 { @@ -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: @@ -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: @@ -25,5 +34,3 @@ export function ensureValidData (spec: SchemaLike | Validator | undefined, data:
}
export { SchemaLike }
export type ValdiationSpec = SchemaLike | Validator

4
test/method.spec.ts

@ -39,9 +39,9 @@ const mockGetMethodWithQueryString: MethodSpec = { @@ -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
test/simplified-joi.spec.ts

@ -0,0 +1,50 @@ @@ -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)
})
})

12
test/validation.spec.ts

@ -22,7 +22,7 @@ const invalidData = { @@ -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', () => { @@ -46,4 +46,14 @@ describe('ensureValidData', () => {
.to.throw()
})
})
describe.skip('Using simplified schemas', () => {
it('passes valid data', () => {
//
})
it('rejects invalid data', () => {
//
})
})
})

Loading…
Cancel
Save