Updating validators, make sure everything that is optional is really optional.

This commit is contained in:
mike12345567 2022-02-25 11:39:46 +00:00
parent 4b3da534e3
commit c1a1c02e73
3 changed files with 58 additions and 1000 deletions

View File

@ -1,11 +1,13 @@
SELECT 'CREATE DATABASE main' SELECT 'CREATE DATABASE main'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec
CREATE TYPE person_job AS ENUM ('qa', 'programmer', 'designer');
CREATE TABLE Persons ( CREATE TABLE Persons (
PersonID SERIAL PRIMARY KEY, PersonID SERIAL PRIMARY KEY,
LastName varchar(255), LastName varchar(255),
FirstName varchar(255), FirstName varchar(255),
Address varchar(255), Address varchar(255),
City varchar(255) DEFAULT 'Belfast' City varchar(255) DEFAULT 'Belfast',
Type person_job
); );
CREATE TABLE Tasks ( CREATE TABLE Tasks (
TaskID SERIAL PRIMARY KEY, TaskID SERIAL PRIMARY KEY,
@ -35,8 +37,8 @@ CREATE TABLE Products_Tasks (
REFERENCES Tasks(TaskID), REFERENCES Tasks(TaskID),
PRIMARY KEY (ProductID, TaskID) PRIMARY KEY (ProductID, TaskID)
); );
INSERT INTO Persons (FirstName, LastName, Address, City) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast'); INSERT INTO Persons (FirstName, LastName, Address, City, Type) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast', 'qa');
INSERT INTO Persons (FirstName, LastName, Address, City) Values ('John', 'Smith', '64 Updown Road', 'Dublin'); INSERT INTO Persons (FirstName, LastName, Address, City, Type) VALUES ('John', 'Smith', '64 Updown Road', 'Dublin', 'programmer');
INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (1, 2, 'assembling', TRUE); INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (1, 2, 'assembling', TRUE);
INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (2, 1, 'processing', FALSE); INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (2, 1, 'processing', FALSE);
INSERT INTO Products (ProductName) VALUES ('Computers'); INSERT INTO Products (ProductName) VALUES ('Computers');

View File

@ -7,13 +7,16 @@ const {
} = require("@budibase/backend-core/permissions") } = require("@budibase/backend-core/permissions")
const Joi = require("joi") const Joi = require("joi")
const OPTIONAL_STRING = Joi.string().optional().allow(null).allow("")
const OPTIONAL_NUMBER = Joi.number().optional().allow(null)
exports.tableValidator = () => { exports.tableValidator = () => {
// prettier-ignore // prettier-ignore
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({
_id: Joi.string(), _id: OPTIONAL_STRING,
_rev: Joi.string(), _rev: OPTIONAL_STRING,
type: Joi.string().valid("table", "internal", "external"), type: OPTIONAL_STRING.valid("table", "internal", "external"),
primaryDisplay: Joi.string(), primaryDisplay: OPTIONAL_STRING,
schema: Joi.object().required(), schema: Joi.object().required(),
name: Joi.string().required(), name: Joi.string().required(),
views: Joi.object(), views: Joi.object(),
@ -24,7 +27,7 @@ exports.tableValidator = () => {
exports.nameValidator = () => { exports.nameValidator = () => {
// prettier-ignore // prettier-ignore
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({
name: Joi.string(), name: OPTIONAL_STRING,
})) }))
} }
@ -33,21 +36,17 @@ exports.datasourceValidator = () => {
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({
_id: Joi.string(), _id: Joi.string(),
_rev: Joi.string(), _rev: Joi.string(),
// source: Joi.string().valid("POSTGRES_PLUS"), type: OPTIONAL_STRING.allow("datasource_plus"),
type: Joi.string().allow("datasource_plus"),
relationships: Joi.array().items(Joi.object({ relationships: Joi.array().items(Joi.object({
from: Joi.string().required(), from: Joi.string().required(),
to: Joi.string().required(), to: Joi.string().required(),
cardinality: Joi.valid("1:N", "1:1", "N:N").required() cardinality: Joi.valid("1:N", "1:1", "N:N").required()
})), })),
// entities: Joi.array().items(Joi.object({
// type: Joi.string().valid(...Object.values(FieldTypes)).required(),
// name: Joi.string().required(),
// })),
}).unknown(true)) }).unknown(true))
} }
function filterObject() { function filterObject() {
// prettier-ignore
return Joi.object({ return Joi.object({
string: Joi.object().optional(), string: Joi.object().optional(),
fuzzy: Joi.object().optional(), fuzzy: Joi.object().optional(),
@ -57,20 +56,22 @@ function filterObject() {
empty: Joi.object().optional(), empty: Joi.object().optional(),
notEmpty: Joi.object().optional(), notEmpty: Joi.object().optional(),
oneOf: Joi.object().optional(), oneOf: Joi.object().optional(),
}) contains: Joi.object().optional(),
notContains: Joi.object().optional(),
}).unknown(true)
} }
exports.internalSearchValidator = () => { exports.internalSearchValidator = () => {
// prettier-ignore // prettier-ignore
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({
tableId: Joi.string(), tableId: OPTIONAL_STRING,
query: filterObject(), query: filterObject(),
limit: Joi.number().optional(), limit: OPTIONAL_NUMBER,
sort: Joi.string().optional(), sort: OPTIONAL_STRING,
sortOrder: Joi.string().optional(), sortOrder: OPTIONAL_STRING,
sortType: Joi.string().optional(), sortType: OPTIONAL_STRING,
paginate: Joi.boolean().optional(), paginate: Joi.boolean(),
bookmark: Joi.alternatives().try(Joi.string(), Joi.number()).optional(), bookmark: Joi.alternatives().try(OPTIONAL_STRING, OPTIONAL_NUMBER).optional(),
})) }))
} }
@ -79,12 +80,14 @@ exports.externalSearchValidator = () => {
Joi.object({ Joi.object({
query: filterObject(), query: filterObject(),
paginate: Joi.boolean().optional(), paginate: Joi.boolean().optional(),
bookmark: Joi.alternatives().try(Joi.string(), Joi.number()).optional(), bookmark: Joi.alternatives()
limit: Joi.number().optional(), .try(OPTIONAL_STRING, OPTIONAL_NUMBER)
.optional(),
limit: OPTIONAL_NUMBER,
sort: Joi.object({ sort: Joi.object({
column: Joi.string(), column: Joi.string(),
order: Joi.string().optional().valid("ascending", "descending"), order: OPTIONAL_STRING.valid("ascending", "descending"),
type: Joi.string().valid("string", "number"), type: OPTIONAL_STRING.valid("string", "number"),
}).optional(), }).optional(),
}) })
) )
@ -115,8 +118,8 @@ exports.webhookValidator = () => {
// prettier-ignore // prettier-ignore
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({
live: Joi.bool(), live: Joi.bool(),
_id: Joi.string().optional(), _id: OPTIONAL_STRING,
_rev: Joi.string().optional(), _rev: OPTIONAL_STRING,
name: Joi.string().required(), name: Joi.string().required(),
bodySchema: Joi.object().optional(), bodySchema: Joi.object().optional(),
action: Joi.object({ action: Joi.object({
@ -130,15 +133,15 @@ exports.roleValidator = () => {
const permLevelArray = Object.values(PermissionLevels) const permLevelArray = Object.values(PermissionLevels)
// prettier-ignore // prettier-ignore
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({
_id: Joi.string().optional(), _id: OPTIONAL_STRING,
_rev: Joi.string().optional(), _rev: OPTIONAL_STRING,
name: Joi.string().required(), name: Joi.string().required(),
// this is the base permission ID (for now a built in) // this is the base permission ID (for now a built in)
permissionId: Joi.string().valid(...Object.values(BUILTIN_PERMISSION_IDS)).required(), permissionId: Joi.string().valid(...Object.values(BUILTIN_PERMISSION_IDS)).required(),
permissions: Joi.object() permissions: Joi.object()
.pattern(/.*/, [Joi.string().valid(...permLevelArray)]) .pattern(/.*/, [Joi.string().valid(...permLevelArray)])
.optional(), .optional(),
inherits: Joi.string().optional(), inherits: OPTIONAL_STRING,
}).unknown(true)) }).unknown(true))
} }
@ -166,9 +169,9 @@ exports.screenValidator = () => {
_children: Joi.array().required(), _children: Joi.array().required(),
_instanceName: Joi.string().required(), _instanceName: Joi.string().required(),
_styles: Joi.object().required(), _styles: Joi.object().required(),
type: Joi.string().optional(), type: OPTIONAL_STRING,
table: Joi.string().optional(), table: OPTIONAL_STRING,
layoutId: Joi.string().optional(), layoutId: OPTIONAL_STRING,
}).required().unknown(true), }).required().unknown(true),
}).unknown(true)) }).unknown(true))
} }
@ -191,8 +194,8 @@ function generateStepSchema(allowStepTypes) {
exports.automationValidator = (existing = false) => { exports.automationValidator = (existing = false) => {
// prettier-ignore // prettier-ignore
return joiValidator.body(Joi.object({ return joiValidator.body(Joi.object({
_id: existing ? Joi.string().required() : Joi.string(), _id: existing ? Joi.string().required() : OPTIONAL_STRING,
_rev: existing ? Joi.string().required() : Joi.string(), _rev: existing ? Joi.string().required() : OPTIONAL_STRING,
name: Joi.string().required(), name: Joi.string().required(),
type: Joi.string().valid("automation").required(), type: Joi.string().valid("automation").required(),
definition: Joi.object({ definition: Joi.object({

File diff suppressed because it is too large Load Diff