1
0
Fork 0
mirror of synced 2024-09-20 11:27:56 +12:00

Add extra test

This commit is contained in:
Adria Navarro 2024-07-12 12:17:05 +02:00
parent 99b4aae7de
commit 50c8449f4b
3 changed files with 37 additions and 7 deletions

View file

@ -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)
}

View file

@ -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 }
)
}

View file

@ -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", () => {