refactor(plugin-workflow): refactor validate context api (#6082)

This commit is contained in:
Junyi 2025-01-17 09:58:28 +08:00 committed by GitHub
parent 18fec90aa8
commit 85b36da746
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 34 additions and 26 deletions

View File

@ -233,16 +233,16 @@ export default class extends Trigger {
);
}
validateConfig(values) {
validateContext(values) {
if (!values.data) {
return {
data: 'Data property is required',
data: 'Data is required',
};
}
if (!values.userId) {
return {
userId: 'UserId property is required',
userId: 'UserId is required',
};
}

View File

@ -96,9 +96,9 @@ function useExecuteConfirmAction() {
const ctx = useActionContext();
const navigate = useNavigateNoUpdate();
const { message: messageApi } = App.useApp();
const { autoRevision, ...values } = form.values;
return {
async run() {
const { autoRevision, ...values } = form.values;
// Not executed, could choose to create new version (by default)
// Executed, stay in current version, and refresh
await form.submit();

View File

@ -83,7 +83,14 @@ export const ScheduleModes = {
nullable: false,
changeOnSelect: true,
render(props) {
return <DatePicker showTime placeholder={lang('Current time')} {...props} value={dayjs(props.value)} />;
return (
<DatePicker
showTime
placeholder={lang('Current time')}
{...props}
value={dayjs(props.value || new Date())}
/>
);
},
},
},

View File

@ -236,10 +236,10 @@ export default class CollectionTrigger extends Trigger {
});
}
validateConfig(values) {
validateContext(values) {
if (!values.data) {
return {
data: 'Data property is required',
data: 'Data is required',
};
}

View File

@ -437,4 +437,13 @@ export default class DateFieldScheduleTrigger {
return this.workflow.trigger(workflow, { ...values, data, date: values?.date ?? new Date() }, options);
}
validateContext(values) {
if (!values?.data) {
return {
data: 'Data is required',
};
}
return null;
}
}

View File

@ -7,8 +7,6 @@
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { Context } from '@nocobase/actions';
import { parseCollectionName } from '@nocobase/data-source-manager';
import Trigger from '..';
import type Plugin from '../../Plugin';
import DateFieldScheduleTrigger from './DateFieldScheduleTrigger';
@ -69,25 +67,19 @@ export default class ScheduleTrigger extends Trigger {
// return !existed.length;
// }
validateConfig(values) {
if (!values.mode) {
validateContext(values) {
if (!values?.mode) {
return {
mode: 'Mode property is required',
mode: 'Mode is required',
};
}
const trigger = this.getTrigger(values.mode);
if (!trigger) {
return {
mode: 'Mode in invalid',
};
}
if (!values.startsOn) {
return {
startsOn: 'StartsOn property is required',
};
}
if (values.mode === SCHEDULE_MODE.DATE_FIELD && !values.collection) {
return {
collection: 'Collection property is required',
};
}
return null;
return trigger.validateContext?.(values);
}
}

View File

@ -20,7 +20,7 @@ export abstract class Trigger {
return true;
}
duplicateConfig?(workflow: WorkflowModel, options: Transactionable): object | Promise<object>;
validateConfig?(values: any): null | void | { [key: string]: string };
validateContext?(values: any): null | void | { [key: string]: string };
sync?: boolean;
execute?(
workflow: WorkflowModel,