Browse Source

simplified booleans

tags/v1.0.0
ojizero 1 year ago
parent
commit
e30eb81845
No account linked to committer's email address
2 changed files with 40 additions and 13 deletions
  1. 6
    5
      src/simplified-joi.ts
  2. 34
    8
      test/simplified-joi.spec.ts

+ 6
- 5
src/simplified-joi.ts View File

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

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') {

+ 34
- 8
test/simplified-joi.spec.ts View File

@@ -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', () => {
.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