Skip to content

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

Open
@mandrillxx

Description

@mandrillxx

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 { }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions