Skip to content

Commit

Permalink
ArraySchema: ignore, warn, and do not throw when pushing undefined va…
Browse files Browse the repository at this point in the history
…lue.
  • Loading branch information
endel committed Jul 23, 2023
1 parent cf8b1a9 commit 8562af6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/types/ArraySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ export class ArraySchema<V = any> implements Array<V>, SchemaDecoderCallbacks {
}

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

if (value['$changes'] !== undefined) {
(value['$changes'] as ChangeTree).setParent(this, this.$changes.root, index);
}
Expand Down
4 changes: 2 additions & 2 deletions test/ArraySchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { State, Player } from "./Schema";
import { ArraySchema, Schema, type, Reflection, filter, MapSchema, dumpChanges, filterChildren } from "../src";

describe("ArraySchema Tests", () => {
xit("should allow to setAt an undefined value", () => {
it("should not crash when pushing an undefined value", () => {
class Block extends Schema {
@type("number") num: number;
}
Expand All @@ -18,7 +18,7 @@ describe("ArraySchema Tests", () => {

const decodedState = new State();
decodedState.decode(state.encode());
assert.strictEqual(decodedState.blocks.length, 2);
assert.strictEqual(decodedState.blocks.length, 1);
assert.ok(decodedState.blocks.at(0) instanceof Block);
assert.ok(decodedState.blocks.at(1) === undefined);
});
Expand Down

0 comments on commit 8562af6

Please sign in to comment.