From 83c2fa712d73bd2bd5f384528bcbb3e591501a2a Mon Sep 17 00:00:00 2001 From: Jiri Hajek Date: Mon, 13 Nov 2023 12:52:48 +0100 Subject: [PATCH 1/4] fix: do not override existing labels of underlying schemas in alternatives --- lib/types/alternatives.js | 6 +- test/types/alternatives.js | 143 +++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 1 deletion(-) diff --git a/lib/types/alternatives.js b/lib/types/alternatives.js index 5fce47ae..d59e7256 100755 --- a/lib/types/alternatives.js +++ b/lib/types/alternatives.js @@ -212,7 +212,11 @@ module.exports = Any.extend({ label(name) { const obj = this.$_parent('label', name); - const each = (item, source) => (source.path[0] !== 'is' ? item.label(name) : undefined); + const each = (item, source) => { + + return source.path[0] !== 'is' && typeof item._flags.label !== 'string' ? item.label(name) : undefined; + }; + return obj.$_modify({ each, ref: false }); } }, diff --git a/test/types/alternatives.js b/test/types/alternatives.js index 2ab96b2e..1a0d15e1 100755 --- a/test/types/alternatives.js +++ b/test/types/alternatives.js @@ -1377,6 +1377,149 @@ describe('alternatives', () => { } }); }); + + it('does not override existing labels in nested alternatives', () => { + + const schema = Joi.alternatives().try( + Joi.boolean().label('boolean'), + Joi.alternatives().try( + Joi.string().label('string'), + Joi.number().label('number') + ).label('string or number') + ); + + expect(schema.describe()).to.equal({ + type: 'alternatives', + matches: [ + { + schema: { + type: 'boolean', + flags: { + label: 'boolean' + } + } + }, + { + schema: { + type: 'alternatives', + flags: { + label: 'string or number' + }, + matches: [ + { + schema: { + type: 'string', + flags: { + label: 'string' + } + } + }, + { + schema: { + type: 'number', + flags: { + label: 'number' + } + } + } + ] + } + } + ] + }); + }); + + it('does not override existing label of then', () => { + + const schema = Joi.object({ + a: Joi.boolean(), + b: Joi.alternatives() + .conditional('a', { is: true, then: Joi.string().label('not x') }) + .label('x') + }); + + expect(schema.describe()).to.equal({ + type: 'object', + keys: { + a: { + type: 'boolean' + }, + b: { + type: 'alternatives', + flags: { + label: 'x' + }, + matches: [ + { + is: { + type: 'any', + allow: [{ override: true }, true], + flags: { + only: true, + presence: 'required' + } + }, + ref: { + path: ['a'] + }, + then: { + type: 'string', + flags: { + label: 'not x' + } + } + } + ] + } + } + }); + }); + + it('does not override existing label of otherwise', () => { + + const schema = Joi.object({ + a: Joi.boolean(), + b: Joi.alternatives() + .conditional('a', { is: true, otherwise: Joi.string().label('not x') }) + .label('x') + }); + + expect(schema.describe()).to.equal({ + type: 'object', + keys: { + a: { + type: 'boolean' + }, + b: { + type: 'alternatives', + flags: { + label: 'x' + }, + matches: [ + { + is: { + type: 'any', + allow: [{ override: true }, true], + flags: { + only: true, + presence: 'required' + } + }, + ref: { + path: ['a'] + }, + otherwise: { + type: 'string', + flags: { + label: 'not x' + } + } + } + ] + } + } + }); + }); }); describe('match()', () => { From 5451b3b0760daf64ae6ed0d7f7b52f63c291ee8d Mon Sep 17 00:00:00 2001 From: Dario Snajder <22353326+dariosn85@users.noreply.github.com> Date: Tue, 8 Nov 2022 17:07:45 +0100 Subject: [PATCH 2/4] Fix for #2874 * updated TypeScript definitions * updated documentation --- API.md | 2 +- lib/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index 707f35c8..06801211 100755 --- a/API.md +++ b/API.md @@ -614,7 +614,7 @@ Adds a custom validation function to execute arbitrary code where: - `state` - the current validation state. - `prefs` - the current preferences. - `original` - the original value passed into validation before any conversions. - - `error(code, [local])` - a method to generate error codes using a message code and optional local context. + - `error(code, [local], [localState])` - a method to generate error codes using a message code, optional local context and optional validation local state. - `message(messages, [local])` - a method to generate an error with an internal `'custom'` error code and the provided messages object to use as override. Note that this is much slower than using the preferences `messages` option but is much simpler to write when performance is not important. - `warn(code, [local])` - a method to add a warning using a message code and optional local context. diff --git a/lib/index.d.ts b/lib/index.d.ts index 3ed262ea..6b60ec37 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -738,7 +738,7 @@ declare namespace Joi { prefs: ValidationOptions; original: V; warn: (code: string, local?: Context) => void; - error: (code: string, local?: Context) => ErrorReport; + error: (code: string, local?: Context, localState?: State) => ErrorReport; message: (messages: LanguageMessages, local?: Context) => ErrorReport; } From 6d11457fd006d07dfbd203a7cfd1073e895b4508 Mon Sep 17 00:00:00 2001 From: Nicolas Morel Date: Mon, 15 Jan 2024 13:54:03 +0100 Subject: [PATCH 3/4] chore: bump packages to latest versions --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index deb3152d..50d63a70 100755 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ "validation" ], "dependencies": { - "@hapi/hoek": "^9.0.0", - "@hapi/topo": "^5.0.0", - "@sideway/address": "^4.1.3", + "@hapi/hoek": "^9.3.0", + "@hapi/topo": "^5.1.0", + "@sideway/address": "^4.1.4", "@sideway/formula": "^3.0.1", "@sideway/pinpoint": "^2.0.0" }, @@ -25,8 +25,8 @@ "@hapi/bourne": "2.x.x", "@hapi/code": "8.x.x", "@hapi/joi-legacy-test": "npm:@hapi/joi@15.x.x", - "@hapi/lab": "^25.0.1", - "@types/node": "^14.18.24", + "@hapi/lab": "^25.1.3", + "@types/node": "^14.18.63", "typescript": "4.3.x" }, "scripts": { From fb5926c3ce5f649bf6f50474f9cbf3a4dfcc7564 Mon Sep 17 00:00:00 2001 From: Nicolas Morel Date: Mon, 15 Jan 2024 13:56:23 +0100 Subject: [PATCH 4/4] 17.11.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 50d63a70..e0d20596 100755 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "joi", "description": "Object schema validation", - "version": "17.11.0", + "version": "17.11.1", "repository": "git://github.com/hapijs/joi", "main": "lib/index.js", "types": "lib/index.d.ts",