Skip to content

Commit

Permalink
fix: remove route validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sf3ris committed Dec 26, 2023
1 parent 66803f8 commit b3ad236
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 119 deletions.
3 changes: 0 additions & 3 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,6 @@ function fastify (options) {
findRoute: function _route (options) {
return router.findRoute(options)
},
validateRouteParams: function _route (options) {
return router.validateRouteParams(options)
},
// expose logger instance
log: logger,
// type provider
Expand Down
19 changes: 1 addition & 18 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ function buildRouting (options) {
addConstraintStrategy,
hasConstraintStrategy,
isAsyncConstraint,
findRoute,
validateRouteParams
findRoute
}

function addConstraintStrategy (strategy) {
Expand Down Expand Up @@ -181,22 +180,6 @@ function buildRouting (options) {
)
}

function validateRouteParams (options) {
const { method, url, constraints } = options
const route = findRoute({ method, url: url || '', constraints })
if (route === null) {
return false
}

const schemaParams = new Set(Object.keys(route.store.schema.params))
const params = new Set(Object.keys(route.params))
if (schemaParams.size !== params.size) {
return false
}

return [...schemaParams].every(param => params.has(param))
}

/**
* Route management
* @param {{ options: import('../fastify').RouteOptions, isFastify: boolean }}
Expand Down
36 changes: 33 additions & 3 deletions test/findRoute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { test } = require('tap')
const Fastify = require('..')
const fastifyPlugin = require('fastify-plugin')

test('findRoute should return null when route cannot be found due to a different method', t => {
t.plan(1)
Expand Down Expand Up @@ -45,7 +46,7 @@ test('findRoute should return an immutable route to avoid leaking and runtime ro
method: 'GET',
url: '/artists/:artistId'
})
t.deepEqual(route.params, { artistId: ':artistId' })
t.same(route.params, { artistId: ':artistId' })
})

test('findRoute should return null when route cannot be found due to a different path', t => {
Expand Down Expand Up @@ -83,6 +84,35 @@ test('findRoute should return the route when found', t => {
url: '/artists/:artistId'
})

t.deepEqual(route.params, { artistId: ':artistId' })
t.deepEqual(route.store.schema, { params: { artistId: { type: 'integer' } } })
t.same(route.params, { artistId: ':artistId' })
t.same(route.store.schema, { params: { artistId: { type: 'integer' } } })
})

test('findRoute should work correctly when used within plugins', t => {
t.plan(1)
const fastify = Fastify()
const handler = (req, reply) => reply.send(typeof req.params.artistId)
fastify.get('/artists/:artistId', {
schema: {
params: { artistId: { type: 'integer' } }
},
handler
})

function validateRoutePlugin (instance, opts, done) {
const validateParams = function () {
return instance.findRoute({
method: 'GET',
url: '/artists/:artistId'
}) !== null
}
instance.decorate('validateRoutes', { validateParams })
done()
}

fastify.register(fastifyPlugin(validateRoutePlugin))

fastify.ready(() => {
t.equal(fastify.validateRoutes.validateParams(), true)
})
})
89 changes: 0 additions & 89 deletions test/validateRouteParams.test.js

This file was deleted.

6 changes: 0 additions & 6 deletions types/instance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,6 @@ export interface FastifyInstance<
SchemaCompiler extends FastifySchema = FastifySchema,
>(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'method' | 'url' | 'constraints'>): FindResult<FindMyWayVersion<RawServer>>;

validateRouteParams<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
SchemaCompiler extends FastifySchema = FastifySchema,
>(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'method' | 'url' | 'constraints'>): boolean;

// addHook: overloads

// Lifecycle addHooks
Expand Down

0 comments on commit b3ad236

Please sign in to comment.