Skip to content

Commit

Permalink
issue-54 Workflow params validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilt committed Nov 26, 2023
1 parent 05fc989 commit c51cb41
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/inputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,4 +18,5 @@ export {
DEV_SETTINGS,
AdapterMethods,
ADAPTER_METHODS,
WORKFLOW,
};
4 changes: 2 additions & 2 deletions src/inputs/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ 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 };
};

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 };
Expand Down
33 changes: 33 additions & 0 deletions src/inputs/workflow.ts
Original file line number Diff line number Diff line change
@@ -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> = {
[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]
}
};
10 changes: 9 additions & 1 deletion src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/index';
import {
WorkflowParams,
ProcessName,
Expand Down Expand Up @@ -31,7 +32,14 @@ export class Workflow<ItemData = unknown> {

scroller: Scroller<ItemData>;

constructor({ element, datasource, consumer, run, Routines }: WorkflowParams<ItemData>) {
constructor(params: WorkflowParams<ItemData>) {
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;
Expand Down

0 comments on commit c51cb41

Please sign in to comment.