Fix for validator adding.

This commit is contained in:
mike12345567 2022-02-24 12:03:46 +00:00
parent bc87e2b562
commit d0e0889cc6
7 changed files with 64 additions and 6 deletions

View File

@ -1,5 +1,6 @@
const controller = require("../../controllers/public/applications") const controller = require("../../controllers/public/applications")
const Endpoint = require("./utils/Endpoint") const Endpoint = require("./utils/Endpoint")
const { nameValidator } = require("../utils/validators")
const read = [], const read = [],
write = [] write = []
@ -35,7 +36,11 @@ const read = [],
* applications: * applications:
* $ref: '#/components/examples/applications' * $ref: '#/components/examples/applications'
*/ */
read.push(new Endpoint("post", "/applications/search", controller.search)) read.push(
new Endpoint("post", "/applications/search", controller.search).addMiddleware(
nameValidator()
)
)
/** /**
* @openapi * @openapi

View File

@ -22,6 +22,9 @@ const publicRouter = new Router({
}) })
function addMiddleware(endpoints, middleware) { function addMiddleware(endpoints, middleware) {
if (!Array.isArray(endpoints)) {
endpoints = [endpoints]
}
for (let endpoint of endpoints) { for (let endpoint of endpoints) {
endpoint.addMiddleware(middleware) endpoint.addMiddleware(middleware)
} }

View File

@ -1,5 +1,6 @@
const controller = require("../../controllers/public/queries") const controller = require("../../controllers/public/queries")
const Endpoint = require("./utils/Endpoint") const Endpoint = require("./utils/Endpoint")
const { nameValidator } = require("../utils/validators")
const read = [], const read = [],
write = [] write = []
@ -35,7 +36,11 @@ const read = [],
* queries: * queries:
* $ref: '#/components/examples/queries' * $ref: '#/components/examples/queries'
*/ */
read.push(new Endpoint("post", "/queries/search", controller.search)) read.push(
new Endpoint("post", "/queries/search", controller.search).addMiddleware(
nameValidator()
)
)
/** /**
* @openapi * @openapi

View File

@ -1,6 +1,6 @@
const controller = require("../../controllers/public/tables") const controller = require("../../controllers/public/tables")
const Endpoint = require("./utils/Endpoint") const Endpoint = require("./utils/Endpoint")
const { tableValidator } = require("../utils/validators") const { tableValidator, nameValidator } = require("../utils/validators")
const read = [], const read = [],
write = [] write = []
@ -36,7 +36,11 @@ const read = [],
* tables: * tables:
* $ref: '#/components/examples/tables' * $ref: '#/components/examples/tables'
*/ */
read.push(new Endpoint("post", "/tables/search", controller.search)) read.push(
new Endpoint("post", "/tables/search", controller.search).addMiddleware(
nameValidator()
)
)
/** /**
* @openapi * @openapi

View File

@ -1,5 +1,6 @@
const controller = require("../../controllers/public/users") const controller = require("../../controllers/public/users")
const Endpoint = require("./utils/Endpoint") const Endpoint = require("./utils/Endpoint")
const { nameValidator } = require("../utils/validators")
const read = [], const read = [],
write = [] write = []
@ -32,7 +33,11 @@ const read = [],
* users: * users:
* $ref: '#/components/examples/users' * $ref: '#/components/examples/users'
*/ */
read.push(new Endpoint("post", "/users/search", controller.search)) read.push(
new Endpoint("post", "/users/search", controller.search).addMiddleware(
nameValidator()
)
)
/** /**
* @openapi * @openapi

View File

@ -8,6 +8,7 @@ class Endpoint {
addMiddleware(middleware) { addMiddleware(middleware) {
this.middlewares.push(middleware) this.middlewares.push(middleware)
return this
} }
apply(router) { apply(router) {

View File

@ -5,7 +5,6 @@ const {
BUILTIN_PERMISSION_IDS, BUILTIN_PERMISSION_IDS,
PermissionLevels, PermissionLevels,
} = require("@budibase/backend-core/permissions") } = require("@budibase/backend-core/permissions")
const Joi = require("joi") const Joi = require("joi")
exports.tableValidator = () => { exports.tableValidator = () => {
@ -48,6 +47,42 @@ exports.datasourceValidator = () => {
}).unknown(true)) }).unknown(true))
} }
/**
* * {
* "tableId": "ta_70260ff0b85c467ca74364aefc46f26d",
* "query": {
* "string": {},
* "fuzzy": {},
* "range": {
* "columnName": {
* "high": 20,
* "low": 10,
* }
* },
* "equal": {
* "columnName": "someValue"
* },
* "notEqual": {},
* "empty": {},
* "notEmpty": {},
* "oneOf": {
* "columnName": ["value"]
* }
* },
* "limit": 10,
* "sort": "name",
* "sortOrder": "descending",
* "sortType": "string",
* "paginate": true
* }
*/
exports.searchValidator = () => {
// prettier-ignore
return joiValidator.body(Joi.object({
tableId: Joi.string()
}))
}
exports.datasourceQueryValidator = () => { exports.datasourceQueryValidator = () => {
// prettier-ignore // prettier-ignore
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({