Skip to content

Commit bc363b2

Browse files
committed
fix(schema-org): avoid re-casting valid w3c date times
Fixes #296
1 parent 013a02f commit bc363b2

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

packages/schema-org/src/nodes/JobPosting/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('defineJobPosting', () => {
3131
{
3232
"@id": "https://example.com/#job-posting",
3333
"@type": "JobPosting",
34-
"datePosted": "2023-04-01T00:00:00.000Z",
34+
"datePosted": "2023-04-01",
3535
"description": "<p>job description</p>",
3636
"employmentType": [
3737
"FULL_TIME",
@@ -49,7 +49,7 @@ describe('defineJobPosting', () => {
4949
"longitude": 4.8,
5050
},
5151
"title": "Job posting title",
52-
"validThrough": "2024-04-01T00:00:00.000Z",
52+
"validThrough": "2024-04-01",
5353
},
5454
]
5555
`)

packages/schema-org/src/utils/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,24 @@ export function resolvableDateToDate(val: Date | string) {
2121
return typeof val === 'string' ? val : val.toString()
2222
}
2323

24+
const IS_VALID_W3C_DATE = [
25+
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/,
26+
/^\d{4}-[01]\d-[0-3]\d$/,
27+
/^\d{4}-[01]\d$/,
28+
/^\d{4}$/,
29+
]
30+
export function isValidW3CDate(d: string) {
31+
return IS_VALID_W3C_DATE.some(r => r.test(d))
32+
}
33+
2434
export function resolvableDateToIso(val: Date | string | undefined) {
2535
if (!val)
2636
return val
2737
try {
2838
if (val instanceof Date)
2939
return val.toISOString()
40+
else if (isValidW3CDate(val))
41+
return val
3042
else
3143
return new Date(Date.parse(val)).toISOString()
3244
}

0 commit comments

Comments
 (0)