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

How can I use mixed instead of a yup object? #2176

Open
zbkmr opened this issue Feb 13, 2024 · 1 comment
Open

How can I use mixed instead of a yup object? #2176

zbkmr opened this issue Feb 13, 2024 · 1 comment

Comments

@zbkmr
Copy link

zbkmr commented Feb 13, 2024

@jquense Yup After v1 migration, I started to get errors in validations defined as object and I solved the error with this usage.

export interface Account {
  /**
   * Account number
   * @type {string}
   * @memberof Account
   */
  accountNumber: string;
  /**
   * Branch name of the account
   * @type {string}
   * @memberof Account
   */
  branchName: string;
  /**
   * Branch code of the account
   * @type {string}
   * @memberof Account
   */
  branchCode: string;
  /**
   * IBAN number
   * @type {string}
   * @memberof Account
   */
  ibanNo: string;
  /**
   * Name of the account
   * @type {string}
   * @memberof Account
   */
  accountName: string;
  /**
   *
   * @type {Amount}
   * @memberof Account
   */
  balance: Amount;
  /**
   *
   * @type {Amount}
   * @memberof Account
   */
  availableBalance: Amount;
  /**
   * Currency code of the account
   * @type {string}
   * @memberof Account
   */
  currencyCode: string;
  /**
   *
   * @type {AccountType}
   * @memberof Account
   */
  accountType: AccountType;
}
  type AccountParameter = Yup.ObjectShape & Account;
  
  sourceAccount: Yup.object<AccountParameter>().shape({
    accountNumber: Yup.string().required(),
    branchName: Yup.string().required(),
    branchCode: Yup.string().required(),
    ibanNo: Yup.string().required(),
    accountName: Yup.string().required(),
    balance: Yup.object<Amount>().shape({
      currency: Yup.string().required(),
      formattedAmount: Yup.string().required(),
      amountValue: Yup.number().required(),
    }),

However, it is not important for us to define the species mentioned here. And it creates a lot of code clutter. I solve this problem in enums by using mixed, but how can I provide a solution with mixed in the object example I wrote above?

  sourceAccount: Yup.mixed<AccountParameter>().required(
    'general_mandatoryFieldWarning_errorMessage'
  ),

Whenever I try to use it this way I get this error.

Types of property 'iban' are incompatible.
  Type 'RequiredStringSchema<string | undefined, AnyObject>' is not assignable to type 'string'.
@jquense
Copy link
Owner

jquense commented Feb 28, 2024

please follow the issue template, there isn't enough here for me to understand what your issue is and certainly not enough to help debug it

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