Skip to content

Commit

Permalink
Fix serializations of nulls in list
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmartos96 committed Oct 17, 2022
1 parent ce47d9c commit 85495d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/types/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ export default function list(
if (ar === undefined) {
return SKIP;
}
if (ar === null) {
return null;
}

invariant(ar && "length" in ar && "map" in ar, "expected array (like) object");
return ar.map(propSchema.serializer);
},
deserializer: function (jsonArray, done, context) {
if (jsonArray === null) return void done(null, jsonArray);

if (!Array.isArray(jsonArray)) return void done("[serializr] expected JSON array");

function processItem(
Expand Down
13 changes: 13 additions & 0 deletions test/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,19 @@ test("it should respect lists", (t) => {
t.end()
})

test("it should respect lists when null", (t) => {
var schema = _.createSimpleSchema({
x: _.list(primitive()),
})

var source = { x: null }
var json = source
t.deepEqual(serialize(schema, source), json)
t.deepEqual(deserialize(schema, json), source)

t.end()
})

test("it should respect childs", (t) => {
var childSchema = _.createSimpleSchema({
x: primitive(),
Expand Down

0 comments on commit 85495d7

Please sign in to comment.