From 9dfe7f317b3600e818ed5da135ce6539f30dc82d Mon Sep 17 00:00:00 2001 From: Ricky Boyce Date: Fri, 9 Aug 2024 14:26:09 +1200 Subject: [PATCH] removed redundant line in validate() --- lib/model-validate.js | 1 - lib/util.js | 2 +- test/util.js | 40 +++++++--------------------------------- 3 files changed, 8 insertions(+), 35 deletions(-) diff --git a/lib/model-validate.js b/lib/model-validate.js index 261bd15..d80c4f1 100644 --- a/lib/model-validate.js +++ b/lib/model-validate.js @@ -191,7 +191,6 @@ Model.prototype._validateFields = function (dataRoot, fields, data, opts, parent } // Normalise array indexes and return - if (fieldsIsArray) data2 = data2.filter(() => true) //todo: remove??? if (data === null) data2 = null return [errors, data2] } diff --git a/lib/util.js b/lib/util.js index 375c141..fa60142 100644 --- a/lib/util.js +++ b/lib/util.js @@ -218,7 +218,7 @@ module.exports = { parseDotNotation: function(obj) { /** * Converts dot notation field paths into deep objects - * @param {object} obj - e.g. { 'deep.companyLogos2.1.logo': '' } + * @param {object} obj - e.g. { 'deep.companyLogos2.1.logo': '' } (not mutated) * @return {object} - e.g. { deep: { companyLogos2: [{ logo: '' }] }} */ if (!Object.keys(obj).some(key => key.includes('.'))) return obj diff --git a/test/util.js b/test/util.js index 14a91c4..ad2cf30 100644 --- a/test/util.js +++ b/test/util.js @@ -33,13 +33,14 @@ test('util > parseDotNotation', async () => { 'deep.companyLogo3': 'c', 'deep.companyLogos.0.logo': 'd', 'deep.companyLogos.1.logo': 'e', + 'deep.companyLogos.2': 'f', } const output = { name: 'Martin', deep: { // object first seen here companyLogo2: 'b', companyLogo3: 'c', - companyLogos: [{ logo: 'd' }, { logo: 'e' }], + companyLogos: [{ logo: 'd' }, { logo: 'e' }, 'f'], }, specialInstructions: [ { @@ -79,7 +80,7 @@ test('util > parseDotNotation', async () => { test('util > parseBracketNotation', async () => { const input = { 'name': 'Martin', - // 'pets[]': '', // <-- not supported + // 'pets[]': '', // <-- no longer supported 'deep[companyLogo1]': 'a', // not dot notation 'specialInstructions': [ @@ -107,8 +108,9 @@ test('util > parseBracketNotation', async () => { }, // should be added into above 'deep[companyLogo3]': 'c', - 'deep[companyLogos][0][logo]':'d', - 'deep[companyLogos][1][logo]':'e', + 'deep[companyLogos][0][logo]': 'd', + 'deep[companyLogos][1][logo]': 'e', + 'deep[companyLogos][2]': 'f', } const output = { name: 'Martin', @@ -116,7 +118,7 @@ test('util > parseBracketNotation', async () => { deep: { // object first seen here companyLogo2: 'b', companyLogo3: 'c', - companyLogos: [{ logo: 'd' }, { logo: 'e' }], + companyLogos: [{ logo: 'd' }, { logo: 'e' }, 'f'], }, specialInstructions: [ { @@ -156,34 +158,6 @@ test('util > parseBracketNotation', async () => { ) }) -// test('util > parseBracketToDotNotation', async () => { -// expect(await util.parseBracketToDotNotation({ -// 'name': 'Martin', -// 'pets[]': '', -// 'deep[companyLogo]': 'a', -// 'deep[companyLogos][0]': 'b', -// 'deep[companyLogos2][0][logo]':'c', -// 'deep[companyLogos2][1][logo]': '', -// 'users[0][first]': 'Martin', -// 'users[0][last]': 'Luther', -// 'users[1][first]': 'Bruce', -// 'users[1][last]': 'Lee', -// })).toEqual({ -// name: 'Martin', -// pets: expect.any(Array), -// 'deep.companyLogo': 'a', -// 'deep.companyLogos.0': 'b', -// 'deep.companyLogos2.0.logo': 'c', -// 'deep.companyLogos2.1.logo': '', -// 'users.0.first': 'Martin', -// 'users.0.last': 'Luther', -// 'users.1.first': 'Bruce', -// 'users.1.last': 'Lee', -// }) -// expect(() => util.parseBracketNotation({ 'users[][\'name\']': 'Martin' })).toThrow( -// 'Monastery: Array items in bracket notation need array indexes "users[][\'name\']", e.g. users[0][name]' -// ) -// }) test('util > isId', async () => { expect(util.isId('')).toEqual(false)