Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to define getters that depends of another properties #245

Open
soullivaneuh opened this issue Oct 26, 2022 · 1 comment
Open

Allows to define getters that depends of another properties #245

soullivaneuh opened this issue Oct 26, 2022 · 1 comment

Comments

@soullivaneuh
Copy link

I have a modeling like this:

hosting: {
  id: primaryKey(idNumberIncr()),
  name: faker.internet.domainName,
  slug: faker.lorem.slug,
  server: oneOf('server'),
},

On the production API, the slug property has a generated value that depends to the name property.

However, I have no way to reproduce that with the fake data factory, as I don't have the information on the getter, so I have to generate a random one.

Ideally, the simplest implementation way would be this:

hosting: {
  id: primaryKey(idNumberIncr()),
  name: faker.internet.domainName,
  slug: (item) => item.name.whatEverStringTransformation(),
  server: oneOf('server'),
},

Or maybe a way to define a custom function to process the given data on each .create and .update call.

What do you think?

@kettanaito
Copy link
Member

There is no way to do that in v1.

In v2, you can derive a value from another value by using a new derivative() API:

import { id, factory, derivative } from '@mswjs/data'

factory({
  user: {
    id: id(String),
    age: Number,
    isAdult: derivative(({ age }) => age >= 18),
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants