@nightlightmare/isvalid
v1.1.2
Published
Pack of validation functions for JavaScript/TypeScript
Readme
isValid Package
The isValid package provides two utility functions for validating common types of data: emails and URLs.
Functions
isEmail
isEmail(email: string, options?: { blacklist?: string[], whitelist?: string[], maxLength?: number }): boolean
Validates whether the given string is a valid email with additional options.
Parameters:
email: The string to validate (type:string).options: An optional object that allows you to customize the validation:blacklist(optional): A list of blacklisted domains to reject.whitelist(optional): A list of whitelisted domains to allow.maxLength(optional): Maximum allowed length for the email.
Returns:
trueif the string is a valid email.falseotherwise.
Example:
import { isEmail } from "@nightlightmare/isvalid";
const result = isEmail("[email protected]", {
blacklist: ["example.com"],
whitelist: ["example.org"],
maxLength: 100,
});
console.log(result); // Output: falseisUrl
isUrl(url: string, options?: { whitelist?: UrlValidationRules, blacklist?: UrlValidationRules }): boolean
Validates whether the given string is a valid URL with additional options.
Parameters:
url(string): The string to validate as a URL.options(optional object): Contains additional validation rules for the URL:whitelist(optional): Specifies the allowed parts of the URL (protocol, domain, port, path, query, fragment).blacklist(optional): Specifies the restricted parts of the URL (protocol, domain, port, path, query, fragment).
Returns:
- Returns
trueif theurlis a valid URL and matches the rules provided inwhitelistand doesn't match any rules inblacklist. - Returns
falseif theurlis invalid or doesn't meet the conditions specified in thewhitelistorblacklist.
Example:
import { isUrl } from "@nightlightmare/isvalid";
const options = {
whitelist: {
protocol: ["https"],
domain: ["example.com"],
path: ["/home"],
},
blacklist: {
query: ["id=456"],
},
};
const isValid = isUrl("https://example.com/home?id=123", options);
console.log(isValid); // Output: trueInstallation
To install the package, use your preferred package manager:
Using npm:
npm install @nightlightmare/isvalidUsing yarn:
yarn add @nightlightmare/isvalidUsing pnpm:
pnpm add @nightlightmare/isvalidLicense
This project is licensed under the Apache License Version 2.0.
