-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
81 lines (70 loc) · 1.87 KB
/
Copy pathtest.js
File metadata and controls
81 lines (70 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
const { test } = require('node:test')
const assert = require('node:assert')
const Fastify = require('fastify')
const plugin = require('./index')
test('fastify-param-schema-validation', async t => {
await t.test('passes if all route params are defined in schema', async () => {
const fastify = Fastify()
// Pass the option directly to the plugin here:
await fastify.register(plugin, { exposeParamSchemaValidation: true })
fastify.get('/valid/:id', {
schema: {
params: {
type: 'object',
properties: {
id: { type: 'string' }
}
}
}
}, (req, reply) => {
reply.send('ok')
})
await fastify.ready()
assert.ok(true)
await fastify.close()
})
await t.test('throws if schema is missing route parameter', async () => {
const fastify = Fastify()
await fastify.register(plugin, { exposeParamSchemaValidation: true })
assert.throws(
() => {
fastify.get('/broken/:missingId', {
schema: {
params: {
type: 'object',
properties: {
wrongName: { type: 'string' }
}
}
}
}, (req, reply) => {
reply.send('ok')
})
},
(err) => {
return err.code === 'FST_ERR_SCH_VALIDATION_BUILD'
}
)
await fastify.close()
})
await t.test('supports path parameters with regex parentheses', async () => {
const fastify = Fastify()
await fastify.register(plugin, { exposeParamSchemaValidation: true })
fastify.get('/regex/:id(\\d+)', {
schema: {
params: {
type: 'object',
properties: {
id: { type: 'string' }
}
}
}
}, (req, reply) => {
reply.send('ok')
})
await fastify.ready()
assert.ok(true)
await fastify.close()
})
})