-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Message Validations #190
Comments
It's a great feature but possibly a bit too opinionated for WebSocket handling. Maybe it could be behind an option? |
Not really need an option, but we should provide a custom import { WebSocket } from 'ws'
class ValidateWebSocket extends WebSocket {
on (event, handler, options) {
if(event === 'message') {
// wrap the event handler here
}
super.on(event, handler, options)
}
}
import FastifyWebSocket, { ValidateWebSocket } from 'fastify-websocket'
fastify.register(FastifyWebSocket, {
options: { WebSocket: ValidateWebSocket }
})
fastify.get('/', { websocket: true }, function(connection, request) {
connection.socket.on('message', function(data) {
// data here is the validated object
})
}) |
@climba03003 do we really need to pass the I thought about options like: fastify.get('/', { websocket: true, websocketValidation: true }, function(connection, request) {
connection.socket.on('message', function(data) {
// data here is the validated object
})
}) We can detect if there is a schema on the route already but we turn the validation @mcollina do you think it's possible to expose the |
Yes, because overriding I do not see a problem of passing |
That would be great (even though the answer was 2 years ago). I want to use my custom deserializers when receiving messages and serializers on send, but as I can see it's not possible (yet) in @fastify/websocket. Is there any workaround for now? |
Are you up to provide a PR? |
@Uzlopak sorry, I found out that you can specify custom WebSocket instance through |
Prerequisites
🚀 Feature Proposal
What i'm suggesting is using the route schema available already in
fastify
route definition and use it to validate messages payload.Constraint here is we cannot
unemit
an event on the socket, i propose theparsedMessage
event which is used for the validation.message
event handler will still listen to all the events.Problem currently is we cannot have access to the
ajv-compiler
from thefastify
instance. If we can have access to it on within the instance or theonRoute
hook we can compile the schema from the route option and create the validation function for the websocket connection.What do you think?
note: also slight change needed in the
onRoute
hookMotivation
Reviving fastify/help#102
It would be great to have message validations based on the route schemas
Example
No response
The text was updated successfully, but these errors were encountered: