From 0d1dd1a1242cffe4d27a2e2e9712052d79a28cef Mon Sep 17 00:00:00 2001 From: Denis Hilt Date: Sun, 26 Nov 2023 22:32:57 +0100 Subject: [PATCH] issue-54 Workflow params validation --- src/inputs/index.ts | 2 ++ src/inputs/validation.ts | 4 ++-- src/inputs/workflow.ts | 33 +++++++++++++++++++++++++++++++++ src/workflow.ts | 10 +++++++++- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/inputs/workflow.ts diff --git a/src/inputs/index.ts b/src/inputs/index.ts index bd3b055a..8ecf4445 100644 --- a/src/inputs/index.ts +++ b/src/inputs/index.ts @@ -3,6 +3,7 @@ import { ValidatorType, VALIDATORS, validateOne, validate } from './validation'; import { DatasourceProps, DATASOURCE } from './datasource'; import { SETTINGS, DEV_SETTINGS } from './settings'; import { AdapterMethods, ADAPTER_METHODS } from './adapter'; +import { WORKFLOW } from './workflow'; export { Direction, @@ -17,4 +18,5 @@ export { DEV_SETTINGS, AdapterMethods, ADAPTER_METHODS, + WORKFLOW, }; diff --git a/src/inputs/validation.ts b/src/inputs/validation.ts index c2b8830a..c7da1f52 100644 --- a/src/inputs/validation.ts +++ b/src/inputs/validation.ts @@ -102,7 +102,7 @@ const onBoolean = (value: unknown): ValidatedValue => { const onObject = (value: unknown): ValidatedValue => { const errors = []; - if (Object.prototype.toString.call(value) !== '[object Object]') { + if (!!value && Object.prototype.toString.call(value) !== '[object Object]') { errors.push(ValidatorType.object); } return { value, isSet: true, isValid: !errors.length, errors }; @@ -110,7 +110,7 @@ const onObject = (value: unknown): ValidatedValue => { const onHtmlElement = (value: unknown): ValidatedValue => { const errors = []; - if (!(value instanceof Element) && !(value instanceof HTMLDocument)) { + if (!(value instanceof Element) && !(value instanceof Document)) { errors.push(ValidatorType.element); } return { value, isSet: true, isValid: !errors.length, errors }; diff --git a/src/inputs/workflow.ts b/src/inputs/workflow.ts new file mode 100644 index 00000000..f1aacd65 --- /dev/null +++ b/src/inputs/workflow.ts @@ -0,0 +1,33 @@ +import { VALIDATORS } from './validation'; +import { ICommonProps } from '../interfaces/index'; + +const { ELEMENT, OBJECT, FUNC, FUNC_WITH_X_ARGUMENTS } = VALIDATORS; + +export enum WorkflowProps { + consumer = 'consumer', + element = 'element', + datasource = 'datasource', + run = 'run', + Routines = 'Routines', +} + +export const WORKFLOW: ICommonProps = { + [WorkflowProps.consumer]: { + validators: [OBJECT] + }, + [WorkflowProps.element]: { + validators: [ELEMENT], + mandatory: true + }, + [WorkflowProps.datasource]: { + validators: [OBJECT], + mandatory: true + }, + [WorkflowProps.run]: { + validators: [FUNC_WITH_X_ARGUMENTS(1)], + mandatory: true + }, + [WorkflowProps.Routines]: { + validators: [FUNC] + } +}; diff --git a/src/workflow.ts b/src/workflow.ts index 9e31b41a..556c7abd 100644 --- a/src/workflow.ts +++ b/src/workflow.ts @@ -3,6 +3,7 @@ import { runStateMachine } from './workflow-transducer'; import { Reactive } from './classes/reactive'; import { Item } from './classes/item'; import { CommonProcess, ProcessStatus as Status, } from './processes/index'; +import { WORKFLOW, validate } from './inputs'; import { WorkflowParams, ProcessName, @@ -31,7 +32,14 @@ export class Workflow { scroller: Scroller; - constructor({ element, datasource, consumer, run, Routines }: WorkflowParams) { + constructor(params: WorkflowParams) { + const { element, datasource, consumer, run, Routines } = params; + + const validationResult = validate(params, WORKFLOW); + if (!validationResult.isValid) { + throw new Error(`Invalid Workflow params: ${validationResult.errors.join(', ')}.`); + } + this.isInitialized = false; this.disposed = false; this.initTimer = null;