Skip to content

Commit

Permalink
fix validated row new column
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Jan 22, 2024
1 parent e1775c5 commit 41e2917
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/DboBuilder/TableHandler/DataValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,17 @@ const getValidatedRowFieldData = async ({ allowedCols, rows, validationOptions,
rows.map(async nonvalidatedRow => {

let row = pickKeys(nonvalidatedRow, allowedCols);
const initialRowKeys = Object.keys(row);
if (validationOptions.validate) {
if(!validationOptions.localParams){
throw "localParams missing for validate";
}
row = await validationOptions.validate({ row, dbx: dbTx, localParams: validationOptions.localParams });
}
const keysAddedDuringValidate = Object.keys(row).filter(newKey => !initialRowKeys.includes(newKey));

const getColumn = (fieldName: string) => {
if (!allowedCols.includes(fieldName)) {
if (!allowedCols.concat(keysAddedDuringValidate).includes(fieldName)) {
throw `Unexpected/Dissallowed column name: ${fieldName}`;
}
const column = tableHandler.columns.find(c => c.name === fieldName);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.7",
"version": "4.2.8",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/client_only_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export default async function client_only(db: DBHandlerClient, auth: Auth, log:
]);

const cols = await db.insert_rules.getColumns!();
assert.equal(cols.filter(({ insert, update: u, select: s, delete: d }) => insert && !u && s && !d).length, 3, "Validated getColumns failed")
assert.equal(cols.filter(({ insert, update: u, select: s, delete: d }) => insert && !u && s && !d).length, 2, "Validated getColumns failed")

/* Validated insert */
const expectB = await db.insert_rules.insert!({ name: "a" }, { returning: "*" });
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions tests/server/testPublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ export const testPublish: Publish<DBSchemaGenerated> = async ({ user, sid }) =>
insert_rules: {
select: "*",
insert: {
fields: "*",
fields: { added: 0 },
returningFields: { name: 1 },
validate: async ({ row }) => {
if(row.name === "a") row.name = "b"
if(row.name === "a") row.name = "b";
row.added = (new Date()).toUTCString();
return row
},
checkFilter: {
Expand Down

0 comments on commit 41e2917

Please sign in to comment.