Skip to content

Commit cf222dd

Browse files
authored
add: more example on how use the $ref keyword (fastify#1613)
1 parent 78bef60 commit cf222dd

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

docs/Validation-and-Serialization.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ Thanks to the `addSchema` API, you can add multiple schemas to the Fastify insta
9191
There are two ways to reuse your shared schemas:
9292
+ **`$ref-way`**: as described in the [standard](https://tools.ietf.org/html/draft-handrews-json-schema-01#section-8),
9393
you can refer to an external schema. To use it you have to `addSchema` with a valid `$id` absolute URI.
94+
+ **`replace-way`**: this is a Fastify utility that lets you to substitute some fields with a shared schema.
95+
To use it you have to `addSchema` with an `$id` having a relative URI fragment which is a simple string that
96+
applies only to alphanumeric chars `[A-Za-z0-9]`.
97+
98+
Here an overview on _how_ to set an `$id` and _how_ references to it:
99+
100+
+ `replace-way`
101+
+ `myField: 'foobar#'` will search for a shared schema added with `$id: 'foobar'`
102+
+ `$ref-way`
103+
+ `myField: { $ref: '#foo'}` will search for field with `$id: '#foo'` inside the current schema
104+
+ `myField: { $ref: '#/definitions/foo'}` will search for field `definitions.foo` inside the current schema
105+
+ `myField: { $ref: 'http://url.com/sh.json#'}` will search for a shared schema added with `$id: 'http://url.com/sh.json'`
106+
+ `myField: { $ref: 'http://url.com/sh.json#/definitions/foo'}` will search for a shared schema added with `$id: 'http://url.com/sh.json'` and will use the field `definitions.foo`
107+
+ `myField: { $ref: 'http://url.com/sh.json#foo'}` will search for a shared schema added with `$id: 'http://url.com/sh.json'` and it will look inside of it for object with `$id: '#foo'`
108+
109+
110+
More examples:
111+
112+
**`$ref-way`** usage examples:
94113

95114
```js
96115
fastify.addSchema({
@@ -114,9 +133,7 @@ fastify.route({
114133
})
115134
```
116135

117-
+ **`replace-way`**: this is a Fastify utility that lets you to substitute some fields with a shared schema.
118-
To use it you have to `addSchema` with an `$id` having a relative URI fragment which is a simple string that
119-
applies only to alphanumeric chars `[A-Za-z0-9]`.
136+
**`replace-way`** usage examples:
120137

121138
```js
122139
const fastify = require('fastify')()

examples/shared-schema.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const fastify = require('../fastify')()
4+
5+
fastify.addSchema({
6+
$id: 'https://foo/common.json',
7+
definitions: {
8+
response: {
9+
$id: '#reply',
10+
type: 'object',
11+
properties: {
12+
hello: {
13+
$id: '#bar',
14+
type: 'string'
15+
}
16+
}
17+
}
18+
}
19+
})
20+
21+
const opts = {
22+
schema: {
23+
response: {
24+
200: { $ref: 'https://foo/common.json#reply' }
25+
}
26+
}
27+
}
28+
29+
fastify
30+
.get('/', opts, function (req, reply) {
31+
reply.send({ hello: 'world' })
32+
})
33+
34+
fastify.listen(3000, err => {
35+
if (err) throw err
36+
console.log(`server listening on ${fastify.server.address().port}`)
37+
})

0 commit comments

Comments
 (0)