diff --git a/README.md b/README.md index 3a72513..52e5407 100644 --- a/README.md +++ b/README.md @@ -348,11 +348,11 @@ This is still a work in progress :D any help is appreciated - [x] More on external API - [ ] Support non JSON payload - [ ] Get `onError: resolve` to work -- [ ] Support simplified form for validation +- [x] Support simplified form for validation - [x] Finalize behvaiour of genreator method functions - [x] What to do with their arguments (ambiguity of options/payload/querystring in args) - [x] Document RouteFunction behvaiour -- [ ] ... +- [ ] Browser support through Ky ? ## License diff --git a/test/simplified-joi.spec.ts b/test/simplified-joi.spec.ts index 98ed3c1..53aad63 100644 --- a/test/simplified-joi.spec.ts +++ b/test/simplified-joi.spec.ts @@ -1,19 +1,49 @@ /// +import Joi from 'joi' 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('passes on `string` type', () => { + const joiString = Joi.string().required() + + expect(transformSchema('string')) + .to.deep.equal(joiString) + }) + + it('passes on `string|notrequired` type', () => { + const joiString = Joi.string() + + expect(transformSchema('string|notrequired')) + .to.deep.equal(joiString) + }) + + it('passes on `number` type', () => { + const joiNumber = Joi.number().required() + + expect(transformSchema('number')) + .to.deep.equal(joiNumber) }) - it.skip('should pass on `number` type', () => { - // + it('passes on `number|notrequired` type', () => { + const joiNumber = Joi.number() + + expect(transformSchema('number|notrequired')) + .to.deep.equal(joiNumber) }) - it.skip('should pass on `symbol` type', () => { - // + it('passes on `symbol` type', () => { + const joiSymbol = Joi.symbol().required() + + expect(transformSchema('symbol')) + .to.deep.equal(joiSymbol) + }) + + it('passes on `symbol|notrequired` type', () => { + const joiSymbol = Joi.symbol() + + expect(transformSchema('symbol|notrequired')) + .to.deep.equal(joiSymbol) }) it('fails on undefined types', () => { @@ -21,16 +51,36 @@ describe('Transform simplified validation schema to Joi schemas', () => { .to.throw() }) - it.skip('transforms array schemas', () => { - // - }) + it('transforms array schemas', () => { + const joiArray = Joi.array().ordered( + Joi.string().required(), + Joi.number().required(), + Joi.number(), + Joi.string(), + ).required() - it.skip('transforms object schemas', () => { - // + expect(transformSchema([ + 'string', + 'number', + 'number|notrequired', + 'string|notrequired', + ])) + .to.deep.equal(joiArray) }) - it.skip('passes `notrequired` option to schemas', () => { - // + it('transforms object schemas', () => { + const joiObject = Joi.object({ + numberKey: Joi.number().required(), + stringOptional: Joi.string(), + someArray: Joi.array().ordered(Joi.string().required(), Joi.number()).required(), + }).required() + + expect(transformSchema({ + numberKey: 'number', + stringOptional: 'string|notrequired', + someArray: ['string', 'number|notrequired'], + })) + .to.deep.equal(joiObject) }) it('passes `raw` option to schemas', () => { diff --git a/test/validation.spec.ts b/test/validation.spec.ts index e750af3..9f3f3b7 100644 --- a/test/validation.spec.ts +++ b/test/validation.spec.ts @@ -14,6 +14,10 @@ const customValidator = { } } +const simplifiedSchema = { + correctKey: 'string', +} + const validData = { correctKey: 'correctValue' } @@ -47,13 +51,15 @@ describe('Ensure valid data', () => { }) }) - describe.skip('Using simplified schemas', () => { + describe('Using simplified schemas', () => { it('passes valid data', () => { - // + expect(() => ensureValidData(simplifiedSchema, validData)) + .to.not.throw() }) it('rejects invalid data', () => { - // + expect(() => ensureValidData(simplifiedSchema, invalidData)) + .to.throw() }) }) })