Browse Source

simplified booleans

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

11
src/simplified-joi.ts

@ -3,9 +3,13 @@ import Joi, { SchemaLike } from 'joi' @@ -3,9 +3,13 @@ import Joi, { SchemaLike } from 'joi'
const JOI_MAPPING: { [k: string]: SchemaLike } = {
string: Joi.string(),
number: Joi.number(),
bool: Joi.bool(),
boolean: Joi.boolean(),
symbol: Joi.symbol(),
object: Joi.object(),
array: Joi.array(),
// Simply pass schema as object or array for them
// object: Joi.object(),
// array: Joi.array(),
}
function isObject (value: any): value is { [k: string]: any } {
@ -41,11 +45,8 @@ export function transformSchema (schema: any): SchemaLike { @@ -41,11 +45,8 @@ export function transformSchema (schema: any): SchemaLike {
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') {

42
test/simplified-joi.spec.ts

@ -32,6 +32,40 @@ describe('Transform simplified validation schema to Joi schemas', () => { @@ -32,6 +32,40 @@ describe('Transform simplified validation schema to Joi schemas', () => {
.to.deep.equal(joiNumber)
})
it('passes on `bool` and `boolean` type', () => {
const joiBool = Joi.bool().required()
const joiBoolean = Joi.boolean().required()
const simplifiedBool = transformSchema('bool')
const simplifiedBoolean = transformSchema('boolean')
expect(simplifiedBool)
.to.deep.equal(joiBool)
expect(simplifiedBoolean)
.to.deep.equal(joiBoolean)
expect(simplifiedBool)
.to.deep.equal(simplifiedBoolean)
})
it('passes on `bool|notrequired` and `boolean|notrequired` type', () => {
const joiBool = Joi.bool()
const joiBoolean = Joi.boolean()
const simplifiedBool = transformSchema('bool|notrequired')
const simplifiedBoolean = transformSchema('boolean|notrequired')
expect(simplifiedBool)
.to.deep.equal(joiBool)
expect(simplifiedBoolean)
.to.deep.equal(joiBoolean)
expect(simplifiedBool)
.to.deep.equal(simplifiedBoolean)
})
it('passes on `symbol` type', () => {
const joiSymbol = Joi.symbol().required()
@ -83,14 +117,6 @@ describe('Transform simplified validation schema to Joi schemas', () => { @@ -83,14 +117,6 @@ describe('Transform simplified validation schema to Joi schemas', () => {
.to.deep.equal(joiObject)
})
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()

Loading…
Cancel
Save