qa-smart-validator
v1.0.1
Published
Reusable validation library for QA automation testing
Maintainers
Readme
qa-smart-validator
A small, framework-agnostic validation library for QA automation. Use it in Node.js test scripts, CI checks, or any JavaScript environment (Node.js ≥ 18). No Playwright or test-runner dependency—just validators and a safe wrapper so your automation never crashes on bad input.
Features
- Email validator – Standard format; supports gmail.com, outlook.com, yahoo.com, and custom domains (e.g. example.com). Rejects empty, null, and invalid formats.
- String validator – Ensures value is a string and enforces optional min/max length (after trim). Clear, controlled errors.
- Phone validator – Digits only; length 10–15. Rejects empty, null, non-digits, and out-of-range length.
- safeValidate(fn) – Wraps any sync validation in try/catch. Returns
{ valid: true, data }or{ valid: false, error }so execution never throws. - ValidationError – All validators throw this (subclass of
Error) withmessage,code, and optionalfieldfor stable assertions and reporting.
Requirements
- Node.js ≥ 18
- ES Modules (
"type": "module"or.mjs)
Installation
npm install qa-smart-validatorUsage
import {
validateEmail,
validateString,
validatePhone,
safeValidate,
ValidationError,
} from 'qa-smart-validator';Validator examples
validateEmail(email)
Supports gmail.com, outlook.com, yahoo.com, and custom domains (e.g. example.com). Rejects empty, null, and invalid formats.
validateEmail('[email protected]'); // { valid: true, message: 'Valid email' }
validateEmail('[email protected]'); // { valid: true, message: 'Valid email' }
validateEmail(''); // throws ValidationError: Email cannot be empty
validateEmail(null); // throws ValidationError: Email is required
validateEmail('not-an-email'); // throws ValidationError: Invalid email formatvalidateString(value, minLength?, maxLength?)
Ensures value is a string and enforces optional min/max length (after trim).
validateString('hello', 1, 10); // { valid: true, value: 'hello' }
validateString('short', 10); // throws ValidationError: String length 5 is less than minimum 10
validateString(123); // throws ValidationError: Value must be a string, got numbervalidatePhone(phone)
Digits only; length 10–15.
validatePhone('1234567890'); // { valid: true, value: '1234567890' }
validatePhone('+1234567890'); // throws ValidationError: Phone must contain only digits
validatePhone('123'); // throws ValidationError: Phone length 3 is less than minimum 10safeValidate example
Wraps execution in try/catch and returns a structured result. Never throws.
- Success:
{ valid: true, data }–datais the return value of the function. - Failure:
{ valid: false, error }–erroris the thrown error message.
import { validateEmail, safeValidate } from 'qa-smart-validator';
const result = safeValidate(() => validateEmail('[email protected]'));
console.log(result);
// { valid: true, data: { valid: true, message: 'Valid email' } }
const failed = safeValidate(() => validateEmail(''));
console.log(failed);
// { valid: false, error: 'Email cannot be empty' }Example file
An example.js is included in the package. After installing, you can run it from your app directory:
node example.jsAPI summary
| Export | Description |
|-------------------|-------------|
| validateEmail | Validates email; throws ValidationError on failure. |
| validateString | Validates string and optional length; throws ValidationError on failure. |
| validatePhone | Validates phone (digits, 10–15); throws ValidationError on failure. |
| safeValidate(fn)| Runs fn() in try/catch; returns { valid, data } or { valid, error }. |
| ValidationError | Error class with message, code, field. |
| EMAIL_REGEX | RegExp for email (advanced use). |
| DIGITS_ONLY | RegExp for digits (advanced use). |
| PHONE_MIN_LENGTH / PHONE_MAX_LENGTH | 10 and 15 (advanced use). |
Contributing
- Fork the repository.
- Create a branch (
git checkout -b feature/your-feature). - Run tests:
npm test. Run lint:npm run lint. Format:npm run format. - Commit your changes and push the branch.
- Open a Pull Request.
Publishing to npm
Before publishing:
- Dry run – See what will be packed and run lint/tests:
npm run prepublishOnly npm pack - Login (if needed):
npm login - Publish (scoped packages use
--access publicfor first publish):npm publish --access public
Update repository, bugs, homepage, and author in package.json to your own repo and identity before publishing.
License
MIT
