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

Issue with adding a custom method #2234

Open
t0rum89 opened this issue Jul 24, 2024 · 0 comments
Open

Issue with adding a custom method #2234

t0rum89 opened this issue Jul 24, 2024 · 0 comments

Comments

@t0rum89
Copy link

t0rum89 commented Jul 24, 2024

I'm trying to add a custom method to Yup for my schemas.

declare module 'yup' {
  interface StringSchema {
    password(message?: string): StringSchema;
  }
}

function password(
  this: Yup.StringSchema,
  message: string = 'Password should be between 6 and 40 characters long',
) {
  return this.min(6, message).max(40, message);
}

Yup.addMethod(Yup.string, 'password', password);

And I'm going to use it like this:

export const changePasswordValidationSchema = Yup.object().shape({
  current_password: Yup.string().required('Current password is required').password(),
  password: Yup.string().required('New password is required').password(),
});

But in that case I get a possible undefined for my fields:

const changePasswordValidationSchema: Yup.ObjectSchema<{
    current_password: string | undefined;
    password: string | undefined;
}, Yup.AnyObject, {
    current_password: undefined;
    password: undefined;
}, "">

which causes an error in the resolver:

  const form = useForm<UpdatePasswordPayload>({
    resolver: yupResolver(changePasswordValidationSchema),
    defaultValues: {
      current_password: '',
      password: '',
    },
  });
Type 'Resolver<{ password?: string | undefined; current_password?: string | undefined; }>' is not assignable to type 'Resolver<UpdatePasswordPayload, any>'.
  Types of parameters 'options' and 'options' are incompatible.
    Type 'ResolverOptions<UpdatePasswordPayload>' is not assignable to type 'ResolverOptions<{ password?: string | undefined; current_password?: string | undefined; }>'.
      Type '{ password?: string | undefined; current_password?: string | undefined; }' is not assignable to type 'UpdatePasswordPayload'.ts(2322)
(property) resolver?: Resolver<UpdatePasswordPayload, any> | undefined

My type:

export type UpdatePasswordPayload = {
  current_password: string;
  password: string;
};

I have tried changing the order in the schema to:

export const changePasswordValidationSchema = Yup.object().shape({
  current_password: Yup.string().password().required('Current password is required'),
  password: Yup.string().password().required('New password is required'),
});

But in this case, methods called after mine don't work. Perhaps the context .this lost for them. How i can fix this?

@t0rum89 t0rum89 changed the title Custom method Issue with adding a custom method Jul 24, 2024
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

1 participant