Query string validation in Fastify

What does the example of query string validation look in the Fastify documentation? Like this:

1
2
3
4
5
6
const schema = {
  querystring: {
    name: { type: 'string' },
    excitement: { type: 'integer' }
  }
}

What is the problem? It is a lie!

The correct way of defining the schema of query parameters in this case (two values: “name” and “excitement”). Looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const querySchema = {
    schema: {
       querystring: {
         type: 'object',
           properties: {
             name: {
               type: 'string'
             },
             excitement: {
               type: 'integer'
             },
         }
     }
  }
}

Additionally, if we want to make the name parameter mandatory, we add it to the array of required parameters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const querySchema = {
    schema: {
       querystring: {
         type: 'object',
           properties: {
             name: {
               type: 'string'
             },
             excitement: {
               type: 'integer'
             },
         }
         required: ['name']
     }
  }
}

Did you enjoy reading this article?
Would you like to learn more about leveraging AI to drive growth and innovation, software craft in data engineering, and MLOps?

Subscribe to the newsletter or add this blog to your RSS reader (does anyone still use them?) to get a notification when I publish a new essay!

Newsletter

Do you enjoy reading my articles?
Subscribe to the newsletter if you don't want to miss the new content, business offers, and free training materials.

Bartosz Mikulski

Bartosz Mikulski

  • MLOps engineer by day
  • AI and data engineering consultant by night
  • Python and data engineering trainer
  • Conference speaker
  • Contributed a chapter to the book "97 Things Every Data Engineer Should Know"
  • Twitter: @mikulskibartosz
  • Mastodon: @mikulskibartosz@mathstodon.xyz
Newsletter

Do you enjoy reading my articles?
Subscribe to the newsletter if you don't want to miss the new content, business offers, and free training materials.