Reputation: 15188
according to the fastify 4.0 docs, the default options for Ajv include removeAdditional: true
. I have a queryString
schema, and when I send a query with a param that is not in the schema, I expect that param to be removed. But no such thing happens. The schema itself is pretty run-of-the-mill
querystring: {
type: "object",
properties: { β¦ }
}
Some of my params are of type array, and there too I would like any additional values (outside of those specified via enum
to be removed. How can I achieve this?
update: I am adding the actual schema below (with some info snipped for brevity)
{
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'$id': 'https://example.com/treatments.schema.json',
title: 'Treatments',
summary: 'Fetches treatments',
description: 'Treatments areβ¦',
response: {},
querystring: {
type: 'object',
additionalProperties: false,
properties: {
treatmentId: {
type: "string",
maxLength: 32,
minLength: 32,
description: "A 32 character string: treatmentId=388D179E0D564775C3925A5B93C1C407"
},
β¦ <snipped> β¦
}
},
tags: [ 'zenodeo' ]
}
I know the schema is being validated because http://localhost:3010/v3/treatments?treatmentId=foo
gives an error {"statusCode":400,"error":"Bad Request","message":"querystring/treatmentId must NOT have fewer than 32 characters"}
(that is good ππ½) but the additionalProperties: false
constraint is still not working because http://localhost:3010/v3/treatments?foo=bar
gives no error and foo
is also not removed from request.query
(that is not good ππ½)
Upvotes: 3
Views: 755
Reputation: 5051
With removeAdditional: true
you must still set additionalProperties: false
on your schema:
querystring: {
type: "object",
properties: { β¦ },
additionalProperties: false
}
See https://ajv.js.org/json-schema.html#additionalproperties
You can also change the default behavior to removeAdditional: 'all'
in which case you don't have to set additionalProperties: false
on your schema.
See about halfway down the page here: https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/
Upvotes: 4