orval
orval copied to clipboard
Mock: Invalid mocked values when useDates enabled
What are the steps to reproduce this issue?
- Create an Open API spec with dates.
- Configure Orval with
useDates: trueandmock: true. - 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
This also impacts usage of formData since the Date needs to be converted to a string.
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(),
},
},
},
}
};
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
Nice solution!