Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/types/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,8 +977,8 @@ internals.isoDate = function (value) {
return null;
}

if (/.*T.*[+-]\d\d$/.test(value)) { // Add missing trailing zeros to timeshift
value += '00';
if (/T.*[+-]\d\d$/.test(value)) { // Add missing separator and trailing zeros to timeshift
value += ':00';
}

const date = new Date(value);
Expand Down
32 changes: 32 additions & 0 deletions test/types/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -5709,6 +5709,38 @@ describe('string', () => {
const schema = Joi.string().isoDate().allow('x');
Helper.validate(schema, [['x', true]]);
});

it('pads a bare-hour timeshift with a colon, not just zeros', () => {

// A bare-hour offset like "+07" is padded to a full offset before being
// handed to Date(). Node's own parser tolerates "+0700" (basic format),
// but the ISO 8601 extended format used everywhere else in this string
// requires "+07:00" - mixing the two breaks stricter parsers (e.g. Safari).
// Node can't tell these apart itself, so this spies on what string
// actually reaches Date() rather than trusting Date() to reject it.

const NativeDate = global.Date;
const seen = [];

global.Date = class extends NativeDate {

constructor(...args) {

seen.push(args[0]);
super(...args);
}
};

try {
Joi.string().isoDate().validate('2013-06-07T14:21:46+07');
}
finally {
global.Date = NativeDate;
}

expect(seen).to.include('2013-06-07T14:21:46+07:00');
expect(seen).to.not.include('2013-06-07T14:21:46+0700');
});
});

describe('isoDuration()', () => {
Expand Down