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

Model doesn't support array of strings, numbers or booleans #97

Closed
juusot opened this issue Jun 2, 2021 · 2 comments
Closed

Model doesn't support array of strings, numbers or booleans #97

juusot opened this issue Jun 2, 2021 · 2 comments
Labels
feature New feature or request

Comments

@juusot
Copy link

juusot commented Jun 2, 2021

Hi.

I cannot create models with array fields. The models support array of arrays or array of object, but not array of strings, numbers or booleans.

Here is an example:

import { primaryKey } from "@mswjs/data";

const car = {
  id: primaryKey((id: string) => id),
  name: (carName: string): string => carName,
  dimensions: () => [1, 2, 3], // <-- error
};

The browser gives the following error:

main.js:27438 Uncaught TypeError: Cannot use 'in' operator to search for '__type' in true
    at Object.isInternalEntity (isInternalEntity.js?0caa:9)
    at eval (removeInternalProperties.js?5ec5:44)
    at Array.map (<anonymous>)
    at eval (removeInternalProperties.js?5ec5:43)
    at Array.map (<anonymous>)
    at Object.removeInternalProperties (removeInternalProperties.js?5ec5:35)
    at Object.create (factory.js?898e:72)
    at handlers (person.mockRequest.ts?f8cc:34)
    at eval (handlers.ts?0e5c:21)
    at Array.flatMap (<anonymous>)

Am I doing something wrong or is this a bug?

Thanks!

@kettanaito
Copy link
Member

Hey, @juusot. Thanks for reporting this.

Array values are currently only supported through the manyOf relation. Using it, you represent an Array of objects (models):

import { factory, primaryKey } from '@mswjs/data'

factory({
  user: {
    id: primaryKey(ID),
    posts: manyOf('post')
  },
  post: {
    id: primaryKey(ID)
  }
})

With this factory, user.posts is an Array of post entities.

We'd like to have support for primitive types, as strings/numbers/booleans as well, but it's not implemented at the moment. If you're interested at tackling this, we'd love your assistance and will help you with the code review.

@kettanaito
Copy link
Member

Closed by #113.

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

Successfully merging a pull request may close this issue.

2 participants