A lightweight, dependency-minimal runtime type validator for JavaScript and Node.js.
npm install arc-validate_*readme generated by AI_
A lightweight, dependency-minimal runtime type validator for JavaScript and Node.js.
It integrates with arc-is for general type detection and email-validator for email verification, while adding explicit handling for common validation cases like integers, floats, UUIDs, and explicit literal matching.
---
- Validates primitive and structured types using arc-is
- Custom checks for:
- parseInt / parseFloat numeric validation
- email using email-validator
- uuid (RFC 4122 v1โv5 pattern)
- explicit matching for literal values
- null inclusion when allowed in _types
- Consistent TypeError messages with optional customError override
- Lightweight and dependency-minimal
---
``bash`
npm install arc-validate
---
`js
import validateTypes from 'arc-validate';
// โ
Strings
validateTypes('hello', ['string']); // ok
// โ
Numbers
validateTypes(123, ['number']); // ok
// โ
Integer Parsing
validateTypes('42', ['parseInt']); // ok
validateTypes('abc', ['parseInt']); // throws TypeError
// โ
Float Parsing
validateTypes('3.14', ['parseFloat']); // ok
validateTypes('NaN', ['parseFloat']); // throws TypeError
// โ
Emails
validateTypes('user@example.com', ['email']); // ok
validateTypes('invalid', ['email']); // throws TypeError
// โ
UUIDs
validateTypes('123e4567-e89b-42d3-a456-426614174000', ['uuid']); // ok
validateTypes('invalid-uuid', ['uuid']); // throws TypeError
// โ
Explicit literals
validateTypes('on', ['explicit', 'on', 'off']); // ok
validateTypes('maybe', ['explicit', 'on', 'off']); // throws TypeError
// โ
Allow nulls explicitly
validateTypes(null, ['string', null]); // ok
// โ
Custom Error
const customErr = new RangeError('Custom validation failed');
validateTypes('bad', ['email'], customErr); // throws customErr
`
---
| Type Token | Behavior |
|-------------|-----------|
| 'parseInt' | Checks that parseInt(_unknown) returns a valid number. |'parseFloat'
| | Checks that parseFloat(_unknown) returns a valid number. |'email'
| | Validates with email-validator. |'uuid'
| | Validates against RFC 4122 v1โv5 UUID regex. |'explicit'
| | Removes "explicit" from _types and requires _unknown to equal one of the remaining values. |null
| | If _types includes null and _unknown === null, validation passes. |arc-is
| Default | Uses โs is(_unknown) result and compares against _types. |
---
`js
import validateTypes from 'arc-validate';
function processUser(input) {
validateTypes(input.email, ['email']);
validateTypes(input.id, ['uuid']);
validateTypes(input.role, ['explicit', 'admin', 'user', 'guest']);
console.log('All fields validated!');
}
processUser({
email: 'user@example.com',
id: '123e4567-e89b-42d3-a456-426614174000',
role: 'admin'
});
`
---
All validation failures throw a TypeError (or a custom error if provided).
`js`
try {
validateTypes('not-an-email', ['email']);
} catch (err) {
console.error(err.message); // "Expected email. Received: not-an-email"
console.error(err instanceof TypeError); // true
}
---
Comprehensive Jest tests are included. Run:
`bash``
npm test
---
This project is released under The Unlicense, placing it in the public domain.