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

Why change validate typings?

Open chenzhq opened this issue 3 years ago • 4 comments

https://github.com/yiminghe/async-validator/blob/master/src/index.d.ts#L67

This is new typings 3.5.2

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

This is the old one 3.5.1

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

The two returned value is different and this caused an error in my ts project.

chenzhq avatar May 20 '21 09:05 chenzhq

I don't think this change should be included in a patch version.

chenzhq avatar May 20 '21 09:05 chenzhq

Or at least the return type should be boolean | Error | Error[] | void, otherwise callback argument is meaningless: if validator returns boolean | Error | Error[] the cb() is always called instantly by the library itself.

In the eleme docs the validator is used for asynchronous validation with setTimeout() so I have to wonder what is the actual difference between validator and asyncValidator? If validator is supposed to be sync, why does it have a callback parameter?

klesun avatar Feb 07 '23 13:02 klesun

For projects that use the callback parameter the downgrade to 3.5.1 seems to be the only option to make types work. A shame, considering that using this API by the callback parameter is the only way described by eleme.

klesun avatar Feb 07 '23 13:02 klesun

Upd.: oh, the typing was updated in more recent versions (v4.2.5 at most) to:

validator?: (
    rule: InternalRuleItem, 
    value: Value, 
    callback: (error?: string | Error) => void, 
    source: Values,
    options: ValidateOption
) => SyncValidateResult | void;

So upgrading also fixes the typing problems for callback users.

Guess this ticket can be closed?

klesun avatar Feb 07 '23 13:02 klesun