fastify/fast-json-stringify

Tuple of date-time does not allow Date

qwelias opened this issue · 3 comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

latest

Plugin version

No response

Node.js version

20

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Arch

Description

Throws on stringifying Date for schema with {type: 'string',format: 'date-time'}

Steps to Reproduce

require('./index.js')({
  title: 'dates tuple',
  type: 'object',
  properties: {
    dates: {
      type: 'array',
      minItems: 2,
      maxItems: 2,
      items: [
        {
          type: 'string',
          format: 'date-time'
        },
        {
          type: 'string',
          format: 'date-time'
        }
      ]
    }
  }
})({
  dates: [new Date(1), new Date(2)]
}) // Error: Item at 0 does not match schema definition.

Expected Behavior

Does not throw on stringifying Date for schema with {type: 'string',format: 'date-time'}

I've tried to put together a PR for fixing it, but am not sure if need to check for format before allowing Date and if it's ok to just add || instanceof Date in buildArrayTypeCondition's string case (and || instanceof RegExp as in buildMultiTypeSerializer?)

@mcollina hi, I'm willing to contribute here, just need a bit if advice regarding my previous comment when you have time

@qwelias Yes, the logic should be simmilar to the buildMultiTypeSerializer.

typeof ${input} === "string" ||

P.S. IMO this check is redundant, but dropping it would be a breaking change, so we should support this. #524

PR would be welcome. Don't forget to add a test.