0

I'm trying to learn fastify throught the official documentation. I'm really intrested in the validation of an incoming post request with a json schema. Following the instructions i added to my routes:

    fastify.addSchema({
        $id: 'http://example.com/',
        type: 'object',
        properties: {
            hello: { type: 'string' }
        }
    })

    fastify.post('/', {
        handler() { },
        schema: {
            body: {
                type: 'array',
                items: { $ref: 'http://example.com#/properties/hello' }
            }
        }
    })

Now the problem is that I can not write a json that can be accepted by this schema. From my basic understanding a simple post request like the following should be accepted

[
    {
        "hello": "bye"
    },
    {
        "hello": "bye bye"
    }
]

However server keeps telling me that body[0] should be string. Where am I wrong?

Mario Rossi
  • 107
  • 1
  • 4
  • 12

1 Answers1

3

The reference $ref: 'http://example.com#/properties/hello' points to the hello property schema value, which is { type: 'string' }.

This means the schema in fastify.post('/', { expects the body to be an array of strings.

Relequestual
  • 11,631
  • 6
  • 47
  • 83
  • Thanks for accepting my answer. Glad I could help! If you would like to buy me a coffee to say thanks, please find a link on my profile. No expectations =] – Relequestual Mar 19 '21 at 15:34