-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lib)!: refactored validator stuff
- Loading branch information
Showing
5 changed files
with
98 additions
and
91 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './utils'; | ||
export * from './validators'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// #region Validator | ||
export type Validator = { | ||
name: string; | ||
validate: (value: unknown) => boolean; | ||
}; | ||
// #endregion Validator | ||
|
||
// #region AsyncValidator | ||
export type AsyncValidator = { | ||
name: string; | ||
validate: (value: unknown) => Promise<boolean>; | ||
}; | ||
// #endregion AsyncValidator | ||
|
||
export function defineValidator<TReturn extends boolean | Promise<boolean>>( | ||
name: string, | ||
validate: (value: unknown) => TReturn | ||
): TReturn extends boolean ? Validator : AsyncValidator { | ||
return { | ||
name, | ||
validate, | ||
} as any; | ||
} | ||
|
||
export function defineValidatorWithArgs<Targs, TReturn extends boolean | Promise<boolean>>( | ||
name: string, | ||
validate: (value: unknown, args: Targs) => TReturn | ||
): TReturn extends boolean ? (args: Targs) => Validator : (args: Targs) => AsyncValidator { | ||
return (args: Targs) => { | ||
return { | ||
name, | ||
validate: (value: unknown) => validate(value, args), | ||
} as any; | ||
}; | ||
} | ||
|
||
export function isString(value: unknown): value is string { | ||
return typeof value === 'string'; | ||
} | ||
|
||
export function isNumber(value: unknown): value is number { | ||
return typeof value === 'number'; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { defineValidator, defineValidatorWithArgs, isNumber, isString } from './utils'; | ||
|
||
export const required = defineValidator('required', (value) => !!value); | ||
|
||
export const maxLength = defineValidatorWithArgs( | ||
'maxLength', | ||
(value, maxLength: number) => isString(value) && value?.length <= maxLength | ||
); | ||
|
||
export const minLength = defineValidatorWithArgs( | ||
'minLength', | ||
(value, minLength: number) => isString(value) && value?.length >= minLength | ||
); | ||
|
||
export const min = defineValidatorWithArgs( | ||
'min', | ||
(value, min: number) => isNumber(value) && value >= min | ||
); | ||
|
||
export const max = defineValidatorWithArgs( | ||
'max', | ||
(value, max: number) => isNumber(value) && value <= max | ||
); | ||
|
||
export const startsWith = defineValidatorWithArgs( | ||
'startsWith', | ||
(value, startsWith: string) => | ||
isString(value) && value.toLocaleLowerCase().startsWith(startsWith.toLocaleLowerCase()) | ||
); | ||
|
||
export const regex = defineValidatorWithArgs( | ||
'regex', | ||
(value, regex: RegExp) => isString(value) && regex.test(value) | ||
); | ||
|
||
// https://github.com/colinhacks/zod | ||
const emailRegex = | ||
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i; | ||
|
||
export const email = defineValidator('email', (value) => regex(emailRegex).validate(value)); | ||
|
||
// https://github.com/colinhacks/zod | ||
const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u; | ||
|
||
export const emoji = defineValidator('emoji', (value) => regex(emojiRegex).validate(value)); |