orval icon indicating copy to clipboard operation
orval copied to clipboard

Mock: Invalid mocked values when useDates enabled

Open clintonb opened this issue 1 year ago • 4 comments

What are the steps to reproduce this issue?

  1. Create an Open API spec with dates.
  2. Configure Orval with useDates: true and mock: true.
  3. Generate client

What happens?

The mocks use faker for the mocked values, but convert the fake Date to a string.

What were you expecting to happen?

The mocked data should be a Date.

What versions are you using?

Package Version: 6.31.0

clintonb avatar Jul 14 '24 22:07 clintonb

This also impacts usage of formData since the Date needs to be converted to a string.

clintonb avatar Jul 22 '24 23:07 clintonb

I can work around the mock issue with an override:

module.exports = {
  api: {
      mock: true,
      override: {
        useDates: true,
        mock: {
          format: {
            date: () => faker.date.recent(),
            'date-time': () => faker.date.recent(),
          },
        },
      },
  }
};

clintonb avatar Jul 23 '24 04:07 clintonb

module.exports = {
  api: {
      mock: true,
      override: {
        useDates: true,
                formData: {
          path: './src/api/custom-form-data.ts',
          name: 'customFormData',
        },
      },
  }
};
export const customFormData = <Body>(body: Body): FormData => {
  const formData = new FormData()

  Object.entries(body).forEach(([key, value]) => {
    // Ensure we encode dates as strings
    if (value instanceof Date) {
      value = value.toISOString()
    }

    formData.append(key, value)
  })

  return formData
}

export default customFormData

clintonb avatar Jul 23 '24 04:07 clintonb

Nice solution!

melloware avatar Jul 23 '24 11:07 melloware