diff --git a/src/schema/yaml-1.1/timestamp.ts b/src/schema/yaml-1.1/timestamp.ts index 2093f3ae..958bf1d7 100644 --- a/src/schema/yaml-1.1/timestamp.ts +++ b/src/schema/yaml-1.1/timestamp.ts @@ -83,12 +83,18 @@ export const timestamp: ScalarTag & { test: RegExp } = { // may be omitted altogether, resulting in a date format. In such a case, the time part is // assumed to be 00:00:00Z (start of day, UTC). test: RegExp( - '^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})' + // YYYY-Mm-Dd + '^([0-9]{4})-?([0-9]{1,2})-?([0-9]{1,2})' + // YYYY-?Mm-?Dd '(?:' + // time is optional '(?:t|T|[ \\t]+)' + // t | T | whitespace - '([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}(\\.[0-9]+)?)' + // Hh:Mm:Ss(.ss)? - '(?:[ \\t]*(Z|[-+][012]?[0-9](?::[0-9]{2})?))?' + // Z | +5 | -03:30 - ')?$' + '([0-9]{1,2})' + // Hh + '(?:' + // minutes are optional + ':?([0-9]{1,2})' + // :?Mm + '(?:' + // seconds are optionals + ':?([0-9]{1,2}(\\.[0-9]{1,3})?)' + // :?Ss(.sss)? + '(?:[ \\t]*(Z|[-+][012]?[0-9](?::?[0-9]{2})?))?' + // Z | +5 | -03:30 + ')?' + // end seconds + ')?' + // end minutes + ')?$' // end time ), resolve(str) { @@ -116,5 +122,5 @@ export const timestamp: ScalarTag & { test: RegExp } = { }, stringify: ({ value }) => - (value as Date).toISOString().replace(/((T00:00)?:00)?\.000Z$/, '') + (value as Date).toISOString().replace(/(((T00)?:00)?:00)?\.000Z$/, '') } diff --git a/tests/doc/types.ts b/tests/doc/types.ts index 31658aa3..db134ca7 100644 --- a/tests/doc/types.ts +++ b/tests/doc/types.ts @@ -660,6 +660,9 @@ english: null --- canonical: 2001-12-15T02:59:43.1Z valid iso8601: 2001-12-14t21:59:43.10-05:00 +no secs: 2001-12-14T01:02 +no mins: 2001-12-14T01 +simplified iso: 20011214T215943.10-5 space separated: 2001-12-14 21:59:43.10 -5 no time zone (Z): 2001-12-15 2:59:43.10 date (00:00:00Z): 2002-12-14` @@ -671,6 +674,9 @@ date (00:00:00Z): 2002-12-14` expect(doc.toJSON()).toMatchObject({ canonical: '2001-12-15T02:59:43.100Z', 'valid iso8601': '2001-12-15T02:59:43.100Z', + 'no secs': '2001-12-14T01:02:00.000Z', + 'no mins': '2001-12-14T01:00:00.000Z', + 'simplified iso': '2001-12-15T02:59:43.100Z', 'space separated': '2001-12-15T02:59:43.100Z', 'no time zone (Z)': '2001-12-15T02:59:43.100Z', 'date (00:00:00Z)': '2002-12-14T00:00:00.000Z' @@ -679,6 +685,9 @@ date (00:00:00Z): 2002-12-14` --- canonical: 2001-12-15T02:59:43.100Z valid iso8601: 2001-12-15T02:59:43.100Z +no secs: 2001-12-14T01:02 +no mins: 2001-12-14T01 +simplified iso: 2001-12-15T02:59:43.100Z space separated: 2001-12-15T02:59:43.100Z no time zone (Z): 2001-12-15T02:59:43.100Z date (00:00:00Z): 2002-12-14\n`)