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

EntityBuilder has to know if a property should be single or multiple #1

Open
hkan opened this issue Oct 23, 2022 · 0 comments
Open

Comments

@hkan
Copy link
Member

hkan commented Oct 23, 2022

Assuming the following entity definitions:

class Dog extends Entity {
  // ...
}

class Person extends Entity {
  @Type(Dog)
  bestFriend!: Dog;
}

If I create an instance of Person with the following data:

const p = EntityBuilder.buildOne(Person, {
  bestFriend: [
    { name: 'Good' },
    { name: 'Boy' }
  ]
})

(Assume the data comes from an API so it's not type-checkable on build-time.)

It will accept that data, and p.bestFriend will be Dog[] even though the prop definition was a non-array Dog. We should have a way to be explicit about if we want the prop to be an array or not, so that EntityBuilder can reject array data for a singular property.

Proposal: Typed collection decorator

One proposal is only being explicit about arrays so that EntityBuilder will reject arrays if the field is not annotated with the decorator.

class Person extends Entity {
  @Type(Dog)
  bestFriend!: Dog; // this will not be filled with array input

  @CollectionType(Dog)
  pets!: Dog[]; // this will be filled with ONLY array input
}

Proposal: Separate and single-purpose collection decorator

One proposal is only being explicit about arrays so that EntityBuilder will reject arrays if the field is not annotated with the decorator.

class Person extends Entity {
  @Type(Dog)
  bestFriend!: Dog; // this will not be filled with array input

  @Type(Dog)
  @Collection // can be before `@Type` as well.
  pets!: Dog[]; // this will be filled with ONLY array input
}

Upside of a single-purpose collection decorator is that they can be utilized for primitive type properties, as well.

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

1 participant