Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle sync validations #2053

Merged
merged 5 commits into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading