From 67fbe24fbdea6c67a006c3c81440c69d48d2ada7 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Fri, 13 Oct 2023 12:24:29 -0500 Subject: [PATCH] Improve json string parser abstraction --- .../customFields/JsonStringParser.spec.ts | 56 +++++++++++-------- .../customFields/JsonStringParser.ts | 19 +++++-- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.spec.ts b/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.spec.ts index 7a81c4ff5..d7c690b46 100644 --- a/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.spec.ts +++ b/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.spec.ts @@ -2,31 +2,43 @@ import JsonStringParser from './JsonStringParser'; describe('JsonStringParser', () => { - it("when an empty array", () => { - expect(new JsonStringParser("[]").results).toStrictEqual([]); - }); - it("when invalid with bracket", () => { - const parser = new JsonStringParser("[") - expect(parser.results).toStrictEqual([]); - }); - it("when invalid with brace", () => { - const parser = new JsonStringParser("[{]") - expect(parser.results).toStrictEqual([]); + describe.each([ + ["with bracket", "["], + ["with brace", "[{]"], + ["with non-custom-field-description", "[{name:'name'}]"] + ])("when invalid %s", (_n, input)=> { + const parser = new JsonStringParser(input) + it('has correct result', () => { + expect(parser.results).toStrictEqual([]); + }); + + it('has error', () => { + expect(parser.errors).not.toBeEmpty(); + }); + + it('is marked not valid', () => { + expect(parser.isValid).toBe(false) + }); }); - it('when invalid with non-custom-field-description', () => { - const parser = new JsonStringParser("[{name:'name'}]") - expect(parser.results).toStrictEqual([]); - }) + describe.each([ + ['with non-custom-field-description', "[{name:'name', label: 'another'}]", [{name: 'name', label: 'another'}]], + ['with different json quote', '[{name:"name", label: "another"}]', [{name: 'name', label: 'another'}]], + ['when an empty array', '[]', []], + ])("when valid %s", (_name, input, result) => { + const parser = new JsonStringParser(input); - it('when valid with non-custom-field-description', () => { - const parser = new JsonStringParser("[{name:'name', label: 'another'}]"); - expect(parser.results).toStrictEqual([{name: 'name', label: 'another'}]); - }); + it('has no errors', () => { + expect(parser.errors).toBeEmpty(); + }); - it('when valid with different json quote', () => { - const parser = new JsonStringParser('[{name:"name", label: "another"}]'); - expect(parser.results).toStrictEqual([{name: 'name', label: 'another'}]); - }); + it('has is marked valid', () => { + expect(parser.isValid).toStrictEqual(true); + }); + + it('matches expected result', () => { + expect(parser.results).toStrictEqual(result); + }); + }); }); \ No newline at end of file diff --git a/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.ts b/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.ts index f02194a6f..a729eac54 100644 --- a/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.ts +++ b/client/js/nonprofits/donate/parseFields/customFields/JsonStringParser.ts @@ -7,14 +7,14 @@ function isCustomFieldDesc(item:unknown) : item is CustomFieldDescription { return typeof item == 'object' && has(item, 'name') && has(item, 'label'); } export default class JsonStringParser { - public error:SyntaxError = null; + public errors:SyntaxError[] = []; public readonly results: CustomFieldDescription[] = []; constructor(public readonly fieldsString:string) { this._parse(); } get isValid(): boolean { - return !!this.error + return this.errors.length == 0; } @@ -22,13 +22,22 @@ export default class JsonStringParser { try { const result = parse(this.fieldsString) if (result instanceof Array) { - result.filter(isCustomFieldDesc).forEach((i) => { - this.results.push({ ...i}); + result.forEach((i) => { + if (isCustomFieldDesc(i)) { + this.results.push({ ...i}); + } + + else { + this.errors.push(new SyntaxError(JSON.stringify(i) + " is not a valid custom field description")) + } }); } + else { + this.errors.push(new SyntaxError("Input did not parse to an array")) + } } catch(e:any) { - this.error = e; + this.errors.push(e) } } }