Skip to content

Commit

Permalink
feat: decision model schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmiletic committed Jul 5, 2024
1 parent 6de8918 commit e727bc4
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/jdm-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"reactflow": "11.11.3",
"ts-pattern": "^5.1.2",
"use-debounce": "^10.0.0",
"zod": "^3.23.8",
"zustand": "^4.5.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ export const defaultGraph = {
inputs: [
{
id: 'HVo_JpALi8',
type: 'expression',
field: 'cart.weight',
name: 'Cart Weight (Kg)',
},
{
id: 'HW6mSVfLbs',
type: 'expression',
field: 'customer.country',
name: 'Customer Country',
},
Expand All @@ -47,7 +45,6 @@ export const defaultGraph = {
field: 'shippingFee',
id: '3EGDrV0ssV',
name: 'Shipping Fee',
type: 'expression',
},
],
rules: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ export const decisionTableSpecification: NodeSpecification<NodeDecisionTableData
{
id: crypto.randomUUID(),
name: 'Input',
type: 'expression',
},
],
outputs: [
{
id: crypto.randomUUID(),
field: 'output',
name: 'Output',
type: 'expression',
},
],
rules: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export type TableSchemaItem = {
id: string;
name: string;
field?: string;
type: string;
defaultValue?: string;
};

Expand Down Expand Up @@ -84,7 +83,6 @@ export const parseDecisionTable = (decisionTable?: DecisionTableType) => {
{
id: crypto.randomUUID(),
name: 'Input',
type: 'expression',
},
];
}
Expand All @@ -95,7 +93,6 @@ export const parseDecisionTable = (decisionTable?: DecisionTableType) => {
id: crypto.randomUUID(),
field: 'output',
name: 'Output',
type: 'expression',
},
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ export const FieldAdd: React.FC<FieldAddProps> = (props) => {
form={form}
layout='vertical'
initialValues={{ type: 'expression' }}
onFinish={({ type, field, name, defaultValue }) => {
onFinish={({ field, name, defaultValue }) => {
onSuccess?.({
id: crypto.randomUUID(),
type: type || 'expression',
field: (field || '')?.trim?.()?.length > 0 ? field : undefined,
name,
defaultValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ const shippingFeesDefault = {
inputs: [
{
id: 'HVo_JpALi8',
type: 'expression',
field: 'cart.weight',
name: 'Cart Weight (Kg)',
},
{
id: 'HW6mSVfLbs',
type: 'expression',
field: 'customer.country',
name: 'Customer Country',
},
Expand All @@ -28,7 +26,6 @@ const shippingFeesDefault = {
field: 'shippingFee',
id: '3EGDrV0ssV',
name: 'Shipping Fee',
type: 'expression',
},
],
rules: [
Expand Down
3 changes: 0 additions & 3 deletions packages/jdm-editor/src/helpers/excel-file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ const parseSpreadsheetData = (spreadSheetData: any) => {
name: header.value,
field: headerMeta?.name,
_type: headerMeta?.type,
type: 'expression',
id: headerMeta?.id,
defaultValue: '',
};
Expand All @@ -190,7 +189,6 @@ const parseSpreadsheetData = (spreadSheetData: any) => {
id: column.id,
name: column?.name,
field: column?.field,
type: column?.type,
defaultValue: column?.defaultValue,
})) as TableSchemaItem[];

Expand All @@ -200,7 +198,6 @@ const parseSpreadsheetData = (spreadSheetData: any) => {
id: column.id,
name: column?.name,
field: column?.field,
type: column?.type,
defaultValue: column?.defaultValue,
})) as TableSchemaItem[];

Expand Down
128 changes: 128 additions & 0 deletions packages/jdm-editor/src/helpers/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { z } from 'zod';

const id = z.string().default(crypto.randomUUID);

const nodeCommon = z.object({
id,
name: z.string(),
position: z.object({ x: z.number(), y: z.number() }).default({ x: 0, y: 0 }),
});

export const inputNodeSchema = z
.object({
type: z.literal('inputNode'),
})
.merge(nodeCommon);

export const outputNodeSchema = z
.object({
type: z.literal('outputNode'),
})
.merge(nodeCommon);

export const decisionTableSchema = z
.object({
type: z.literal('decisionTableNode'),
content: z.object({
hitPolicy: z.enum(['first', 'collect']).default('first'),
rules: z.array(z.record(z.string(), z.string())).default([]),
inputs: z.array(
z.object({
id,
name: z.string().nullish(),
field: z.string().nullish(),
defaultValue: z.string().nullish(),
}),
),
outputs: z.array(
z.object({
id,
name: z.string().nullish(),
field: z.string().nullish(),
defaultValue: z.string().nullish(),
}),
),
}),
})
.merge(nodeCommon);

export const functionNodeSchema = z
.object({
type: z.literal('functionNode'),
content: z.string().nullish(),
})
.merge(nodeCommon);

export const expressionNodeSchema = z
.object({
type: z.literal('expressionNode'),
content: z.object({
expressions: z.array(
z.object({
id,
key: z.string().default(''),
value: z.string().default(''),
}),
),
}),
})
.merge(nodeCommon);

export const decisionNodeSchema = z
.object({
type: z.literal('decisionNode'),
content: z.object({
key: z.string(),
}),
})
.merge(nodeCommon);

export const switchNodeSchema = z
.object({
type: z.literal('switchNode'),
content: z.object({
hitPolicy: z.enum(['first', 'collect']).default('first'),
statements: z.array(
z.object({
id,
condition: z.string().default(''),
}),
),
}),
})
.merge(nodeCommon);

export const customNodeSchema = z
.object({
type: z.literal('customNode'),
content: z.object({
kind: z.string(),
config: z.any(),
}),
})
.merge(nodeCommon);

export const nodeSchema = z.discriminatedUnion('type', [
decisionNodeSchema,
expressionNodeSchema,
functionNodeSchema,
decisionTableSchema,
switchNodeSchema,
customNodeSchema,
inputNodeSchema,
outputNodeSchema,
]);

export const edgeSchema = z.object({
id: z.string(),
sourceId: z.string(),
targetId: z.string(),
sourceHandle: z.string().nullish(),
type: z.enum(['edge']),
});

export const decisionModelSchema = z.object({
contentType: z.string().default('application/vnd.gorules.decision'),
nodes: z.array(nodeSchema).default([]),
edges: z.array(edgeSchema).default([]),
});
1 change: 1 addition & 0 deletions packages/jdm-editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './components';
export * from './theme';

export { codemirror } from './helpers/codemirror';
export * from './helpers/schema';
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e727bc4

Please sign in to comment.