Add validation
This commit is contained in:
parent
bf161d9d93
commit
fe31f88cc8
|
@ -2,10 +2,19 @@ import Router from "@koa/router"
|
||||||
import * as rowActionController from "../controllers/rowAction"
|
import * as rowActionController from "../controllers/rowAction"
|
||||||
import { authorizedResource } from "../../middleware/authorized"
|
import { authorizedResource } from "../../middleware/authorized"
|
||||||
|
|
||||||
import { permissions } from "@budibase/backend-core"
|
import { middleware, permissions } from "@budibase/backend-core"
|
||||||
|
import Joi from "joi"
|
||||||
|
|
||||||
const { PermissionLevel, PermissionType } = permissions
|
const { PermissionLevel, PermissionType } = permissions
|
||||||
|
|
||||||
|
export function rowActionValidator() {
|
||||||
|
return middleware.joiValidator.body(
|
||||||
|
Joi.object({
|
||||||
|
name: Joi.string().required(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const router: Router = new Router()
|
const router: Router = new Router()
|
||||||
|
|
||||||
// CRUD endpoints
|
// CRUD endpoints
|
||||||
|
@ -18,11 +27,13 @@ router
|
||||||
.post(
|
.post(
|
||||||
"/api/tables/:tableId/actions",
|
"/api/tables/:tableId/actions",
|
||||||
authorizedResource(PermissionType.TABLE, PermissionLevel.READ, "tableId"),
|
authorizedResource(PermissionType.TABLE, PermissionLevel.READ, "tableId"),
|
||||||
|
rowActionValidator(),
|
||||||
rowActionController.create
|
rowActionController.create
|
||||||
)
|
)
|
||||||
.put(
|
.put(
|
||||||
"/api/tables/:tableId/actions/:actionId",
|
"/api/tables/:tableId/actions/:actionId",
|
||||||
authorizedResource(PermissionType.TABLE, PermissionLevel.READ, "tableId"),
|
authorizedResource(PermissionType.TABLE, PermissionLevel.READ, "tableId"),
|
||||||
|
rowActionValidator(),
|
||||||
rowActionController.update
|
rowActionController.update
|
||||||
)
|
)
|
||||||
.delete(
|
.delete(
|
||||||
|
|
|
@ -71,6 +71,19 @@ describe("/rowsActions", () => {
|
||||||
|
|
||||||
expect(res).toEqual({})
|
expect(res).toEqual({})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("rejects with bad request when creating with no name", async () => {
|
||||||
|
const rowAction: CreateRowActionRequest = {
|
||||||
|
name: undefined as any,
|
||||||
|
}
|
||||||
|
|
||||||
|
await config.api.rowAction.save(table._id!, rowAction, {
|
||||||
|
status: 400,
|
||||||
|
body: {
|
||||||
|
message: 'Invalid body - "name" is required',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("find", () => {
|
describe("find", () => {
|
||||||
|
|
Loading…
Reference in New Issue