Refactoring around cleanInputValues
This commit is contained in:
parent
18567f5fe7
commit
4e97f72a43
|
@ -6,10 +6,10 @@ import {
|
||||||
import sdk from "../sdk"
|
import sdk from "../sdk"
|
||||||
import {
|
import {
|
||||||
AutomationAttachment,
|
AutomationAttachment,
|
||||||
|
BaseIOStructure,
|
||||||
|
FieldSchema,
|
||||||
FieldType,
|
FieldType,
|
||||||
Row,
|
Row,
|
||||||
LoopStepType,
|
|
||||||
LoopStepInputs,
|
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { objectStore, context } from "@budibase/backend-core"
|
import { objectStore, context } from "@budibase/backend-core"
|
||||||
import * as uuid from "uuid"
|
import * as uuid from "uuid"
|
||||||
|
@ -33,17 +33,15 @@ import path from "path"
|
||||||
*/
|
*/
|
||||||
export function cleanInputValues<T extends Record<string, any>>(
|
export function cleanInputValues<T extends Record<string, any>>(
|
||||||
inputs: T,
|
inputs: T,
|
||||||
schema?: any
|
schema?: Partial<Record<keyof T, FieldSchema | BaseIOStructure>>
|
||||||
): T {
|
): T {
|
||||||
if (schema == null) {
|
const keys = Object.keys(inputs) as (keyof T)[]
|
||||||
return inputs
|
for (let inputKey of keys) {
|
||||||
}
|
|
||||||
for (let inputKey of Object.keys(inputs)) {
|
|
||||||
let input = inputs[inputKey]
|
let input = inputs[inputKey]
|
||||||
if (typeof input !== "string") {
|
if (typeof input !== "string") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
let propSchema = schema.properties[inputKey]
|
let propSchema = schema?.[inputKey]
|
||||||
if (!propSchema) {
|
if (!propSchema) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -96,7 +94,7 @@ export function cleanInputValues<T extends Record<string, any>>(
|
||||||
*/
|
*/
|
||||||
export async function cleanUpRow(tableId: string, row: Row) {
|
export async function cleanUpRow(tableId: string, row: Row) {
|
||||||
let table = await sdk.tables.getTable(tableId)
|
let table = await sdk.tables.getTable(tableId)
|
||||||
return cleanInputValues(row, { properties: table.schema })
|
return cleanInputValues(row, table.schema)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getError(err: any) {
|
export function getError(err: any) {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { AutomationIOType } from "@budibase/types"
|
||||||
import { cleanInputValues, substituteLoopStep } from "../automationUtils"
|
import { cleanInputValues, substituteLoopStep } from "../automationUtils"
|
||||||
|
|
||||||
describe("automationUtils", () => {
|
describe("automationUtils", () => {
|
||||||
|
@ -42,15 +43,12 @@ describe("automationUtils", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
expect(
|
expect(
|
||||||
cleanInputValues(
|
cleanInputValues({
|
||||||
{
|
|
||||||
row: {
|
row: {
|
||||||
relationship: `[{"_id": "ro_ta_users_us_3"}]`,
|
relationship: `[{"_id": "ro_ta_users_us_3"}]`,
|
||||||
},
|
},
|
||||||
schema,
|
schema,
|
||||||
},
|
})
|
||||||
schema
|
|
||||||
)
|
|
||||||
).toEqual({
|
).toEqual({
|
||||||
row: {
|
row: {
|
||||||
relationship: [{ _id: "ro_ta_users_us_3" }],
|
relationship: [{ _id: "ro_ta_users_us_3" }],
|
||||||
|
@ -75,15 +73,12 @@ describe("automationUtils", () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
expect(
|
expect(
|
||||||
cleanInputValues(
|
cleanInputValues({
|
||||||
{
|
|
||||||
row: {
|
row: {
|
||||||
relationship: `ro_ta_users_us_3`,
|
relationship: `ro_ta_users_us_3`,
|
||||||
},
|
},
|
||||||
schema,
|
schema,
|
||||||
},
|
})
|
||||||
schema
|
|
||||||
)
|
|
||||||
).toEqual({
|
).toEqual({
|
||||||
row: {
|
row: {
|
||||||
relationship: "ro_ta_users_us_3",
|
relationship: "ro_ta_users_us_3",
|
||||||
|
@ -94,28 +89,27 @@ describe("automationUtils", () => {
|
||||||
|
|
||||||
it("should be able to clean inputs with the utilities", () => {
|
it("should be able to clean inputs with the utilities", () => {
|
||||||
// can't clean without a schema
|
// can't clean without a schema
|
||||||
let output = cleanInputValues({ a: "1" })
|
const one = cleanInputValues({ a: "1" })
|
||||||
expect(output.a).toBe("1")
|
expect(one.a).toBe("1")
|
||||||
output = cleanInputValues(
|
|
||||||
|
const two = cleanInputValues(
|
||||||
{ a: "1", b: "true", c: "false", d: 1, e: "help" },
|
{ a: "1", b: "true", c: "false", d: 1, e: "help" },
|
||||||
{
|
{
|
||||||
properties: {
|
|
||||||
a: {
|
a: {
|
||||||
type: "number",
|
type: AutomationIOType.NUMBER,
|
||||||
},
|
},
|
||||||
b: {
|
b: {
|
||||||
type: "boolean",
|
type: AutomationIOType.BOOLEAN,
|
||||||
},
|
},
|
||||||
c: {
|
c: {
|
||||||
type: "boolean",
|
type: AutomationIOType.BOOLEAN,
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
expect(output.a).toBe(1)
|
expect(two.a).toBe(1)
|
||||||
expect(output.b).toBe(true)
|
expect(two.b).toBe(true)
|
||||||
expect(output.c).toBe(false)
|
expect(two.c).toBe(false)
|
||||||
expect(output.d).toBe(1)
|
expect(two.d).toBe(1)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -407,7 +407,7 @@ if (descriptions.length) {
|
||||||
client = ds.client!
|
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({
|
const newTable = await config.api.table.save({
|
||||||
...basicTable(),
|
...basicTable(),
|
||||||
name: "table",
|
name: "table",
|
||||||
|
|
|
@ -557,7 +557,7 @@ class Orchestrator {
|
||||||
const stepFn = await this.getStepFunctionality(step.stepId)
|
const stepFn = await this.getStepFunctionality(step.stepId)
|
||||||
const inputs = automationUtils.cleanInputValues(
|
const inputs = automationUtils.cleanInputValues(
|
||||||
await processObject(cloneDeep(step.inputs), prepareContext(ctx)),
|
await processObject(cloneDeep(step.inputs), prepareContext(ctx)),
|
||||||
step.schema.inputs
|
step.schema.inputs.properties
|
||||||
)
|
)
|
||||||
|
|
||||||
const outputs = await stepFn({
|
const outputs = await stepFn({
|
||||||
|
|
|
@ -146,7 +146,7 @@ export interface Automation extends Document {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BaseIOStructure {
|
export interface BaseIOStructure {
|
||||||
type?: AutomationIOType
|
type?: AutomationIOType
|
||||||
subtype?: AutomationIOType
|
subtype?: AutomationIOType
|
||||||
customType?: AutomationCustomIOType
|
customType?: AutomationCustomIOType
|
||||||
|
|
Loading…
Reference in New Issue