-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
115 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
export { zodErrorToReadable } from './zod-error-to-readable'; | ||
|
||
|
12 changes: 7 additions & 5 deletions
12
packages/common/src/utils/zod-error-to-readable/zod-error-to-readable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { ZodError } from 'zod' | ||
import { ZodError } from 'zod'; | ||
|
||
export const zodErrorToReadable = (error: ZodError) => { | ||
return error.issues.map((err) => { | ||
return `${err.path?.join(`.`)}: ${err.message}` | ||
}).join('\n'); | ||
} | ||
return error.issues | ||
.map(err => { | ||
return `${err.path?.join(`.`)}: ${err.message}`; | ||
}) | ||
.join('\n'); | ||
}; |
51 changes: 25 additions & 26 deletions
51
packages/common/src/validations/workflow-validators/api-plugin.validator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,54 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { validate } from "./api-plugin.validator"; | ||
import { ZodError } from 'zod'; | ||
import { validate } from './api-plugin.validator'; | ||
import { ZodError } from 'zod'; | ||
describe('Api Validator', () => { | ||
describe('validate Api Plugin', () => { | ||
|
||
const apiPlugin = { | ||
name: 'ballerineEnrichment', | ||
url: 'https://simple-kyb-demo.s3.eu-central-1.amazonaws.com/mock-data/business_test_us.json', | ||
method: 'GET', | ||
stateNames: ['checkBusinessScore'], | ||
successAction: 'API_CALL_SUCCESS', | ||
errorAction: 'API_CALL_FAILURE', | ||
request: { | ||
transform: { | ||
transformer: 'jq', | ||
mapping: '{data: .entity.id}', | ||
}, | ||
}, | ||
response: { | ||
transform: {transformer: 'jq', mapping: '{result: .}'}, | ||
name: 'ballerineEnrichment', | ||
url: 'https://simple-kyb-demo.s3.eu-central-1.amazonaws.com/mock-data/business_test_us.json', | ||
method: 'GET', | ||
stateNames: ['checkBusinessScore'], | ||
successAction: 'API_CALL_SUCCESS', | ||
errorAction: 'API_CALL_FAILURE', | ||
request: { | ||
transform: { | ||
transformer: 'jq', | ||
mapping: '{data: .entity.id}', | ||
}, | ||
}; | ||
}, | ||
response: { | ||
transform: { transformer: 'jq', mapping: '{result: .}' }, | ||
}, | ||
}; | ||
|
||
describe('when api plugin is valid', () => { | ||
it('does not throw validation exception', async () => { | ||
expect(() => validate(apiPlugin)).not.toThrow() | ||
expect(() => validate(apiPlugin)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('when invalid plugin is valid - invalid request', () => { | ||
it('does not throw validation exception', async () => { | ||
let failingPlugin = structuredClone(apiPlugin); | ||
failingPlugin.request.transform.transformer = {someObjectKey: "someObjectValue"}; | ||
expect(() => validate(failingPlugin)).toThrowError(ZodError) | ||
failingPlugin.request.transform.transformer = { someObjectKey: 'someObjectValue' }; | ||
expect(() => validate(failingPlugin)).toThrowError(ZodError); | ||
}); | ||
}); | ||
|
||
describe('when invalid plugin is valid - missing callback', () => { | ||
it('does not throw validation exception', async () => { | ||
let failingPlugin = structuredClone(apiPlugin); | ||
failingPlugin.errorAction = undefined; | ||
expect(() => validate(failingPlugin)).toThrowError(ZodError) | ||
expect(() => validate(failingPlugin)).toThrowError(ZodError); | ||
}); | ||
}) | ||
}); | ||
|
||
describe('when invalid plugin is valid - stateNames is string', () => { | ||
it('does not throw validation exception', async () => { | ||
let failingPlugin = structuredClone(apiPlugin); | ||
failingPlugin.stateNames = "someState"; | ||
expect(() => validate(failingPlugin)).toThrowError(ZodError) | ||
failingPlugin.stateNames = 'someState'; | ||
expect(() => validate(failingPlugin)).toThrowError(ZodError); | ||
}); | ||
}) | ||
}); | ||
}); | ||
}); |
72 changes: 40 additions & 32 deletions
72
packages/common/src/validations/workflow-validators/api-plugin.validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,53 @@ | ||
import { z } from 'zod'; | ||
import { AnyRecord } from "@/types"; | ||
import { AnyRecord } from '@/types'; | ||
|
||
const RequestSchema = z.object({ | ||
transform: z.object({ | ||
transformer: z.string(), | ||
mapping: z.string(), | ||
}), | ||
schema: z.object({ | ||
$schema: z.string(), | ||
type: z.string(), | ||
properties: z.object({}), | ||
required: z.array(z.string()).optional(), | ||
}).optional(), | ||
schema: z | ||
.object({ | ||
$schema: z.string(), | ||
type: z.string(), | ||
properties: z.object({}), | ||
required: z.array(z.string()).optional(), | ||
}) | ||
.optional(), | ||
}); | ||
|
||
const ResponseSchema = z.object({ | ||
transform: z.object({ | ||
transformer: z.string(), | ||
mapping: z.string(), | ||
}), | ||
schema: z.object({ | ||
$schema: z.string(), | ||
type: z.string(), | ||
properties: z.object({}), | ||
required: z.array(z.string()).optional(), | ||
}).optional(), | ||
}).optional(); | ||
const ResponseSchema = z | ||
.object({ | ||
transform: z.object({ | ||
transformer: z.string(), | ||
mapping: z.string(), | ||
}), | ||
schema: z | ||
.object({ | ||
$schema: z.string(), | ||
type: z.string(), | ||
properties: z.object({}), | ||
required: z.array(z.string()).optional(), | ||
}) | ||
.optional(), | ||
}) | ||
.optional(); | ||
|
||
export const ApiPluginSchema = z.object({ | ||
name: z.string(), | ||
url: z.string().url(), | ||
logo: z.string().optional(), | ||
method: z.string(), | ||
headers: z.record(z.string()).optional(), | ||
stateNames: z.array(z.string()), | ||
successAction: z.string(), | ||
errorAction: z.string(), | ||
request: RequestSchema, | ||
response: ResponseSchema, | ||
}).strict(); | ||
export const ApiPluginSchema = z | ||
.object({ | ||
name: z.string(), | ||
url: z.string().url(), | ||
logo: z.string().optional(), | ||
method: z.string(), | ||
headers: z.record(z.string()).optional(), | ||
stateNames: z.array(z.string()), | ||
successAction: z.string(), | ||
errorAction: z.string(), | ||
request: RequestSchema, | ||
response: ResponseSchema, | ||
}) | ||
.strict(); | ||
|
||
export const validate = (apiPlugin: AnyRecord) => { | ||
return ApiPluginSchema.parse(apiPlugin); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 17 additions & 14 deletions
31
packages/common/src/validations/workflow-validators/workflow-definition-validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
import { z } from 'zod'; | ||
import { ApiPluginSchema } from "./api-plugin.validator"; | ||
import { WebhookPluginSchema } from "./webhook-plugin.validator"; | ||
import { AnyRecord } from "@/types"; | ||
import { zodErrorToReadable } from "../../utils/zod-error-to-readable"; | ||
import { ApiPluginSchema } from './api-plugin.validator'; | ||
import { WebhookPluginSchema } from './webhook-plugin.validator'; | ||
import { AnyRecord } from '@/types'; | ||
import { zodErrorToReadable } from '../../utils/zod-error-to-readable'; | ||
|
||
const ApiOrWebhookSchema = z.union([ApiPluginSchema, WebhookPluginSchema]); | ||
|
||
const WorkflowDefinitionSchema = z.object({ | ||
extensions: z.object({ | ||
statePlugins: z.array(z.record(z.any())).optional(), | ||
apiPlugins: z.array(ApiOrWebhookSchema).optional(), | ||
}).optional(), | ||
}).optional(); | ||
const WorkflowDefinitionSchema = z | ||
.object({ | ||
extensions: z | ||
.object({ | ||
statePlugins: z.array(z.record(z.any())).optional(), | ||
apiPlugins: z.array(ApiOrWebhookSchema).optional(), | ||
}) | ||
.optional(), | ||
}) | ||
.optional(); | ||
export const validateWorkflowDefinition = (workflowDefinition: AnyRecord) => { | ||
const parseResult = WorkflowDefinitionSchema.safeParse(workflowDefinition); | ||
|
||
if (!parseResult.success) { | ||
return {isValid: false, error: zodErrorToReadable(parseResult.error)}; | ||
return { isValid: false, error: zodErrorToReadable(parseResult.error) }; | ||
} | ||
return { isValid: true, error: undefined }; | ||
} | ||
}; | ||
|
||
export const validWorkflowDefinitionOrThrow = (workflowDefinition: AnyRecord) => { | ||
const parseResult = WorkflowDefinitionSchema.parse(workflowDefinition); | ||
|
||
return parseResult; | ||
} | ||
|
||
}; |