orval icon indicating copy to clipboard operation
orval copied to clipboard

Specify mock property overrides for a specific schema

Open zunnzunn opened this issue 1 year ago • 1 comments

Feature Request

The possibility to override generated mock data is very useful for adapting the mocks to one's specific business logic. It is possible to specify an override by property name, which will then affect every property of that name, regardless to what schema it belongs. An example from the docs:

email: () => faker.internet.email(), // matches every "email" property, regardless of its path

It is possible to specify more specific overrides if the property is nested, ( e.g. 'user.id': () => faker.string.uuid()) but what if one has e.g. a schema named Dog and a schema named Cat which both have a name property which is not nested (depth of 1)? How would one specify an override of the name property exclusively for the Dog or exclusively for the Cat schema?

If this is already possible, adding it to the docs would be much appreciated. Otherwise, it would be a great feature to implement!

zunnzunn avatar Feb 14 '24 07:02 zunnzunn

@zunnzunn I can't say that I understand the whole thing, but I looked into it again, as far as I know, there is no way to specify that the output.override.mock.properties option overrides only specific types. However, you can use the overrideResponse parameter to override the response of the generated mock function since v6.24.0. Is this helpful to you?

# generated 
export const getShowPetByIdMock = (overrideResponse?: any) => ({
  id: faker.number.int({min: undefined, max: undefined}),
  name: faker.word.sample(),
  tag: faker.helpers.arrayElement([faker.word.sample(), undefined]), ...overrideResponse
})

# usage
import { getShowPetByIdMock } from 'gen/pets.msw'

const pet = getShowPetByIdMock({name: 'orverride'})
console.log(pet)
// => { id: 7272122785202176, ​name: "orverride", tag: undefined }

soartec-lab avatar Feb 15 '24 00:02 soartec-lab

I'll mark this question as complete and close the issue.

soartec-lab avatar Mar 01 '24 00:03 soartec-lab