Skip to content

Commit

Permalink
Handle sync validations (#2053)
Browse files Browse the repository at this point in the history
* Handle sync validations

* Flip boolean logic

* Run lint

* Cleanup returns

* Cleanup returns
  • Loading branch information
DaddyWarbucks authored Jul 28, 2023
1 parent 1178437 commit b4627a2
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/util/createValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,23 @@ export default function createValidation(config: {

const shouldSkip = skipAbsent && isAbsent(value);

if (!options.sync) {
try {
Promise.resolve(!shouldSkip ? test.call(ctx, value, ctx) : true).then(
handleResult,
handleError,
);
} catch (err: any) {
handleError(err);
}

return;
if (shouldSkip) {
return handleResult(true);
}

let result: ReturnType<TestFunction>;
try {
result = !shouldSkip ? test.call(ctx, value, ctx) : true;

result = test.call(ctx, value, ctx);
if (typeof (result as any)?.then === 'function') {
throw new Error(
`Validation test of type: "${ctx.type}" returned a Promise during a synchronous validate. ` +
`This test will finish after the validate call has returned`,
if (options.sync) {
throw new Error(
`Validation test of type: "${ctx.type}" returned a Promise during a synchronous validate. ` +
`This test will finish after the validate call has returned`,
);
}
return Promise.resolve(result).then(
handleResult,
handleError,
);
}
} catch (err: any) {
Expand Down

0 comments on commit b4627a2

Please sign in to comment.