feat: improved input format

This commit is contained in:
Morten Olsen
2025-05-18 20:26:15 +02:00
parent bc92c91ff8
commit 1cb885bb32
2 changed files with 39 additions and 4 deletions

View File

@@ -22,10 +22,34 @@ const inputHandler: ExecutionHandler = ({
context.input[name] = node.attributes.default;
}
if (node.attributes?.format === 'number' && context.input[name] !== undefined) {
context.input[name] = Number(context.input[name]);
if (context.input[name] !== undefined && isNaN(Number(context.input[name]))) {
throw new Error(`Input "${name}" must be a number, but got "${context.input[name]}"`);
if (node.attributes?.format && context.input[name] !== undefined) {
const format = node.attributes.format;
if (format === 'number') {
context.input[name] = Number(context.input[name]);
if (context.input[name] !== undefined && isNaN(Number(context.input[name]))) {
throw new Error(`Input "${name}" must be a number, but got "${context.input[name]}"`);
}
}
if (format === 'boolean') {
context.input[name] = context.input[name] === 'true';
}
if (format === 'string') {
context.input[name] = String(context.input[name]);
}
if (format === 'json') {
try {
context.input[name] = JSON.parse(String(context.input[name]));
} catch (error) {
throw new Error(`Input "${name}" must be a valid JSON, but got "${context.input[name]}"`);
}
}
if (format === 'date') {
const date = new Date(context.input[name] as string);
if (isNaN(date.getTime())) {
throw new Error(`Input "${name}" must be a valid date, but got "${context.input[name]}"`);
}
context.input[name] = date;
}
}