1 | 'use strict'
|
2 |
|
3 | const Fastify = require('..')
|
4 | const S = require('fluent-schema')
|
5 |
|
6 | function fluentSchemaTest (t) {
|
7 | const test = t.test
|
8 |
|
9 | test('fluent-schema generate a valid JSON Schema in "$ref-way"', t => {
|
10 | t.plan(1)
|
11 |
|
12 | const fastify = new Fastify()
|
13 |
|
14 | const addressSchema = S.object()
|
15 | .id('#address')
|
16 | .prop('line1').required()
|
17 | .prop('line2')
|
18 | .prop('country').required()
|
19 | .prop('city').required()
|
20 | .prop('zipcode').required()
|
21 | .valueOf()
|
22 |
|
23 | const commonSchemas = S.object()
|
24 | .id('https://fastify/demo')
|
25 | .definition('addressSchema', addressSchema)
|
26 | .valueOf()
|
27 |
|
28 | fastify.addSchema(commonSchemas)
|
29 |
|
30 | const bodyJsonSchema = S.object()
|
31 | .prop('residence', S.ref('https://fastify/demo#address')).required()
|
32 | .prop('office', S.ref('https://fastify/demo#/definitions/addressSchema')).required()
|
33 | .valueOf()
|
34 |
|
35 | const schema = { body: bodyJsonSchema }
|
36 | fastify.post('/the/url', { schema }, () => { })
|
37 |
|
38 | fastify.ready(t.error)
|
39 | })
|
40 |
|
41 | test('fluent-schema generate a valid JSON Schema in "replace-way"', t => {
|
42 | t.plan(1)
|
43 |
|
44 | const fastify = new Fastify()
|
45 |
|
46 | const sharedAddressSchema = {
|
47 | $id: 'sharedAddress',
|
48 | type: 'object',
|
49 | required: ['line1', 'country', 'city', 'zipcode'],
|
50 | properties: {
|
51 | line1: { type: 'string' },
|
52 | line2: { type: 'string' },
|
53 | country: { type: 'string' },
|
54 | city: { type: 'string' },
|
55 | zipcode: { type: 'string' }
|
56 | }
|
57 | }
|
58 |
|
59 | fastify.addSchema(sharedAddressSchema)
|
60 |
|
61 | const bodyJsonSchema = {
|
62 | type: 'object',
|
63 | properties: {
|
64 | vacation: 'sharedAddress#'
|
65 | }
|
66 | }
|
67 | const schema = { body: bodyJsonSchema }
|
68 |
|
69 | fastify.post('/the/url', { schema }, () => { })
|
70 |
|
71 | fastify.ready(t.error)
|
72 | })
|
73 |
|
74 | test('fluent-schema mix-up of "$ref-way" and "replace-way"', t => {
|
75 | t.plan(1)
|
76 |
|
77 | const fastify = new Fastify()
|
78 |
|
79 | const addressSchema = S.object()
|
80 | .id('#address')
|
81 | .prop('line1').required()
|
82 | .prop('line2')
|
83 | .prop('country').required()
|
84 | .prop('city').required()
|
85 | .prop('zipcode').required()
|
86 | .valueOf()
|
87 |
|
88 | const commonSchemas = S.object()
|
89 | .id('https://fastify/demo')
|
90 | .definition('addressSchema', addressSchema)
|
91 | .valueOf()
|
92 |
|
93 | const sharedAddressSchema = {
|
94 | $id: 'sharedAddress',
|
95 | type: 'object',
|
96 | required: ['line1', 'country', 'city', 'zipcode'],
|
97 | properties: {
|
98 | line1: { type: 'string' },
|
99 | line2: { type: 'string' },
|
100 | country: { type: 'string' },
|
101 | city: { type: 'string' },
|
102 | zipcode: { type: 'string' }
|
103 | }
|
104 | }
|
105 |
|
106 | fastify.addSchema(commonSchemas)
|
107 | fastify.addSchema(sharedAddressSchema)
|
108 |
|
109 | const bodyJsonSchema = S.object()
|
110 | .prop('residence', S.ref('https://fastify/demo#address')).required()
|
111 | .prop('office', S.ref('https://fastify/demo#/definitions/addressSchema')).required()
|
112 | .valueOf()
|
113 |
|
114 |
|
115 | bodyJsonSchema.properties.vacation = 'sharedAddress#'
|
116 |
|
117 | const schema = { body: bodyJsonSchema }
|
118 |
|
119 | fastify.post('/the/url', { schema }, () => { })
|
120 |
|
121 | fastify.ready(t.error)
|
122 | })
|
123 |
|
124 | test('Should call valueOf internally', t => {
|
125 | t.plan(1)
|
126 |
|
127 | const fastify = new Fastify()
|
128 |
|
129 | const addressSchema = S.object()
|
130 | .id('#address')
|
131 | .prop('line1').required()
|
132 | .prop('line2')
|
133 | .prop('country').required()
|
134 | .prop('city').required()
|
135 | .prop('zipcode').required()
|
136 |
|
137 | const commonSchemas = S.object()
|
138 | .id('https://fastify/demo')
|
139 | .definition('addressSchema', addressSchema)
|
140 |
|
141 | fastify.addSchema(commonSchemas)
|
142 |
|
143 | fastify.route({
|
144 | method: 'POST',
|
145 | url: '/query',
|
146 | handler: () => {},
|
147 | schema: {
|
148 | query: S.object().prop('hello', S.string()).required(),
|
149 | body: S.object().prop('hello', S.string()).required(),
|
150 | params: S.object().prop('hello', S.string()).required(),
|
151 | headers: S.object().prop('hello', S.string()).required(),
|
152 | response: {
|
153 | 200: S.object().prop('hello', S.string()).required(),
|
154 | 201: S.object().prop('hello', S.string()).required()
|
155 | }
|
156 | }
|
157 | })
|
158 |
|
159 | fastify.route({
|
160 | method: 'POST',
|
161 | url: '/querystring',
|
162 | handler: () => {},
|
163 | schema: {
|
164 | querystring: S.object().prop('hello', S.string()).required(),
|
165 | body: S.object().prop('hello', S.string()).required(),
|
166 | params: S.object().prop('hello', S.string()).required(),
|
167 | headers: S.object().prop('hello', S.string()).required(),
|
168 | response: {
|
169 | 200: S.object().prop('hello', S.string()).required(),
|
170 | 201: S.object().prop('hello', S.string()).required()
|
171 | }
|
172 | }
|
173 | })
|
174 |
|
175 | fastify.ready(t.error)
|
176 | })
|
177 | }
|
178 |
|
179 | module.exports = fluentSchemaTest
|