diff --git a/packages/backend-core/src/middleware/joi-validator.ts b/packages/backend-core/src/middleware/joi-validator.ts index 5047cdbbc1..7575e7e778 100644 --- a/packages/backend-core/src/middleware/joi-validator.ts +++ b/packages/backend-core/src/middleware/joi-validator.ts @@ -4,8 +4,9 @@ import { Ctx } from "@budibase/types" function validate( schema: Joi.ObjectSchema | Joi.ArraySchema, property: string, - opts: { errorPrefix: string } = { errorPrefix: `Invalid ${property}` } + opts?: { errorPrefix?: string; allowUnknown?: boolean } ) { + const errorPrefix = opts?.errorPrefix || `Invalid ${property}` // Return a Koa middleware function return (ctx: Ctx, next: any) => { if (!schema) { @@ -28,10 +29,12 @@ function validate( }) } - const { error } = schema.validate(params) + const { error } = schema.validate(params, { + allowUnknown: opts?.allowUnknown, + }) if (error) { let message = error.message - if (opts.errorPrefix) { + if (errorPrefix) { message = `Invalid ${property} - ${message}` } ctx.throw(400, message) @@ -42,7 +45,7 @@ function validate( export function body( schema: Joi.ObjectSchema | Joi.ArraySchema, - opts?: { errorPrefix: string } + opts?: { errorPrefix?: string; allowUnknown?: boolean } ) { return validate(schema, "body", opts) } diff --git a/packages/server/src/api/routes/rowAction.ts b/packages/server/src/api/routes/rowAction.ts index 3ec00dff4d..f4f20822d1 100644 --- a/packages/server/src/api/routes/rowAction.ts +++ b/packages/server/src/api/routes/rowAction.ts @@ -11,9 +11,8 @@ export function rowActionValidator() { return middleware.joiValidator.body( Joi.object({ name: Joi.string().required(), - id: Joi.optional(), - tableId: Joi.optional(), - }) + }), + { allowUnknown: true } ) } diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index 976dc950e4..68dcc484c4 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -139,6 +139,34 @@ describe("/rowsActions", () => { }, }) }) + + it("ignores not valid row action data", async () => { + const rowAction = createRowActionRequest() + const dirtyRowAction = { + ...rowAction, + id: generator.guid(), + valueToIgnore: generator.word(), + } + const res = await createRowAction(tableId, dirtyRowAction, { + status: 201, + }) + + expect(res).toEqual({ + id: expect.any(String), + tableId, + ...rowAction, + }) + + expect(await config.api.rowAction.find(tableId)).toEqual({ + actions: { + [res.id]: { + ...rowAction, + id: res.id, + tableId: tableId, + }, + }, + }) + }) }) describe("find", () => {