-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
The coerceTypes: 'array' doesn't coerce one parameter to a single element in array #318
Comments
Can you please include a reproducible example? It might be straightforward but it will greatly simplify fixing it. You could also send a PR, it would be highly welcomed. |
Hi, I dunno how to create a reproducible example that is easy for everyone to see. But I wrote a test, could you copy it to the test directory of If I uncomment line 79 then the test will be passed. |
I've taken a look at this and I think without a reproducible example it's not going to be possible to definitively conclude on this issue. To me it looks as though it could be to do with the way you are registering routes and the validation on your fastify server, as in the below example where username gets coerced into an array. import Fastify from 'fastify'
import Ajv from 'ajv'
import fastifyMultipart from 'fastify-multipart'
function buildServer() {
const fastify = Fastify({
logger: {
prettyPrint: true,
},
})
const ajv = new Ajv({
removeAdditional: true,
useDefaults: true,
coerceTypes: 'array',
})
const opts = {
attachFieldsToBody: true,
sharedSchemaId: '#sharedLoginSchema',
}
fastify.register(fastifyMultipart, opts)
fastify.setValidatorCompiler(({ schema }) => ajv.compile(schema))
// works
fastify.register(async function (fs) {
fs.post(
'/login',
{
schema: {
body: {
type: 'object',
properties: {
username: {
type: 'array',
},
password: {
type: 'string',
},
},
},
},
},
async req => {
const { username, password } = req.body
return { username, password }
}
)
})
// doesn't work
fastify.post(
'/login2',
{
schema: { body: fastify.getSchema('login') },
schema: {
body: {
type: 'object',
properties: {
username: {
type: 'array',
},
password: {
type: 'string',
},
},
},
},
},
async req => {
const { username, password } = req.body
return { username, password }
}
)
fastify.log.info('Fastify is starting up!')
return fastify
}
const fastify = buildServer()
const start = async function () {
try {
await fastify.listen(3000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start() If this is the cause of the confusion (and I agree it is not obvious that you might register the route like this) then perhaps the solution is simply to update the docs. Also, from what I can tell, this isn't to do with fastify-multipart as the same behaviour is seen on all fastify route validation. |
@andrewwood2 could you send a PR to update the docs? |
I am having a similar (or same problem), where I define a property of my schema as a |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hi, I added the example: you'll get this error: P/S: Please also re-open this issue: #313 |
Also bumped into this issue today trying to get a For multipart file uploads, A potential fix would be to change the |
I worked around this by creating a custom ajv plugin import type Ajv from "ajv";
import { DataValidateFunction } from "ajv/dist/types";
export const ajvArrayCoercion = (ajv: Ajv) => {
ajv.addKeyword({
keyword: "coerceObjectArray",
modifying: true,
compile: (schema, parent) => {
delete parent.coerceObjectArray;
const validator: DataValidateFunction = (data, dataCxt) => {
if (!Array.isArray(data) && typeof data === "object") {
// @ts-expect-error No typings for package ajv
dataCxt.parentData[dataCxt.parentDataProperty] = [data];
}
return true;
};
return validator;
},
before: "array",
});
return ajv;
}; Which I then use as multiFileField: {
allOf: [
{
coerceObjectArray: true,
},
{
type: "array",
items: {
isFile: true,
},
},
],
} as unknown as {
type: "array";
items: {
isFile: true;
};
} |
Prerequisites
Fastify version
3.x.x
Plugin version
5.x.x
Node.js version
14.x
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
10
Description
I have got this error when send a POST request.
My schema
Fastify configuration
Steps to Reproduce
It is straightforward.
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: