Skip to content

Commit

Permalink
fix: handle workflow contexts without matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Aug 4, 2024
1 parent e6d6aa3 commit f212b2d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
20 changes: 11 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35483,7 +35483,11 @@ const WorkflowContextSchema = z.object({
const WorkflowContextParser = z.string()
.transform((str, ctx) => {
try {
return JSON.parse(`[${str.replace(/,\s*$/g, '')}]`);
return JSON.parse(`[${str
// fix trailing comma
.replace(/,\s*$/g, '')
// fix missing values
.replace(/,(?=,)/, ',null')}]`);
}
catch (error) {
ctx.addIssue({ code: 'custom', message: error.message });
Expand All @@ -35502,15 +35506,13 @@ const WorkflowContextParser = z.string()
});
return z.NEVER;
}
const matrix = contextArray.shift();
if (matrix != null && typeof matrix !== 'object') {
ctx.addIssue({
code: 'custom',
message: `Value must match the schema: "<JOB_NAME>", [<MATRIX_JSON>], [<JOB_NAME>", [<MATRIX_JSON>], ...]`,
});
return z.NEVER;
if (typeof contextArray[0] === 'object') {
const matrix = contextArray.shift();
context.push({ job, matrix });
}
else {
context.push({ job });
}
context.push({ job, matrix });
}
return z.array(WorkflowContextSchema).parse(context);
});
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,12 @@ type WorkflowContext = z.infer<typeof WorkflowContextSchema>
const WorkflowContextParser = z.string()
.transform((str, ctx) => {
try {
return JSON.parse(`[${str.replace(/,\s*$/g, '')}]`)
return JSON.parse(`[${str
// fix trailing comma
.replace(/,\s*$/g, '')
// fix missing values
.replace(/,(?=,)/, ',null')
}]`)
} catch (error: unknown) {
ctx.addIssue({code: 'custom', message: (error as { message?: string }).message})
return z.NEVER
Expand All @@ -322,15 +327,12 @@ const WorkflowContextParser = z.string()
})
return z.NEVER
}
const matrix = contextArray.shift()
if (matrix != null && typeof matrix !== 'object') {
ctx.addIssue({
code: 'custom',
message: `Value must match the schema: "<JOB_NAME>", [<MATRIX_JSON>], [<JOB_NAME>", [<MATRIX_JSON>], ...]`,
})
return z.NEVER
if (typeof contextArray[0] === 'object') {
const matrix = contextArray.shift()
context.push({job, matrix})
} else {
context.push({job})
}
context.push({job, matrix})
}
return z.array(WorkflowContextSchema).parse(context)
})
Expand Down

0 comments on commit f212b2d

Please sign in to comment.