Skip to content

Using Rules

Handling Async Validations with Inquiry

When a rule needs an async check (e.g., server, database, API), use Inquiry instead of Assertion.

ts
import { Inquiry, Ruleset, RulesBroken } from "self-assert";

function isEmailTaken(email: string): Promise<boolean> {
  return Promise.resolve(email.toLowerCase() === "taken@email.com");
}

const emailMustBeUnique = Inquiry.requiring<string>(
  "user.email.unique",
  "Email must be unique",
  async (email) => !(await isEmailTaken(email))
);

async function runInquiry() {
  try {
    await Ruleset.workOn(emailMustBeUnique.evaluateFor("taken@email.com"));
  } catch (error) {
    if (error instanceof RulesBroken) {
      console.log("Inquiry failed:");
      console.log(error);
    }
  }
}

runInquiry()
  .catch(console.error)
  .finally(() => {
    console.log("Done");
  });

You can mix Assertions and Inquiries in the same Ruleset:

ts
await Ruleset.workOn(
  person.nameNotBlank.evaluateFor(name),
  emailMustBeUnique.evaluateFor(email)
);