Skip to content

Commit

Permalink
feat: ajv coerce array (#3594)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Jan 6, 2022
1 parent 38fc063 commit 0033f73
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 34 deletions.
32 changes: 4 additions & 28 deletions docs/Reference/Validation-and-Serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ fastify.post('/the/url', { schema }, handler)
the types specified in your schema `type` keywords, both to pass the validation
and to use the correctly typed data afterwards.*

The Ajv default configuration in Fastify doesn't support coercing array
parameters in querystring. However, Fastify allows
[`customOptions`](./Server.md#ajv) in Ajv instance. The `coerceTypes: 'array'`
will coerce one parameter to a single element in array. Example:
The Ajv default configuration in Fastify supports coercing array
parameters in `querystring`.
Example:

```js
const opts = {
Expand All @@ -256,37 +255,14 @@ const opts = {
}

fastify.get('/', opts, (request, reply) => {
reply.send({ params: request.query })
reply.send({ params: request.query }) // echo the querystring
})

fastify.listen(3000, (err) => {
if (err) throw err
})
```

Using Fastify defaults the following request will result in `400` status code:

```sh
curl -X GET "http://localhost:3000/?ids=1
{"statusCode":400,"error":"Bad Request","message":"querystring/hello should be array"}
```
Using `coerceTypes` as 'array' will fix it:
```js
const ajv = new Ajv({
removeAdditional: true,
useDefaults: true,
coerceTypes: 'array', // This line
allErrors: true
})
fastify.setValidatorCompiler(({ schema, method, url, httpPart }) => {
return ajv.compile(schema)
})
```
```sh
curl -X GET "http://localhost:3000/?ids=1
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
"yup": "^0.32.11"
},
"dependencies": {
"@fastify/ajv-compiler": "^2.1.0",
"@fastify/ajv-compiler": "^3.0.0",
"@fastify/fast-json-stringify-compiler": "^1.0.0",
"abstract-logging": "^2.0.1",
"avvio": "^8.1.0",
Expand Down
10 changes: 5 additions & 5 deletions test/schema-feature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1695,7 +1695,7 @@ test('Should throw if not default validator passed', async t => {
}
})

test('Should throw if not default validator passed', async t => {
test('Should coerce the array if the default validator is used', async t => {
t.plan(2)
const someSchema = {
$id: 'some',
Expand Down Expand Up @@ -1733,7 +1733,7 @@ test('Should throw if not default validator passed', async t => {
}
},
(req, reply) => {
reply.send({ noop: 'noop' })
reply.send(req.query)
}
)

Expand All @@ -1745,12 +1745,12 @@ test('Should throw if not default validator passed', async t => {
method: 'POST',
url: '/',
query: {
msg: ['string']
msg: 'string'
}
})

t.equal(res.json().message, 'querystring/msg must be array')
t.equal(res.statusCode, 400, 'Should not coearce the string into array')
t.equal(res.statusCode, 200)
t.same(res.json(), { msg: ['string'] }, 'Should coearce the string into array')
} catch (err) {
t.error(err)
}
Expand Down

0 comments on commit 0033f73

Please sign in to comment.