jquense / yup

Dead simple Object schema validation
MIT License
22.93k stars 934 forks source link

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

Open zbkmr opened 9 months ago

zbkmr commented 9 months ago

@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 commented 8 months ago

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