-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
JSON schema validation is called for application/octet-stream #1012
Comments
I believe the document need to re-phase. So, after you add a new content type parser. The validation will still happen since it can understand the content now. |
Thanks, that explains the observed behavior. What's the recommended approach to use in this case, can it be disabled per parser (I haven't found that option) or do you have to extend the schema to allow a |
Currently, there is no way to skip the validation for dedicated |
This is what I use as a workaround right now as all other attempts failed for me, I updated my schema like this now to allow schema: {
body: {oneOf: [{type: "array"}, {type: "object", required: ["buffer"], properties: {buffer: {}}}]};
} it's not nice but the only alternative would be to completely drop body validation.
Is there a way to override the validator at the time of the request and remove the body schema? Some things I tried without success:
Would be great if there was an easy way to at least disable validation for a specific content type (i.e. by setting a bool on request body so similar) and ideally should be able to apply this in content type parser. Ideal solution would of course be to specify different schema per content type as this is part of openapi spec as well and should be possible (similar as fastify/fastify#3902 for responses) |
I don't really understand what you are trying to achieve here. Why are you trying to validate a |
This is the issue I faced when receiving @nflaig import Fastify from 'fastify'
const fastify = Fastify()
fastify.addContentTypeParser('application/octet-stream', {
parseAs: 'buffer'
}, async function(request, payload) {
return payload
})
fastify.post('/', {
schema: {
body: { type: 'array' }
},
validatorCompiler() {
return () => true
}
}, async function(request, reply) {
return reply.send(request.body)
})
const response = await fastify.inject({
method: 'POST',
path: '/',
headers: {
'Content-Type': 'application/octet-stream'
},
body: Buffer.from('AAAAAAAA')
})
console.log(response.statusCode)
console.log(response.body) If you need to validate the |
@climba03003 I think we should document this in fastify-swagger. |
@mcollina Thanks for taking a look, my use case is an API that accepts two different content types as body, for example see this openapi definition. I would like to validate the body via JSON schema but only if the content of body is As per openapi spec and the example above, it should be possible to define a schema per content type. A possible solution to support this would be to allow something like this schema: {
body:{
"application/json": {type: "array"},
"application/octet-stream": {}, // no validation
// ...
}
}
Thanks for the suggestion @climba03003. This is kinda what I mentioned in #1012 (comment) (1) but the problem is that contentType is not passed to the validation function and in the example you provided I would assume it skips validation for all requests? Also just a note, the example I posted here is vastly simplified, in practice I still want schema validation for requests with
That would be a possible solution, unfortunately this doesn't work in my case as the serialized data is different from the raw json sent in the request, and converting it to json-like format to pass schema validation would be costly to do and some cases just wanna to use the raw bytes ( While the solution in #1012 (comment) adding a schema that passes What I settled with now is really hacky but does exactly what I want without impacting UX export function addSszContentTypeParser(server) {
// Cache body schema symbol, does not change per request
let bodySchemaSymbol;
server.addContentTypeParser(
"application/octet-stream",
{parseAs: "buffer"},
async (request, payload) => {
if (bodySchemaSymbol === undefined) {
// Get body schema symbol to be able to access validation function
// https://github.com/fastify/fastify/blob/af2ccb5ff681c1d0ac22eb7314c6fa803f73c873/lib/symbols.js#L25
bodySchemaSymbol = Object.getOwnPropertySymbols(request.context).find((s) => s.description === "body-schema");
}
// JSON schema validation will be applied to `Buffer` object, it is required to override validation function
// See https://github.com/fastify/help/issues/1012, it not possible right now to skip validation based on content type
request.context[bodySchemaSymbol] = () => true;
return payload;
}
);
} Maybe others will find this useful, and please forgive me my sins :) |
As per fasitfy docs schema validation should only be called if body is
application/json
My server is configured like this
The schema is really simple
I can confirm that my content type parser is called correctly but I am still getting a schema validation error
As per docs I would expect that JSON schema validation is skipped for non JSON bodies.
Let me know if I am just misreading what the docs are saying and if anyone has an idea how to work around this any hint would be appreciated.
The text was updated successfully, but these errors were encountered: