Giter Site home page Giter Site logo

Comments (7)

metcoder95 avatar metcoder95 commented on June 12, 2024 1

You can validate its format but nothing else unless manually coerced

from fastify.

mcollina avatar mcollina commented on June 12, 2024 1

We might think of enabling coerceTypes by default in v5.

from fastify.

Eomm avatar Eomm commented on June 12, 2024 1

We coerce the type already:
https://github.com/fastify/ajv-compiler?tab=readme-ov-file#ajv-configuration

image

https://runkit.com/embed/4gzt2onngmx9

Could you add a Minimal, Reproducible Example?

Without it, we are unable to help you.

from fastify.

metcoder95 avatar metcoder95 commented on June 12, 2024

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.

AmosSzust avatar AmosSzust commented on June 12, 2024

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.

gurgunday avatar gurgunday commented on June 12, 2024

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.

AmosSzust avatar AmosSzust commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.