From 9a05b687b809528d3a898d9c415a8264dc2b472e Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 24 Oct 2020 00:15:19 -0700 Subject: [PATCH] Fix #2491 --- API.md | 8 ++++++++ test/types/link.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/API.md b/API.md index f3a11e7b2..3d63e2838 100755 --- a/API.md +++ b/API.md @@ -2044,6 +2044,14 @@ then initialized. If `ref` was not passed to the constructor, `link.ref()` must Will throw an error during validation if left uninitialized (e.g. `Joi.link()` called without a link and `link.ref()` not called). +```js +const schema = Joi.object({ + a: [Joi.string(), Joi.number()], + b: Joi.link().ref('#type.a') +}) + .id('type'); +``` + #### `link.concat(schema)` Same as [`any.concat()`](#anyconcatschema) but the schema is merged after the link is resolved which allows merging with schemas of the same type as the resolved link. Will throw an exception during validation if the merged types are not compatible. diff --git a/test/types/link.js b/test/types/link.js index 1fc9499aa..121e9470e 100755 --- a/test/types/link.js +++ b/test/types/link.js @@ -36,6 +36,21 @@ describe('link', () => { ]); }); + it('links named schema (by ref)', () => { + + const schema = Joi.object({ + a: [Joi.string(), Joi.number()], + b: Joi.link().ref('#type.a') + }) + .id('type'); + + Helper.validate(schema, [ + [{ a: 1, b: 2 }, true], + [{ a: '1', b: '2' }, true], + [{ a: [1], b: '2' }, false, '"a" must be one of [string, number]'] + ]); + }); + it('links named schema (implicit)', () => { const schema = Joi.object({