mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix changes to InputValidator to resolve accidental breaking changes
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
55a977554d
commit
ee11098d7a
@ -11,7 +11,24 @@ import { TypedRegEx } from "typed-regex";
|
|||||||
export class AsyncInputValidationError extends Error {
|
export class AsyncInputValidationError extends Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type InputValidator<IsAsync extends boolean> = {
|
export type InputValidationResult<IsAsync extends boolean> =
|
||||||
|
IsAsync extends true
|
||||||
|
? Promise<void>
|
||||||
|
: boolean;
|
||||||
|
|
||||||
|
export type InputValidation<IsAsync extends boolean, RequireProps extends boolean> = (
|
||||||
|
RequireProps extends true
|
||||||
|
? (value: string, props: InputProps) => InputValidationResult<IsAsync>
|
||||||
|
: (value: string, props?: InputProps) => InputValidationResult<IsAsync>
|
||||||
|
);
|
||||||
|
|
||||||
|
export type SyncValidationMessageBuilder<RequireProps extends boolean> = (
|
||||||
|
RequireProps extends true
|
||||||
|
? (value: string, props: InputProps) => ReactNode
|
||||||
|
: (value: string, props?: InputProps) => ReactNode
|
||||||
|
);
|
||||||
|
|
||||||
|
export type InputValidator<IsAsync extends boolean = boolean, RequireProps extends boolean = false> = {
|
||||||
/**
|
/**
|
||||||
* Filters itself based on the input props
|
* Filters itself based on the input props
|
||||||
*/
|
*/
|
||||||
@ -19,8 +36,8 @@ export type InputValidator<IsAsync extends boolean> = {
|
|||||||
} & (
|
} & (
|
||||||
IsAsync extends false
|
IsAsync extends false
|
||||||
? {
|
? {
|
||||||
validate: (value: string, props: InputProps) => boolean;
|
validate: InputValidation<false, RequireProps>;
|
||||||
message: ReactNode | ((value: string, props: InputProps) => ReactNode | string);
|
message: ReactNode | SyncValidationMessageBuilder<RequireProps>;
|
||||||
debounce?: undefined;
|
debounce?: undefined;
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
@ -29,13 +46,23 @@ export type InputValidator<IsAsync extends boolean> = {
|
|||||||
*
|
*
|
||||||
* This function MUST reject with an instance of {@link AsyncInputValidationError}
|
* This function MUST reject with an instance of {@link AsyncInputValidationError}
|
||||||
*/
|
*/
|
||||||
validate: (value: string, props: InputProps) => Promise<void>;
|
validate: InputValidation<true, RequireProps>;
|
||||||
message?: undefined;
|
message?: undefined;
|
||||||
debounce: number;
|
debounce: number;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
export function inputValidator<IsAsync extends boolean = false>(validator: InputValidator<IsAsync>): InputValidator<IsAsync> {
|
export function asyncInputValidator(validator: InputValidator<true, false>): InputValidator<true, false>;
|
||||||
|
export function asyncInputValidator<RequireProps extends boolean>(validator: InputValidator<true, RequireProps>): InputValidator<true, RequireProps>;
|
||||||
|
|
||||||
|
export function asyncInputValidator<RequireProps extends boolean>(validator: InputValidator<true, RequireProps>): InputValidator<true, RequireProps> {
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function inputValidator(validator: InputValidator<false, false>): InputValidator<false, false>;
|
||||||
|
export function inputValidator<RequireProps extends boolean>(validator: InputValidator<false, RequireProps>): InputValidator<false, RequireProps>;
|
||||||
|
|
||||||
|
export function inputValidator<RequireProps extends boolean>(validator: InputValidator<false, RequireProps>): InputValidator<false, RequireProps> {
|
||||||
return validator;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +78,7 @@ export const isEmail = inputValidator({
|
|||||||
validate: value => !!value.match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/),
|
validate: value => !!value.match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const isNumber = inputValidator({
|
export const isNumber = inputValidator<true>({
|
||||||
condition: ({ type }) => type === "number",
|
condition: ({ type }) => type === "number",
|
||||||
message(value, { min, max }) {
|
message(value, { min, max }) {
|
||||||
const minMax: string = [
|
const minMax: string = [
|
||||||
@ -100,7 +127,7 @@ export const isExtensionNameInstall = inputValidator({
|
|||||||
validate: value => isExtensionNameInstallRegex.isMatch(value),
|
validate: value => isExtensionNameInstallRegex.isMatch(value),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const isPath = inputValidator<true>({
|
export const isPath = asyncInputValidator({
|
||||||
debounce: 100,
|
debounce: 100,
|
||||||
condition: ({ type }) => type === "text",
|
condition: ({ type }) => type === "text",
|
||||||
validate: async value => {
|
validate: async value => {
|
||||||
@ -112,14 +139,14 @@ export const isPath = inputValidator<true>({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const minLength = inputValidator({
|
export const minLength = inputValidator<true>({
|
||||||
condition: ({ minLength }) => !!minLength,
|
condition: ({ minLength }) => !!minLength,
|
||||||
message: (value, { minLength }) => `Minimum length is ${minLength}`,
|
message: (value, { minLength }) => `Minimum length is ${minLength}`,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
validate: (value, { minLength }) => value.length >= minLength!,
|
validate: (value, { minLength }) => value.length >= minLength!,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const maxLength = inputValidator({
|
export const maxLength = inputValidator<true>({
|
||||||
condition: ({ maxLength }) => !!maxLength,
|
condition: ({ maxLength }) => !!maxLength,
|
||||||
message: (value, { maxLength }) => `Maximum length is ${maxLength}`,
|
message: (value, { maxLength }) => `Maximum length is ${maxLength}`,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user