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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Validate against data from database #675

Open
maddiehosseini opened this issue Nov 16, 2022 · 1 comment
Open

[Question] Validate against data from database #675

maddiehosseini opened this issue Nov 16, 2022 · 1 comment

Comments

@maddiehosseini
Copy link

maddiehosseini commented Nov 16, 2022

馃殌 Feature request

Current Behavior

I have a type named Contact which contains an accountNumber. Currently I'm only checking if the account number is within a specified hard-coded range:

interface AccountNumberBrand {
  readonly AccountNumber: unique symbol;
}
export const AccountNumber = t.brand(
  t.number,
  (n): n is t.Branded<number,AccountNumberBrand> =>
    n >= 0 && n <= 1000,
  'AccountNumber'
);

type AccountNumber = t.TypeOf<typeof AccountNumber>;

//-----------------------------

const Contact = t.type({
  AccountNumber: t.union([
    withMessage(
      AccountNumber,
      () => ContactValidationErrorType.InvalidAccountNumber
    ),
    t.undefined,
  ]),
});

Desired Behavior

I need to extend the validation to check if the account-number exists in the database or not. So I should be able to either pass the list of existing account numbers or a reference to the async function that gets the data from database.

Who does this impact? Who is this for?

Advanced users, typescript users

Describe alternatives you've considered

I can do the this validation in a separate function and have a 2-steps validation, but I prefer not to do it. Because other team members may later forget to use it and miss part of the validation logic.

Your environment

Software Version(s)
io-ts 2.2.13
TypeScript 4.7.4
@mlegenhausen
Copy link
Contributor

This is out of scope of this library. io-ts is designed to do "pure" validations without side effects. The proposed 2-step validation approach is the one you should go with io-ts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants