getLastError

The getLastError() method returns the most recent validation error message generated by the library.

This is especially useful when silent mode is enabled, and errors aren't thrown — you can still inspect the last failed validation and take appropriate action (e.g., logging, UI feedback, or debugging).

What It Returns

A string with the most recent validation error message, including:

  • The type of validation (check, checkRange, checkOptions, etc.)

  • A short description of the issue

  • The expected vs. received types

  • A UTC timestamp of when the error occurred

Example Output

Check failed: (check) Type mismatch. Expected "BigInt", got "Object". - 2025-06-02T08:00:59.239Z

Usage Example

import Validate from '@manoelfernandes/validate';

const result = Validate.check({}, "bigint");
console.log(result); // false

const error = Validate.getLastError();
console.log(error);
// Check failed: (check) Type mismatch. Expected "BigInt", got "Object". - 2025-06-02T08:00:59.239Z"

Saving to File (Node.js)

import fs from 'fs';

const error = Validate.check("foo", "number");

if (!error) {
  fs.appendFileSync('validation-errors.log', Validate.getLastError() + '\n');
}

This is useful for maintaining audit logs or debugging in production environments.

Last updated