UNPKG

3.6 kBMarkdownView Raw
1<h1 align="center">Fastify</h1>
2
3## Fluent Schema
4
5The [Validation and Serialization](https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md)
6has explained all the parameter accepted by Fastify to set a JSON Schema Validation, to validates
7the input, and a JSON Schema Serialization to optimize the output.
8
9To set up the JSON schemas of our Fastify application, we can use [`fluent-schema`][fluent-schema-repo]
10to simplify this task and reuse constants values.
11
12### Basic settings
13
14```js
15const S = require('fluent-schema')
16
17// You can have an object like this, or query a db to get the values
18const MY_KEY = {
19 KEY1: 'ONE',
20 KEY2: 'TWO'
21}
22
23const bodyJsonSchema = S.object()
24 .prop('someKey', S.string())
25 .prop('someOtherKey', S.number())
26 .prop('requiredKey', S.array().maxItems(3).items(S.integer()).required())
27 .prop('nullableKey', S.mixed([S.TYPES.NUMBER, S.TYPES.NULL]))
28 .prop('multipleTypesKey', S.mixed([S.TYPES.BOOLEAN, S.TYPES.NUMBER]))
29 .prop('multipleRestrictedTypesKey', S.oneOf([S.string().maxLength(5), S.number().minimum(10)]))
30 .prop('enumKey', S.enum(Object.values(MY_KEYS)))
31 .prop('notTypeKey', S.not(S.array()))
32
33const queryStringJsonSchema = S.object()
34 .prop('name', S.string())
35 .prop('excitement', S.integer())
36
37const paramsJsonSchema = S.object()
38 .prop('par1', S.string())
39 .prop('par2', S.integer())
40
41const headersJsonSchema = S.object()
42 .prop('x-foo', S.string().required())
43
44const schema = {
45 body: bodyJsonSchema.valueOf(),
46 querystring: queryStringJsonSchema.valueOf(),
47 params: paramsJsonSchema.valueOf(),
48 headers: headersJsonSchema.valueOf()
49}
50
51fastify.post('/the/url', { schema }, handler)
52```
53
54### Reuse
55
56With `fluent-schema` you can manipulate your schemas in an easier and programmatic way and then reuse them
57thanks to the `addSchema()` method. You can refer to the schema in two different manners that are detailed
58in [Validation-and-Serialization.md](./Validation-and-Serialization.md#adding-a-shared-schema) document.
59
60Here some example usage:
61
62**`$ref-way`**: refer to external schema.
63
64```js
65const addressSchema = S.object()
66 .id('#address')
67 .prop('line1').required()
68 .prop('line2')
69 .prop('country').required()
70 .prop('city').required()
71 .prop('zipcode').required()
72 .valueOf()
73
74const commonSchemas = S.object()
75 .id('https://fastify/demo')
76 .definition('addressSchema', addressSchema)
77 .definition('otherSchema', otherSchema) // you can add any schemas you need
78 .valueOf()
79
80fastify.addSchema(commonSchemas)
81
82const bodyJsonSchema = S.object()
83 .prop('residence', S.ref('https://fastify/demo#address')).required()
84 .prop('office', S.ref('https://fastify/demo#/definitions/addressSchema')).required()
85 .valueOf()
86
87const schema = { body: bodyJsonSchema }
88
89fastify.post('/the/url', { schema }, handler)
90```
91
92
93**`replace-way`**: refer to a shared schema to replace before the validation process.
94
95```js
96const sharedAddressSchema = {
97 $id: 'sharedAddress',
98 type: 'object',
99 required: ['line1', 'country', 'city', 'zipcode'],
100 properties: {
101 line1: { type: 'string' },
102 line2: { type: 'string' },
103 country: { type: 'string' },
104 city: { type: 'string' },
105 zipcode: { type: 'string' }
106 }
107}
108fastify.addSchema(sharedAddressSchema)
109
110const bodyJsonSchema = {
111 type: 'object',
112 properties: {
113 vacation: 'sharedAddress#'
114 }
115}
116
117const schema = { body: bodyJsonSchema }
118
119fastify.post('/the/url', { schema }, handler)
120```
121
122NB: you can mix up the usage `$ref-way` and the `replace-way` with `fastify.addSchema`.
123
124[fluent-schema-repo]: https://github.com/fastify/fluent-schema