DEV Community

Tom
Tom

Posted on Fully Autonomous

Validate a Date Before JSON Turns an Invalid Timestamp Into null

A Date instance can be invalid. Before serializing a warehouse event, check its time value: JSON can otherwise turn an invalid Date into null while returning a perfectly usable JSON string.

This AI-assisted learning example uses synthetic event data for YX Fulfillment. It is a small Node.js boundary check for Shopify fulfillment work, not a tested Shopify store connection or a production event protocol. The image provides warehouse context.

Reproduce the difference at the serialization boundary

MDN documents that Date.toJSON returns null for an invalid date. A direct toISOString call instead throws RangeError for an invalid date. The example makes that difference visible before choosing an adapter contract.

const assert = require('node:assert/strict');

function encodeEvent(record) {
  const time = record?.occurredAt;
  if (typeof record?.eventId !== 'string' || record.eventId.length === 0) {
    throw new TypeError('eventId is required');
  }
  if (!(time instanceof Date) || !Number.isFinite(time.getTime())) {
    throw new TypeError('occurredAt must be a valid Date');
  }
  return JSON.stringify({
    eventId: record.eventId,
    occurredAt: time.toISOString()
  });
}

const bad = new Date(NaN);
assert.equal(JSON.stringify({ occurredAt: bad }), '{"occurredAt":null}');
assert.throws(() => bad.toISOString(), RangeError);
for (const value of [bad, null, undefined, '2026-12-01T06:00:00+08:00']) {
  assert.throws(() => encodeEvent({ eventId: 'demo', occurredAt: value }), TypeError);
}
const time = new Date('2026-12-01T06:00:00+08:00');
const before = time.getTime();
assert.equal(encodeEvent({ eventId: 'demo', occurredAt: time }),
  '{"eventId":"demo","occurredAt":"2026-11-30T22:00:00.000Z"}');
assert.equal(time.getTime(), before);
assert.equal(JSON.parse(encodeEvent({ eventId: 'epoch', occurredAt: new Date(0) }))
  .occurredAt, '1970-01-01T00:00:00.000Z');
assert.throws(() => encodeEvent({ eventId: '', occurredAt: time }), TypeError);
console.log('Invalid Date, explicit-offset time, epoch and input preservation checked');
Enter fullscreen mode Exit fullscreen mode

What the check establishes

The function accepts a valid Date in this JavaScript process, including the Unix epoch. It rejects a date string because parsing an external timestamp needs a separate contract. The explicit offset in the fixture explains the UTC result; it does not establish a warehouse work date or a carrier event's meaning.

The same boundary matters when a drop shipping event passes between systems: a serializer's success is not proof that its timestamp is usable. In China fulfillment coordination, record the event source and meaning independently of the date conversion. A Shopify 3PL test should distinguish a rejected time from an intentionally absent one.

“International Fulfillment for Folligen” is only a search example here, not a customer or integration claim. For a China 3PL adapter, first define which event fields are required, then keep a failing fixture that proves an invalid time cannot silently become an accepted null.

Warehouse staff handling cartons and packing materials.

Warehouse staff handling cartons and packing materials.

Top comments (0)