diff --git a/src/renderer/components/input/input.tsx b/src/renderer/components/input/input.tsx index ef4b419909..f45c8377a0 100644 --- a/src/renderer/components/input/input.tsx +++ b/src/renderer/components/input/input.tsx @@ -20,7 +20,6 @@ const { conditionalValidators, asyncInputValidator, inputValidator, - inputValidatorWithRequiredProps, isAsyncValidator, unionInputValidatorsAsync, ...InputValidators @@ -30,7 +29,6 @@ export { InputValidators, asyncInputValidator, inputValidator, - inputValidatorWithRequiredProps, isAsyncValidator, unionInputValidatorsAsync, }; diff --git a/src/renderer/components/input/input_validators.ts b/src/renderer/components/input/input_validators.ts index be32f800a1..59e12a977a 100644 --- a/src/renderer/components/input/input_validators.ts +++ b/src/renderer/components/input/input_validators.ts @@ -14,54 +14,42 @@ export type InputValidationResult = ? Promise : boolean; -export type InputValidation = ( - RequireProps extends true - ? (value: string, props: InputProps) => InputValidationResult - : (value: string, props?: InputProps) => InputValidationResult -); +export type InputValidation = (value: string, props?: InputProps) => InputValidationResult; -export type SyncValidationMessage = React.ReactNode | ( - RequireProps extends true - ? (value: string, props: InputProps) => React.ReactNode - : (value: string, props?: InputProps) => React.ReactNode -); +export type SyncValidationMessage = React.ReactNode | ((value: string, props?: InputProps) => React.ReactNode); -export type InputValidator = { +export type InputValidator = { /** * Filters itself based on the input props */ condition?: (props: InputProps) => any; } & ( - IsAsync extends false + IsAsync extends true ? { - validate: InputValidation; - message: SyncValidationMessage; - debounce?: undefined; - } - : { /** * The validation message maybe either specified from the `message` field (higher priority) * or if that is not provided then the message will retrived from the rejected with value */ - validate: InputValidation; - message?: SyncValidationMessage; + validate: InputValidation; + message?: SyncValidationMessage; debounce: number; } + : { + validate: InputValidation; + message: SyncValidationMessage; + debounce?: undefined; + } ); -export function isAsyncValidator(validator: InputValidator): validator is InputValidator { +export function isAsyncValidator(validator: InputValidator): validator is InputValidator { return typeof validator.debounce === "number"; } -export function asyncInputValidator(validator: InputValidator): InputValidator { +export function asyncInputValidator(validator: InputValidator): InputValidator { return validator; } -export function inputValidator(validator: InputValidator): InputValidator { - return validator; -} - -export function inputValidatorWithRequiredProps(validator: InputValidator): InputValidator { +export function inputValidator(validator: InputValidator): InputValidator { return validator; } @@ -70,9 +58,9 @@ export function inputValidatorWithRequiredProps(validator: InputValidator, "condition" | "message">, - ...validators: InputValidator[] -): InputValidator { + baseValidator: Pick, "condition" | "message">, + ...validators: InputValidator[] +): InputValidator { return inputValidator({ ...baseValidator, validate: (value, props) => validators.some(validator => validator.validate(value, props)), @@ -84,9 +72,9 @@ export function unionInputValidators( * valid if one of the input validators matches the input */ export function unionInputValidatorsAsync( - baseValidator: SetRequired, "condition" | "message">, "message">, - ...validators: InputValidator[] -): InputValidator { + baseValidator: SetRequired, "condition" | "message">, "message">, + ...validators: InputValidator[] +): InputValidator { const longestDebounce = Math.max( ...validators .filter(isAsyncValidator) @@ -117,7 +105,7 @@ export function unionInputValidatorsAsync( * If no validator returns `true` then mark as invalid by throwing. The message will be * obtained from the `message` field. */ - throw {}; + throw new Error(); }, ...baseValidator, }); @@ -135,9 +123,9 @@ 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,}))$/), }); -export const isNumber = inputValidatorWithRequiredProps({ +export const isNumber = inputValidator({ condition: ({ type }) => type === "number", - message(value, { min, max }) { + message(value, { min, max } = {}) { const minMax: string = [ typeof min === "number" ? `min: ${min}` : undefined, typeof max === "number" ? `max: ${max}` : undefined, @@ -145,7 +133,7 @@ export const isNumber = inputValidatorWithRequiredProps({ return `Invalid number${minMax ? ` (${minMax})` : ""}`; }, - validate: (value, { min, max }) => { + validate: (value, { min, max } = {}) => { const numVal = +value; return !( @@ -194,16 +182,16 @@ export const isPath = asyncInputValidator({ }, }); -export const minLength = inputValidatorWithRequiredProps({ +export const minLength = inputValidator({ condition: ({ minLength }) => !!minLength, - message: (value, { minLength }) => `Minimum length is ${minLength}`, - validate: (value, { minLength = 0 }) => value.length >= minLength, + message: (value, { minLength = 0 } = {}) => `Minimum length is ${minLength}`, + validate: (value, { minLength = 0 } = {}) => value.length >= minLength, }); -export const maxLength = inputValidatorWithRequiredProps({ +export const maxLength = inputValidator({ condition: ({ maxLength }) => !!maxLength, - message: (value, { maxLength }) => `Maximum length is ${maxLength}`, - validate: (value, { maxLength = 0 }) => value.length <= maxLength, + message: (value, { maxLength = 0 } = {}) => `Maximum length is ${maxLength}`, + validate: (value, { maxLength = 0 } = {}) => value.length <= maxLength, }); const systemNameMatcher = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/;