Comments (7)
You can validate its format but nothing else unless manually coerced
from fastify.
We might think of enabling coerceTypes by default in v5.
from fastify.
We coerce the type already:
https://github.com/fastify/ajv-compiler?tab=readme-ov-file#ajv-configuration
https://runkit.com/embed/4gzt2onngmx9
Could you add a Minimal, Reproducible Example?
Without it, we are unable to help you.
from fastify.
You can solve this by either enabling coerceTypes
on ajv
or parse the req.query
property and parse the type before doing the validation using the preValidation
hook.
Fastify uses find-my-way
as a router, and find-my-way
uses fast-querystring
which mimics the behaviour of node:querystring
in a more performant way, and by default a query string number will always be parsed as string
.
from fastify.
and by default a query string number will always be parsed as string.
After struggling with it, I realized that. I thought I did something wrong with fastify. So there is no real meaning for validating query string params as they are always string.
from fastify.
Why are these tests passing?
'use strict'
const { test } = require('tap')
const Fastify = require('..')
test('querystring', t => {
t.plan(4)
const fastify = Fastify()
fastify.get('/:id', {
schema: {
querystring: {
type: 'object',
properties: {
a: { type: 'number' }
}
}
}
}, function (req, reply) {
t.equal(req.query.a, 42)
reply.code(200).send()
})
fastify.inject({
method: 'GET',
url: '/12?a=42'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, '')
})
})
test('params', t => {
t.plan(4)
const fastify = Fastify()
fastify.get('/:a', {
schema: {
params: {
type: 'object',
properties: {
a: { type: 'integer' }
}
}
}
}, function (req, reply) {
t.equal(req.params.a, 42)
reply.code(200).send()
})
fastify.inject({
method: 'GET',
url: '/42'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, '')
})
})
from fastify.
Silly me, I initialize fastify like this
ajv: {
customOptions: {
coerceTypes: false,
},
},
and I don't event remember why I did that, I copied this from another project I did and forgot about it. Thanks.
from fastify.
Related Issues (20)
- `disableRequestLogging` should disable logs in default error handler HOT 6
- incorrect plugin identified for timeout HOT 1
- Backport webdav methods from next to current branch HOT 7
- OPTIONS HTTP method body processing HOT 3
- Dependency update for v5 HOT 1
- Merge main into next HOT 4
- Scoped FastifyRequest Customization via Generic Parameter in FastifyInstance HOT 1
- Flaky test on N|Solid 20 MacOS HOT 2
- Implement simple B-Tree for faster string comparisons HOT 6
- Throwing error in setErrorHandler after a JSON parse SyntaxError causes app to crash
- Where is the documentation for <Reply>.sendFile? HOT 2
- Switch to ajv/2020 for fastify 5 HOT 7
- Fastify v5 coordination HOT 6
- Does Validation-and-Serialization document's outdated?
- Should we drop Node.js v18 for v5? HOT 11
- `disableErrorLogging` option HOT 1
- Request hangs if you `await reply.status` in an async handler. HOT 15
- onError hook is called before errorHandler when error is thrown in onRequest hook HOT 6
- Approval Request for Future State Proposal HOT 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify.