async-validator icon indicating copy to clipboard operation
async-validator copied to clipboard

Typescript interface for RuleItem validator doesn't make sense.

Open Giwayume opened this issue 4 years ago • 0 comments

validator?: (
    rule: Rules,
    value: any,
    callback: (error: string | string[] | void) => void,
    source: ValidateSource,
    options: ValidateOption,
  ) => boolean | Error | Error[];

From my understanding, "callback" and the return value for a function validator are interchangeable. You either use one or the other, correct?

This interface forces Typescript users to use the return value to inform the status of the validation. So basically, callback is a useless argument for Typescript users. In order to take advantage of callback you must return an undefined value in the validator according to this code:

https://github.com/yiminghe/async-validator/blob/bc18bca1d9422911dc5ac3ce2a99599821534bd9/src/index.js#L236-L246

If I obey the interface typings and return a boolean, Error, or Error[], callback is automatically called in all of these cases. The interface should be updated to allow for a void return, or remove callback as an argument completely if you're leaning towards using the return value instead as a framework.

New interface that reflects the current functionality:

validator?: (
    rule: Rules,
    value: any,
    callback: (error: string | string[] | void) => void,
    source: ValidateSource,
    options: ValidateOption,
  ) => void | boolean | Error | Error[];

Giwayume avatar May 08 '21 16:05 Giwayume