Skip to content

Commit

Permalink
Return empty string if transformer gets invalid date
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshino committed Jan 31, 2025
1 parent 60b9d6d commit d001fff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib/services/contents/entry/transformations.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ export const applyTransformation = ({ fieldConfig, value, transformation }) => {
(dateOnly && !!slugPartStr.match(/^\d{4}-[01]\d-[0-3]\d$/)) ||
(dateOnly && !!slugPartStr.match(/T\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?Z$/));

return (useUTC ? moment.utc : moment)(slugPartStr).format(format);
const date = (useUTC ? moment.utc : moment)(slugPartStr);

if (date.isValid()) {
return date.format(format);
}

return '';
}

const defaultTransformer = transformation.match(defaultRegex);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/services/contents/entry/transformations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,12 @@ describe('Test applyTransformation()', () => {
fieldConfig: { name: 'date', widget: 'datetime', time_format: false },
}),
).toBe('January 23, 2024 12:00 AM');
// Invalid date
expect(
applyTransformation({
value: '',
transformation: "date('LL')",
}),
).toBe('');
});
});

0 comments on commit d001fff

Please sign in to comment.