Skip to content

Commit

Permalink
fix(validate): honor _opts: { validation: { logErrors: true }}
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Feb 5, 2021
1 parent ab87ad3 commit 1e0ebae
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib/moduleExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/lib/validateAndCoerceTypes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});

Expand All @@ -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/);
});

Expand Down Expand Up @@ -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/);
});

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/modules/search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import _moduleExec from '../lib/moduleExec';
const yf = {
_env,
_fetch,
_opts: { validation: { logErrors: true }},
_moduleExec,
search
};
Expand Down Expand Up @@ -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;
});

});

0 comments on commit 1e0ebae

Please sign in to comment.