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

[Feature]: null/undefined support for ArraySchema #175

Open
mandrillxx opened this issue Jul 29, 2024 · 1 comment
Open

[Feature]: null/undefined support for ArraySchema #175

mandrillxx opened this issue Jul 29, 2024 · 1 comment

Comments

@mandrillxx
Copy link

Description & Use case

Description:
Add the ability to add null/undefined objects within ArraySchema type

currently, this is restricted by this check in src/types/ArraySchema.ts#182

if (value === undefined || value === null) {
   console.error("ArraySchema items cannot be null nor undefined; Use `deleteAt(index)` instead.");
   return;
}

Usecase:
My backend for a poker game is already made, and an array is made for the player seating:

export class Table {
   private _tablePlayers = (Player | null)[];

   constructor(numSeats = 4) {
      this._tablePlayers = new Array(numSeats).fill(null);
   }
}

and somebody can join any seat index, meaning the array can look like this:

[
   null,
   null,
   { username: "player" },
   null,
]

but when converting to a schema

export class Table extends Schema {
   @type([Player]) _tablePlayers = new ArraySchema<Player>();

   constructor(numSeats = 4) {
      new Array(numSeats).fill(null).forEach((seat, index) => (this._tablePlayers[index] = seat));
   }
}

this will no longer work, and the array will be empty. as seen in the failing test
image

alongside null/undefined being integral parts of typescript/javascript, so maybe exclusively for js ecosystem

Optional: Proposed API

maybe a separate schema type, such as NullableArraySchema

export class NullableArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks { }
@endel
Copy link
Member

endel commented Jul 29, 2024

Thanks for the suggestion - that'd be a nice addition, can't promise but will try to make it into #173

For now, I'd suggest inserting empty Player instances instead of having the null values.

@endel endel changed the title [Feature]: null/undefined support for schema [Feature]: null/undefined support for ArraySchema Jul 29, 2024
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