You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Validation-and-Serialization.md
+20-3
Original file line number
Diff line number
Diff line change
@@ -91,6 +91,25 @@ Thanks to the `addSchema` API, you can add multiple schemas to the Fastify insta
91
91
There are two ways to reuse your shared schemas:
92
92
+**`$ref-way`**: as described in the [standard](https://tools.ietf.org/html/draft-handrews-json-schema-01#section-8),
93
93
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:
94
113
95
114
```js
96
115
fastify.addSchema({
@@ -114,9 +133,7 @@ fastify.route({
114
133
})
115
134
```
116
135
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
0 commit comments