logaretm / vee-validate

✅ Painless Vue forms
https://vee-validate.logaretm.com/v4
MIT License
10.79k stars 1.26k forks source link

[v4] FormErrors should allow pass value type or has any | undefined value type. #3361

Closed NikitaIT closed 5 days ago

NikitaIT commented 3 years ago

Yup exemple from docs using-a-custom-locale-dictionary:

setLocale({
  // use constant translation keys for messages without values
  mixed: {
    default: 'field_invalid',
  },
  // use functions to generate an error object that includes the value from the schema
  number: {
    min: ({ min }): YupValue => ({ key: 'field_too_short', values: { min } }),
    max: ({ max }): YupValue => ({ key: 'field_too_big', values: { max } }),
  },
});

Expected Type:

// my-port-yop, this can be typed much better
type YupValue = { key: TValues[keyof TValues], values: any };

// vee-validate
declare type FormErrors<TValues extends Record<string, unknown>> = Partial<Record<
  keyof TValues, 
  YupValue
>>;

Current Type:

// vee-validate
declare type FormErrors<TValues extends Record<string, unknown>> = Partial<Record<
  keyof TValues, 
  string | undefined
>>;

Exemple:

    useForm({
      validationSchema: markRaw(
      io.object({
        password: io.string().required().min(2),
      })
    ),
    }).errors.password // string | undefined, but real type is { key: 'field_too_short', values: { min } }

Why i use object as result?

logaretm commented 3 years ago

Interesting use case, I think it's a good addition and would try to add it for the next patch. Thanks for bringing this up.