livesey-validation
is a lightweight and simple JavaScript module designed for data validation. It provides essential utilities for validating common data types such as booleans, dates, numbers, and strings. Ideal for environments where data validation is crucial, such as form validation or data processing tasks.
To install the livesey-validation
module, use npm:
npm install livesey-validation
import { Booleans } from 'livesey-validation';
try {
console.log(Booleans.parseEnvValue('true').isBoolean().isTrue().getValue()); // true
} catch (error) {
console.error(error.message);
}
import { Dates } from 'livesey-validation';
try {
console.log(Dates.parseEnvValue('2024-08-30').isValidDate().isFormat('YYYY-MM-DD').getValue()); // '2024-08-30'
} catch (error) {
console.error(error.message);
}
import { Numbers } from 'livesey-validation';
try {
console.log(Numbers.parseEnvValue('42').isInt().isNonNegative().getValue()); // 42
} catch (error) {
console.error(error.message);
}
import { Strings } from 'livesey-validation';
try {
console.log(Strings.isEmail('valid.email@example.com').max(30).getValue()); // Pass: Valid email
} catch (error) {
console.error(error.message);
}
-
Booleans.parseEnvValue(value: string): Booleans
Parses a string value to a boolean. Throws an error if the value is not "true" or "false".
-
isBoolean(): Boolean
Validates that the internal value is a boolean.
-
isTrue(): Booleans
Validates that the internal value is true.
-
isFalse(): Booleans
Validates that the internal value is false.
-
getValue(): boolean
Returns the internal boolean value.
-
Dates.parseEnvValue(value: string, format: string = 'YYYY-MM-DD'): Dates
Parses a string value into a date and checks it against the specified format.
-
isValidDate(): Dates
Validates that the internal value is a valid date.
-
isFormat(format: string): Dates
Validates that the date matches the specified format.
-
isBefore(date: string): Dates
Validates that the internal date is before the specified date.
-
isAfter(date: string): Dates
Validates that the internal date is after the specified date.
-
isInRange(startDate: string, endDate: string): Dates
Validates that the internal date is within the specified date range.
-
getValue(): string
Returns the internal date string.
-
Numbers.parseEnvValue(value: string | number): Numbers
Parses a value to a number. Throws an error if the value cannot be parsed.
-
isInt(): Numbers
Validates that the internal value is an integer.
-
isBigInt(): Numbers
Validates that the internal value is a BigInt.
-
isFloat(): Numbers
Validates that the internal value is a floating-point number.
-
isPositive(): Numbers
Validates that the internal number is positive.
-
isNonNegative(): Numbers
Validates that the internal number is non-negative.
isNegative(): Numbers
Validates that the internal number is negative.
isNonPositive(): Numbers
Validates that the internal number is non-positive.
isNaN(): Numbers
Validates that the internal value is NaN.
isInfinity(): Numbers
Validates that the internal value is Infinity.
getValue(): number
Returns the internal number value.
Strings.isString(value: string): Strings
Validates that the value is a string.
min(length: number): Strings
Validates that the string's length is at least the specified length.
max(length: number): Strings
Validates that the string's length is no more than the specified length.
exactly(length: number): Strings
Validates that the string's length is exactly the specified length.
isEmail(value: string): Strings
Validates that the string is a valid email address.
isUrl(value: string): Strings
Validates that the string is a valid URL.
isIp(value: string): Strings
Validates that the string is a valid IP address.
isPhone(value: string): Strings
Validates that the string is a valid phone number.
isUUID(value: string): Strings
Validates that the string is a valid UUID.
isTime(value: string): Strings
Validates that the string is a valid time in the format HH.
getValue(): string
Returns the internal string value.
We welcome contributions! If you encounter any bugs or have suggestions for improvements, please open an issue on GitHub.
If you find a bug, please report it by opening an issue here.
Please adhere to our Code of Conduct when contributing.
This project is licensed under the MIT License - see the LICENSE file for details.
-
Heorhii Huziuk
- Lead Developer - huziukwork@gmail.comSpecial thanks to everyone who contributed to this project!❤️