Skip to content

Commit

Permalink
Fix #2491
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Oct 24, 2020
1 parent f5f6e62 commit 9a05b68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions test/types/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 9a05b68

Please sign in to comment.