|
11 | 11 | - [`compile(schema)`](#compileschema)
|
12 | 12 | - [`assert(value, schema, [message])`](#assertvalue-schema-message)
|
13 | 13 | - [`attempt(value, schema, [message])`](#attemptvalue-schema-message)
|
14 |
| - - [`ref(key, options)`](#refkey-options) |
| 14 | + - [`ref(key, [options])`](#refkey-options) |
15 | 15 | - [`isRef(ref)`](#isrefref)
|
16 | 16 | - [`reach(schema, path)`](#reachschema-path)
|
17 | 17 | - [`extend(extension)`](#extendextension)
|
|
124 | 124 | - [`alternatives.try(schemas)`](#alternativestryschemas)
|
125 | 125 | - [`alternatives.when(ref, options)`](#alternativeswhenref-options)
|
126 | 126 | - [`lazy(fn)`](#lazyfn)
|
127 |
| - - [`ref(key, [options])`](#refkey-options) |
128 | 127 | - [Errors](#errors)
|
129 | 128 |
|
130 | 129 | <!-- tocstop -->
|
@@ -229,18 +228,32 @@ Joi.attempt('x', Joi.number()); // throws error
|
229 | 228 | const result = Joi.attempt('4', Joi.number()); // result -> 4
|
230 | 229 | ```
|
231 | 230 |
|
232 |
| -### `ref(key, options)` |
| 231 | +### `ref(key, [options])` |
233 | 232 |
|
234 |
| -Creates a reference to another property in the object being validated. |
| 233 | +Generates a reference to the value of the named key. References are resolved at validation time and in order of dependency |
| 234 | +so that if one key validation depends on another, the dependent key is validated second after the reference is validated. |
| 235 | +References support the following arguments: |
| 236 | +- `key` - the reference target. References cannot point up the object tree, only to sibling keys, but they can point to |
| 237 | + their siblings' children (e.g. 'a.b.c') using the `.` separator. If a `key` starts with `$` is signifies a context reference |
| 238 | + which is looked up in the `context` option object. |
| 239 | +- `options` - optional settings: |
| 240 | + - `separator` - overrides the default `.` hierarchy separator. |
| 241 | + - `contextPrefix` - overrides the default `$` context prefix signifier. |
| 242 | + - Other options can also be passed based on what [`Hoek.reach`](https://github.com/hapijs/hoek/blob/master/API.md#reachobj-chain-options) supports. |
235 | 243 |
|
236 |
| -- `key` - the path to the property. By default it uses `.` as separator and `$` to indicate that the reference should look in the context and not in the object being validated. |
237 |
| -- `options` - an optional object containing options for the syntax of the key : |
238 |
| - - `contextPrefix` - changes the context prefix. Defaults to `$`. |
239 |
| - - `separator` - changes the separator for the path. Defaults to `.`. |
240 |
| - - Other options can also be passed based on what [`Hoek.reach`](https://github.com/hapijs/hoek/blob/master/API.md#reachobj-chain-options) supports. |
| 244 | +Note that references can only be used where explicitly supported such as in `valid()` or `invalid()` rules. If upwards |
| 245 | +(parents) references are needed, use [`object.assert()`](#objectassertref-schema-message). |
241 | 246 |
|
242 | 247 | ```js
|
243 |
| -const ref = Joi.ref('#a/b/c', { contextPrefix: '#', separator: '/' }); |
| 248 | +const schema = Joi.object().keys({ |
| 249 | + a: Joi.ref('b.c'), |
| 250 | + b: { |
| 251 | + c: Joi.any() |
| 252 | + }, |
| 253 | + c: Joi.ref('$x') |
| 254 | +}); |
| 255 | + |
| 256 | +Joi.validate({ a: 5, b: { c: 5 } }, schema, { context: { x: 5 } }, (err, value) => {}); |
244 | 257 | ```
|
245 | 258 |
|
246 | 259 | ### `isRef(ref)`
|
@@ -1737,33 +1750,6 @@ const Person = Joi.object({
|
1737 | 1750 | });
|
1738 | 1751 | ```
|
1739 | 1752 |
|
1740 |
| -### `ref(key, [options])` |
1741 |
| -
|
1742 |
| -Generates a reference to the value of the named key. References are resolved at validation time and in order of dependency |
1743 |
| -so that if one key validation depends on another, the dependent key is validated second after the reference is validated. |
1744 |
| -References support the following arguments: |
1745 |
| -- `key` - the reference target. References cannot point up the object tree, only to sibling keys, but they can point to |
1746 |
| - their siblings' children (e.g. 'a.b.c') using the `.` separator. If a `key` starts with `$` is signifies a context reference |
1747 |
| - which is looked up in the `context` option object. |
1748 |
| -- `options` - optional settings: |
1749 |
| - - `separator` - overrides the default `.` hierarchy separator. |
1750 |
| - - `contextPrefix` - overrides the default `$` context prefix signifier. |
1751 |
| -
|
1752 |
| -Note that references can only be used where explicitly supported such as in `valid()` or `invalid()` rules. If upwards |
1753 |
| -(parents) references are needed, use [`object.assert()`](#objectassertref-schema-message). |
1754 |
| -
|
1755 |
| -```js |
1756 |
| -const schema = Joi.object().keys({ |
1757 |
| - a: Joi.ref('b.c'), |
1758 |
| - b: { |
1759 |
| - c: Joi.any() |
1760 |
| - }, |
1761 |
| - c: Joi.ref('$x') |
1762 |
| -}); |
1763 |
| - |
1764 |
| -Joi.validate({ a: 5, b: { c: 5 } }, schema, { context: { x: 5 } }, (err, value) => {}); |
1765 |
| -``` |
1766 |
| -
|
1767 | 1753 | ## Errors
|
1768 | 1754 |
|
1769 | 1755 | Joi throws classical javascript `Error`s containing :
|
|
0 commit comments