1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const Joi = require('joi')
|
5 | require('./helper').payloadMethod('post', t)
|
6 | require('./input-validation').payloadMethod('post', t)
|
7 |
|
8 | const Fastify = require('..')
|
9 |
|
10 | t.test('cannot call setSchemaCompiler after binding', t => {
|
11 | t.plan(2)
|
12 |
|
13 | const fastify = Fastify()
|
14 | t.tearDown(fastify.close.bind(fastify))
|
15 |
|
16 | fastify.listen(0, err => {
|
17 | t.error(err)
|
18 |
|
19 | try {
|
20 | fastify.setSchemaCompiler(() => { })
|
21 | t.fail()
|
22 | } catch (e) {
|
23 | t.pass()
|
24 | }
|
25 | })
|
26 | })
|
27 |
|
28 | t.test('cannot set schemaCompiler after binding', t => {
|
29 | t.plan(2)
|
30 |
|
31 | const fastify = Fastify()
|
32 | t.tearDown(fastify.close.bind(fastify))
|
33 |
|
34 | fastify.listen(0, err => {
|
35 | t.error(err)
|
36 |
|
37 | try {
|
38 | fastify.schemaCompiler = () => { }
|
39 | t.fail()
|
40 | } catch (e) {
|
41 | t.pass()
|
42 | }
|
43 | })
|
44 | })
|
45 |
|
46 | t.test('get schemaCompiler after set schemaCompiler', t => {
|
47 | t.plan(2)
|
48 | const mySchemaCompiler = () => { }
|
49 |
|
50 | const fastify = Fastify()
|
51 | fastify.schemaCompiler = mySchemaCompiler
|
52 | const sc = fastify.schemaCompiler
|
53 |
|
54 | t.ok(Object.is(mySchemaCompiler, sc))
|
55 | fastify.ready(t.error)
|
56 | })
|
57 |
|
58 | t.test('get schemaCompiler after setSchemaCompiler', t => {
|
59 | t.plan(2)
|
60 | const mySchemaCompiler = () => { }
|
61 |
|
62 | const fastify = Fastify()
|
63 | fastify.setSchemaCompiler(mySchemaCompiler)
|
64 | const sc = fastify.schemaCompiler
|
65 |
|
66 | t.ok(Object.is(mySchemaCompiler, sc))
|
67 | fastify.ready(t.error)
|
68 | })
|
69 |
|
70 | t.test('get schemaCompiler is empty for schemaCompilere settle on routes', t => {
|
71 | t.plan(2)
|
72 |
|
73 | const fastify = Fastify()
|
74 |
|
75 | const body = Joi.object().keys({
|
76 | name: Joi.string(),
|
77 | work: Joi.string()
|
78 | }).required()
|
79 |
|
80 | const schemaCompiler = schema => data => Joi.validate(data, schema)
|
81 |
|
82 | fastify.post('/', {
|
83 | schema: { body },
|
84 | schemaCompiler
|
85 | }, function (req, reply) {
|
86 | reply.send('ok')
|
87 | })
|
88 |
|
89 | fastify.inject({
|
90 | method: 'POST',
|
91 | payload: {},
|
92 | url: '/'
|
93 | }, (err, res) => {
|
94 | t.error(err)
|
95 | t.equal(fastify.schemaCompiler, null)
|
96 | })
|
97 | })
|