Skip to content

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

Description

@hkan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions