From 80cb151c193bd0a5005de6fec9273fb30c4fc2a7 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Fri, 17 Nov 2023 15:52:54 +0100 Subject: [PATCH] Rework tests to not use @hapi/joi@15 --- package.json | 1 - test/validation.js | 30 +++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 802aa3046..88ec1eed4 100755 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "@hapi/code": "^9.0.3", "@hapi/eslint-plugin": "^6.0.0", "@hapi/inert": "^7.1.0", - "@hapi/joi-legacy-test": "npm:@hapi/joi@^15.0.0", "@hapi/lab": "^25.3.2", "@hapi/vision": "^7.0.3", "@hapi/wreck": "^18.1.0", diff --git a/test/validation.js b/test/validation.js index b4e363ff0..05b45d4ed 100755 --- a/test/validation.js +++ b/test/validation.js @@ -5,7 +5,6 @@ const Code = require('@hapi/code'); const Hapi = require('..'); const Inert = require('@hapi/inert'); const Joi = require('joi'); -const JoiLegacy = require('@hapi/joi-legacy-test'); const Lab = require('@hapi/lab'); @@ -18,20 +17,37 @@ const expect = Code.expect; describe('validation', () => { - it('validates using joi v15', async () => { + it('validates using custom validator', async () => { + + const Validator = class { + static compile(schema) { + + if (schema.a === 'is-number') { + return { + validate(value, options) { + + if (parseInt(value?.a, 10).toString() === value?.a.toString()) { + return { value }; + } + + throw new Error('Validation failed'); + } + }; + } + } + }; const server = Hapi.server(); - server.validator(JoiLegacy); + server.validator(Validator); server.route({ method: 'POST', path: '/', handler: () => 'ok', options: { validate: { - payload: JoiLegacy.object({ - a: JoiLegacy.number(), - b: JoiLegacy.array() - }) + payload: { + a: 'is-number' + } } } });