From 85e5ade93aed4ef1d857fd68d472f64a470ec2c5 Mon Sep 17 00:00:00 2001 From: sabhas Date: Mon, 19 Jul 2021 13:01:18 +0500 Subject: [PATCH] fix: handle the case when array is passed in getValidJson method --- src/test/utils/getValidJson.spec.ts | 41 +++++++++++++++++++++++++++++ src/test/utils/isValidJson.spec.ts | 31 ---------------------- src/utils/getValidJson.ts | 5 +++- 3 files changed, 45 insertions(+), 32 deletions(-) create mode 100644 src/test/utils/getValidJson.spec.ts delete mode 100644 src/test/utils/isValidJson.spec.ts diff --git a/src/test/utils/getValidJson.spec.ts b/src/test/utils/getValidJson.spec.ts new file mode 100644 index 0000000..e7fbf66 --- /dev/null +++ b/src/test/utils/getValidJson.spec.ts @@ -0,0 +1,41 @@ +import { getValidJson } from '../../utils' + +describe('jsonValidator', () => { + it('should not throw an error with a valid json', () => { + const json = { + test: 'test' + } + + expect(getValidJson(json)).toBe(json) + }) + + it('should not throw an error with a valid json string', () => { + const json = { + test: 'test' + } + + expect(getValidJson(JSON.stringify(json))).toStrictEqual(json) + }) + + it('should throw an error with an invalid json', () => { + const json = `{\"test\":\"test\"\"test2\":\"test\"}` + let errorThrown = false + try { + getValidJson(json) + } catch (error) { + errorThrown = true + } + expect(errorThrown).toBe(true) + }) + + it('should throw an error when an array is passed', () => { + const array = ['hello', 'world'] + let errorThrown = false + try { + getValidJson(array) + } catch (error) { + errorThrown = true + } + expect(errorThrown).toBe(true) + }) +}) diff --git a/src/test/utils/isValidJson.spec.ts b/src/test/utils/isValidJson.spec.ts deleted file mode 100644 index 1901ba1..0000000 --- a/src/test/utils/isValidJson.spec.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { getValidJson } from '../../utils' - -describe('jsonValidator', () => { - it('should not throw an error with an valid json', () => { - const json = { - test: 'test' - } - - expect(getValidJson(json)).toBe(json) - }) - - it('should not throw an error with an valid json string', () => { - const json = { - test: 'test' - } - - expect(getValidJson(JSON.stringify(json))).toStrictEqual(json) - }) - - it('should throw an error with an invalid json', () => { - const json = `{\"test\":\"test\"\"test2\":\"test\"}` - - expect(() => { - try { - getValidJson(json) - } catch (err) { - throw new Error() - } - }).toThrowError - }) -}) diff --git a/src/utils/getValidJson.ts b/src/utils/getValidJson.ts index 738f679..0313157 100644 --- a/src/utils/getValidJson.ts +++ b/src/utils/getValidJson.ts @@ -1,9 +1,12 @@ /** - * Checks if string is in valid JSON format else throw error. + * if string passed then parse the string to json else if throw error for all other types unless it is not a valid json object. * @param str - string to check. */ export const getValidJson = (str: string | object) => { try { + if (Array.isArray(str)) { + throw new Error('Can not parse array object to json.') + } if (typeof str === 'object') return str return JSON.parse(str)