From 1e0ebae2b516e15242ef592c814519e9a75bdf06 Mon Sep 17 00:00:00 2001 From: Gadi Cohen Date: Fri, 5 Feb 2021 19:10:57 +0200 Subject: [PATCH] fix(validate): honor _opts: { validation: { logErrors: true }} --- src/lib/moduleExec.ts | 2 +- src/lib/validateAndCoerceTypes.spec.ts | 10 +++++----- src/modules/search.spec.ts | 5 +++++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib/moduleExec.ts b/src/lib/moduleExec.ts index efaa91c6..1ff47159 100644 --- a/src/lib/moduleExec.ts +++ b/src/lib/moduleExec.ts @@ -146,7 +146,7 @@ export default async function moduleExec(this: ThisWithFetch, opts: ModuleExecOp * database, etc. Otherwise you'll receive an error. */ try { - validateAndCoerceTypes(result, opts.result.schemaKey, undefined, this._options?.validation); + validateAndCoerceTypes(result, opts.result.schemaKey, undefined, this._opts?.validation); } catch (error) { if (!moduleOpts || moduleOpts.validateResult === undefined || moduleOpts.validateResult === true) throw error; diff --git a/src/lib/validateAndCoerceTypes.spec.ts b/src/lib/validateAndCoerceTypes.spec.ts index be910a64..afc6ffc0 100644 --- a/src/lib/validateAndCoerceTypes.spec.ts +++ b/src/lib/validateAndCoerceTypes.spec.ts @@ -76,7 +76,7 @@ describe('validateAndCoerceTypes', () => { /* @ts-ignore */ result.price.postMarketChangePercent = true; expect( - () => validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY) + () => validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY, undefined, defaultOptions) ).toThrow(/Failed Yahoo Schema/); }); @@ -86,7 +86,7 @@ describe('validateAndCoerceTypes', () => { /* @ts-ignore */ result.price.postMarketChangePercent = { raw: "a string" }; expect( - () => validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY) + () => validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY, undefined, defaultOptions) ).toThrow(/Failed Yahoo Schema/); }); @@ -131,7 +131,7 @@ describe('validateAndCoerceTypes', () => { /* @ts-ignore */ result.price.postMarketTime = "clearly not a date"; expect( - () => validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY) + () => validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY, undefined, defaultOptions) ).toThrow(/Failed Yahoo Schema/); }); @@ -164,7 +164,7 @@ describe('validateAndCoerceTypes', () => { let error: FailedYahooValidationError; try { - validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY) + validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY, undefined, defaultOptions); } catch (e) { error = e; } @@ -240,7 +240,7 @@ describe('validateAndCoerceTypes', () => { let error; try { - validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY) + validateAndCoerceTypes(result, QUERY_RESULT_SCHEMA_KEY, undefined, defaultOptions); } catch (e) { error = e; } diff --git a/src/modules/search.spec.ts b/src/modules/search.spec.ts index e42103bf..7cf4ac35 100644 --- a/src/modules/search.spec.ts +++ b/src/modules/search.spec.ts @@ -8,6 +8,7 @@ import _moduleExec from '../lib/moduleExec'; const yf = { _env, _fetch, + _opts: { validation: { logErrors: true }}, _moduleExec, search }; @@ -36,15 +37,19 @@ describe('search', () => { }); it('throws on unexpected input', async () => { + yf._opts.validation.logErrors = false; await expect(yf.search('AAPL', {}, { devel: 'search-fakeBadResult.json' })) .rejects.toThrow(/Failed Yahoo Schema/) + yf._opts.validation.logErrors = true; }); it('does not throw on unexpected input if called with {validateResult: false}', async () => { + yf._opts.validation.logErrors = false; await expect(yf.search('AAPL', {}, { devel: 'search-fakeBadResult.json', validateResult: false })).resolves.toBeDefined(); + yf._opts.validation.logErrors = true; }); });