Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ and this project adheres to

### Added

- Add validation for edge expressions
[#3704](https://github.com/OpenFn/lightning/issues/3704)

### Changed

- Default failure notifications for project users are now disabled to minimize
Expand Down
14 changes: 12 additions & 2 deletions assets/js/collaborative-editor/components/inspector/EdgeForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useStore } from '@tanstack/react-form';
import { useMemo } from 'react';
import { useEffect, useMemo } from 'react';

import {
useWorkflowActions,
Expand Down Expand Up @@ -30,7 +30,10 @@ export function EdgeForm({ edge }: EdgeFormProps) {
// Initialize form
const form = useAppForm(
{
defaultValues: edge,
defaultValues: {
...edge,
condition_expression: edge.condition_expression || '',
},
listeners: {
onChange: ({ formApi }) => {
if (edge.id) {
Expand Down Expand Up @@ -97,6 +100,13 @@ export function EdgeForm({ edge }: EdgeFormProps) {
conditionExpression
);

// Trigger validation when switching to js_expression
useEffect(() => {
if (conditionType === 'js_expression') {
form.validateField('condition_expression', 'change');
}
}, [conditionType, form]);

return (
<div className="px-6 py-6 space-y-4">
{/* Label Field */}
Expand Down
30 changes: 15 additions & 15 deletions assets/js/collaborative-editor/types/edge.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { z } from "zod";
import { z } from 'zod';

import { uuidSchema } from "./common";
import { uuidSchema } from './common';

export const EdgeConditionType = z.enum([
"on_job_success",
"on_job_failure",
"always",
"js_expression",
'on_job_success',
'on_job_failure',
'always',
'js_expression',
]);

export const EdgeSchema = z
Expand All @@ -23,15 +23,15 @@ export const EdgeSchema = z
target_job_id: uuidSchema,

// Condition configuration
condition_type: EdgeConditionType.default("on_job_success"),
condition_type: EdgeConditionType.default('on_job_success'),
condition_expression: z
.string()
.max(255, "should be at most 255 character(s)")
.nullable()
.optional(),
.trim()
.min(1, "This field can't be blank")
.max(255, 'should be at most 255 character(s)'),
condition_label: z
.string()
.max(255, "should be at most 255 character(s)")
.max(255, 'should be at most 255 character(s)')
.nullable()
.optional(),

Expand All @@ -48,14 +48,14 @@ export const EdgeSchema = z
.refine(
data => {
// Require expression when type is js_expression
if (data.condition_type === "js_expression") {
return !!data.condition_expression;
if (data.condition_type === 'js_expression') {
return !!data.condition_expression.trim();
}
return true;
},
{
message: "can't be blank",
path: ["condition_expression"],
message: "This field can't be blank",
path: ['condition_expression'],
}
);

Expand Down