fluent-schema.test.js — fastify Source File
Architecture documentation for fluent-schema.test.js, a javascript file in the fastify codebase.
Entity Profile
Source Code
'use strict'
const { test } = require('node:test')
const Fastify = require('..')
const S = require('fluent-json-schema')
test('use fluent-json-schema object', async (t) => {
t.plan(10)
const fastify = Fastify()
fastify.post('/:id', {
handler: (req, reply) => { reply.send({ name: 'a', surname: 'b', dateOfBirth: '01-01-2020' }) },
schema: {
params: S.object().prop('id', S.integer().minimum(42)),
headers: S.object().prop('x-custom', S.string().format('email')),
query: S.object().prop('surname', S.string().required()),
body: S.object().prop('name', S.string().required()),
response: {
200: S.object()
.prop('name', S.string())
.prop('surname', S.string())
}
}
})
const res1 = await fastify.inject({
method: 'POST',
url: '/1',
headers: { 'x-custom': 'me@me.me' },
query: { surname: 'bar' },
payload: { name: 'foo' }
})
t.assert.strictEqual(res1.statusCode, 400)
t.assert.deepStrictEqual(res1.json(), { statusCode: 400, code: 'FST_ERR_VALIDATION', error: 'Bad Request', message: 'params/id must be >= 42' })
// check header
const res2 = await fastify.inject({
method: 'POST',
url: '/42',
headers: { 'x-custom': 'invalid' },
query: { surname: 'bar' },
payload: { name: 'foo' }
})
t.assert.strictEqual(res2.statusCode, 400)
t.assert.deepStrictEqual(res2.json(), { statusCode: 400, code: 'FST_ERR_VALIDATION', error: 'Bad Request', message: 'headers/x-custom must match format "email"' })
// check query
const res3 = await fastify.inject({
method: 'POST',
url: '/42',
headers: { 'x-custom': 'me@me.me' },
query: { },
payload: { name: 'foo' }
})
t.assert.strictEqual(res3.statusCode, 400)
t.assert.deepStrictEqual(res3.json(), { statusCode: 400, code: 'FST_ERR_VALIDATION', error: 'Bad Request', message: 'querystring must have required property \'surname\'' })
// check body
const res4 = await fastify.inject({
method: 'POST',
// ... (150 more lines)
Source
Frequently Asked Questions
What does fluent-schema.test.js do?
fluent-schema.test.js is a source file in the fastify codebase, written in javascript.
Where is fluent-schema.test.js in the architecture?
fluent-schema.test.js is located at test/fluent-schema.test.js (directory: test).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free