Formik: Why limit error to just a string?

Created on 8 May 2019  路  3Comments  路  Source: formium/formik

馃悰 Bug report

Current Behavior

Form errors are currently of type string. In most other form validation libraries I've used this is an any type so a user could do things like the following:

setErrors({
  password: ['Must be more than 8 characters', 'Must contain a special character']
})

or for localization:

setErrors({
  password: [{type: 'min', min: 3, value: 1}]
})
setErrors({
  password: [{localizationKey: 'blah', value: 1}]
})

Is there any reason why it needs to strictly be of type string? It actually works setting it to whatever I want so long as I cast it but makes working with TypeScript funky and limits the flexibility of users.

Similar to #1244, #1293

stale

Most helpful comment

Yes, understood (as I mentioned in the original post It actually works setting it to whatever I want so long as I cast).

It's just the types are wrong ATM:

export type FormikErrors<Values> = {
  [K in keyof Values]?: Values[K] extends object
    ? FormikErrors<Values[K]>
    : string
};

setErrors(errors: FormikErrors<Values>): void;
setFieldError(field: keyof Values & string, message: string): void;
setFieldError(field: string, message: string): void;

All 3 comments

Errors are not limited to strings. You can use whatever data structure you wish. As you mentioned, we should fix TS typings to not complain about this.

Yes, understood (as I mentioned in the original post It actually works setting it to whatever I want so long as I cast).

It's just the types are wrong ATM:

export type FormikErrors<Values> = {
  [K in keyof Values]?: Values[K] extends object
    ? FormikErrors<Values[K]>
    : string
};

setErrors(errors: FormikErrors<Values>): void;
setFieldError(field: keyof Values & string, message: string): void;
setFieldError(field: string, message: string): void;

Closing due to long inactivity

Was this page helpful?
0 / 5 - 0 ratings

Related issues

green-pickle picture green-pickle  路  3Comments

PeerHartmann picture PeerHartmann  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

najisawas picture najisawas  路  3Comments

najisawas picture najisawas  路  3Comments