Import individual functions from @neutrium/utilities/validation or the package root. Guard and assertion value arguments accept unknown; there is no string conversion, truncation, mutation, or numeric coercion. Guards return booleans and narrow types. Assertions return void on success, narrow types, and throw on failure. Their optional name argument labels errors without formatting the rejected value.
Use a guard when invalid input should take an ordinary branch. Use an assertion when invalid input should stop the operation with an exception. Neither converts strings into numbers; parse text explicitly before validating it.
import { isPositiveSafeInteger, assertPositiveSafeInteger }
from '@neutrium/utilities/validation';
function capacityOrDefault(value: unknown): number
{
return isPositiveSafeInteger(value) ? value : 128;
}
function requireCapacity(value: unknown): number
{
assertPositiveSafeInteger(value, 'capacity');
return value; // narrowed to number
}
capacityOrDefault('64'); // 128: numeric strings are not numbers
requireCapacity(64); // 64
| Guards | Assertions | Contract |
|---|---|---|
Native typeof checks |
assertString, assertBoolean, assertBigInt |
Primitive values only; boxed objects fail. |
Native Number.isFinite |
assertFiniteNumber |
Excludes NaN and infinities. |
Native Number.isInteger |
assertInteger |
Integer-valued number; may be outside safe integer limits. |
Native Number.isSafeInteger |
assertSafeInteger |
Integer within ±Number.MAX_SAFE_INTEGER. |
isNonNegativeSafeInteger |
assertNonNegativeSafeInteger |
Safe integer ≥ 0; accepts -0. |
isPositiveSafeInteger |
assertPositiveSafeInteger |
Safe integer > 0. |
isUint32 |
assertUint32 |
Integer from 0 through 2³² − 1; accepts -0. |
isInRange(value, min, max) |
assertInRange(value, min, max, name?) |
Finite inclusive range; fractions allowed. |
| Native null/undefined checks | assertDefined |
Excludes only null and undefined. |
isPlainRecord |
assertPlainRecord |
Local Object.prototype or null prototype; no field validation. |
Native choices.includes(value) |
assertOneOf(value, choices, name?) |
SameValueZero membership, retaining literal-union types. |
| — | assertKnownKeys(value, keys, name?) |
Plain record with only allowed own keys. |
Numeric and choice assertions throw RangeError, even when a numeric input has the wrong primitive type. Other assertions throw TypeError. This matches cache limit validation. Use compound guards or native checks when consumers need custom errors. Assertions remain useful because they throw named errors and narrow unknown inputs; built-in predicate functions do not provide that assertion contract.
Range bounds must be finite and ordered: invalid bounds make isInRange return false and assertInRange throw. Combine assertSafeInteger and assertInRange when an integer and a narrower range are both required.
import { assertKnownKeys, assertPositiveSafeInteger, assertOneOf } from '@neutrium/utilities/validation';
function configure(input: unknown)
{
assertKnownKeys(input, ['precision', 'mode'], 'configuration');
const precision = input.precision;
const mode = input.mode;
assertPositiveSafeInteger(precision, 'precision');
assertOneOf(mode, ['fast', 'precise'], 'mode');
return { precision, mode }; // number and 'fast' | 'precise'
}
Number.isFinite('12'); // false
Number.isFinite(Infinity); // false
assertKnownKeys checks all own keys, including non-enumerable and symbol keys. It does not require any allowed key to exist or inspect values. Use string keys for numeric property names ('0', not 0), matching JavaScript reflection. Getter properties are not invoked. Proxies may execute reflection traps; errors from enumerating their keys propagate.
isPlainRecord deliberately excludes class instances, arrays and objects with a foreign realm's Object.prototype. It is a shallow structural check, not a guarantee of accessor-free or immutable data. A failing prototype reflection trap returns false. Choices use object identity, match NaN with NaN, and treat -0 and +0 alike. Pass dense readonly tuples for choice lists; no deep comparison is performed.
isFloat32Array, isFloat64Array, isUint8Array and isUint32Array narrow unknown values to the corresponding typed-array type. They use the intrinsic typed-array brand, so they accept foreign-realm arrays, subclasses and shared-buffer views. Uint8Array subclasses such as Node buffers are accepted; Uint8ClampedArray and DataView are not.
Own Symbol.toStringTag properties cannot spoof the checks and their getters are not invoked. Proxies, including revoked proxies, return false. These guards check the type, not whether the backing buffer remains usable: detached views still match their brand.
copyDataRecord(value, name?) validates a plain record and shallow-copies all its own data properties into a fresh mutable null-prototype record. Nested values remain shared. Symbol and non-enumerable input keys are included; output properties are enumerable and writable. A literal __proto__ remains ordinary data.
Accessors are rejected with TypeError without running getters. The helper does not validate property values or allowed names. Apply assertKnownKeys to the copied record when a fixed key set is required. This is a shallow copy, not a deeply frozen snapshot. Use configuration helpers when you also need defaults and validated settings, or immutableSnapshot for a deeply frozen copy.
import { copyDataRecord, assertKnownKeys } from '@neutrium/utilities/validation';
const input: unknown = { precision: 20 };
const fields = copyDataRecord(input, 'options');
assertKnownKeys(fields, ['precision']);
// fields.precision is still unknown until its value is validated.
Proxy reflection traps may run: a failing prototype check produces TypeError, while errors from enumerating keys or reading property descriptors propagate. A reported key without a descriptor is rejected with TypeError.
The result has no inherited object methods. Use Object.hasOwn(fields, key) rather than fields.hasOwnProperty(key).