Skip to content

Instantly share code, notes, and snippets.

@nomyfan
Last active November 9, 2020 16:01
Show Gist options
  • Save nomyfan/de16631a24c40b05f0cf1400f55eacf2 to your computer and use it in GitHub Desktop.
Save nomyfan/de16631a24c40b05f0cf1400f55eacf2 to your computer and use it in GitHub Desktop.
spawn validator for ElForm
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