Refactoring around cleanInputValues

This commit is contained in:
Sam Rose 2025-02-13 13:21:31 +00:00
parent 18567f5fe7
commit 4e97f72a43
No known key found for this signature in database
5 changed files with 37 additions and 45 deletions

View File

@ -6,10 +6,10 @@ import {
import sdk from "../sdk"
import {
AutomationAttachment,
BaseIOStructure,
FieldSchema,
FieldType,
Row,
LoopStepType,
LoopStepInputs,
} from "@budibase/types"
import { objectStore, context } from "@budibase/backend-core"
import * as uuid from "uuid"
@ -33,17 +33,15 @@ import path from "path"
*/
export function cleanInputValues<T extends Record<string, any>>(
inputs: T,
schema?: any
schema?: Partial<Record<keyof T, FieldSchema | BaseIOStructure>>
): T {
if (schema == null) {
return inputs
}
for (let inputKey of Object.keys(inputs)) {
const keys = Object.keys(inputs) as (keyof T)[]
for (let inputKey of keys) {
let input = inputs[inputKey]
if (typeof input !== "string") {
continue
}
let propSchema = schema.properties[inputKey]
let propSchema = schema?.[inputKey]
if (!propSchema) {
continue
}
@ -96,7 +94,7 @@ export function cleanInputValues<T extends Record<string, any>>(
*/
export async function cleanUpRow(tableId: string, row: Row) {
let table = await sdk.tables.getTable(tableId)
return cleanInputValues(row, { properties: table.schema })
return cleanInputValues(row, table.schema)
}
export function getError(err: any) {

View File

@ -1,3 +1,4 @@
import { AutomationIOType } from "@budibase/types"
import { cleanInputValues, substituteLoopStep } from "../automationUtils"
describe("automationUtils", () => {
@ -42,15 +43,12 @@ describe("automationUtils", () => {
},
}
expect(
cleanInputValues(
{
row: {
relationship: `[{"_id": "ro_ta_users_us_3"}]`,
},
schema,
cleanInputValues({
row: {
relationship: `[{"_id": "ro_ta_users_us_3"}]`,
},
schema
)
schema,
})
).toEqual({
row: {
relationship: [{ _id: "ro_ta_users_us_3" }],
@ -75,15 +73,12 @@ describe("automationUtils", () => {
},
}
expect(
cleanInputValues(
{
row: {
relationship: `ro_ta_users_us_3`,
},
schema,
cleanInputValues({
row: {
relationship: `ro_ta_users_us_3`,
},
schema
)
schema,
})
).toEqual({
row: {
relationship: "ro_ta_users_us_3",
@ -94,28 +89,27 @@ describe("automationUtils", () => {
it("should be able to clean inputs with the utilities", () => {
// can't clean without a schema
let output = cleanInputValues({ a: "1" })
expect(output.a).toBe("1")
output = cleanInputValues(
const one = cleanInputValues({ a: "1" })
expect(one.a).toBe("1")
const two = cleanInputValues(
{ a: "1", b: "true", c: "false", d: 1, e: "help" },
{
properties: {
a: {
type: "number",
},
b: {
type: "boolean",
},
c: {
type: "boolean",
},
a: {
type: AutomationIOType.NUMBER,
},
b: {
type: AutomationIOType.BOOLEAN,
},
c: {
type: AutomationIOType.BOOLEAN,
},
}
)
expect(output.a).toBe(1)
expect(output.b).toBe(true)
expect(output.c).toBe(false)
expect(output.d).toBe(1)
expect(two.a).toBe(1)
expect(two.b).toBe(true)
expect(two.c).toBe(false)
expect(two.d).toBe(1)
})
})
})

View File

@ -407,7 +407,7 @@ if (descriptions.length) {
client = ds.client!
})
it.only("should query an external database for some data then insert than into an internal table", async () => {
it("should query an external database for some data then insert than into an internal table", async () => {
const newTable = await config.api.table.save({
...basicTable(),
name: "table",

View File

@ -557,7 +557,7 @@ class Orchestrator {
const stepFn = await this.getStepFunctionality(step.stepId)
const inputs = automationUtils.cleanInputValues(
await processObject(cloneDeep(step.inputs), prepareContext(ctx)),
step.schema.inputs
step.schema.inputs.properties
)
const outputs = await stepFn({

View File

@ -146,7 +146,7 @@ export interface Automation extends Document {
}
}
interface BaseIOStructure {
export interface BaseIOStructure {
type?: AutomationIOType
subtype?: AutomationIOType
customType?: AutomationCustomIOType