From 603468513d38f8c4e3aab4230922eb038ecb379a Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 14 Jun 2019 09:06:30 -0400 Subject: [PATCH] fix(struct): add encoding check to not pass undefined values (#2) --- index.ts | 2 ++ test.ts | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/index.ts b/index.ts index d5ed1f0..34d0bb2 100644 --- a/index.ts +++ b/index.ts @@ -127,6 +127,8 @@ export const struct = { encode(json: JsonObject): Struct { const fields = {}; Object.keys(json).forEach(key => { + // If value is undefined, do not encode it. + if (!json[key]) return; fields[key] = value.encode(json[key]); }); return {fields}; diff --git a/test.ts b/test.ts index fcbb6d8..88bd9e7 100644 --- a/test.ts +++ b/test.ts @@ -8,6 +8,12 @@ const obj = { yes: true }; +const objWithUndefined = { + foo: 'bar', + yes: true, + isUndefined: undefined +}; + const listValue = { values: [{ kind: 'nullValue', @@ -172,6 +178,11 @@ test('struct.encode', t => { t.deepEqual(actual, structValue); }); +test('struct.encode - undefined value', t => { + const actual = struct.encode(objWithUndefined); + t.deepEqual(actual, structValue); +}); + test('struct.decode', t => { const actual = struct.decode(structValue); t.deepEqual(actual, obj);