Last active
November 9, 2020 16:01
-
-
Save nomyfan/de16631a24c40b05f0cf1400f55eacf2 to your computer and use it in GitHub Desktop.
spawn validator for ElForm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function spawn<T>( | |
isValid: (value: T) => boolean, | |
msg: string, | |
options?: any, | |
) { | |
return { | |
required: true, | |
trigger: "blur", | |
...options, | |
validator: (_: any, val: T, callback: Function) => { | |
isValid(val) ? callback() : callback(new Error(msg)); | |
}, | |
}; | |
} | |
export function or<T>( | |
chain: ((_: T) => boolean)[], | |
msg: string, | |
options?: any, | |
) { | |
return spawn((value: T) => chain.some((c) => c(value)), msg, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment