Validate Hexadecimal Event IDs and Find Duplicates
JavaScript
Read a JSON array of hexadecimal event ID strings from a UTF-8 file or standard input, validate each value, normalize valid IDs to lowercase without an optional leading `0x`, and report duplicate positions. Do not convert IDs to numbers or claim chain validity.
This read-only Node.js 22 command-line recipe checks an input array of event IDs. It accepts hexadecimal strings with or without a `0x` prefix, normalizes valid strings for case-insensitive duplicate detection, and reports one-based positions. Validation here means only that a value is a nonempty hexadecimal string; it does not establish any blockchain-specific format or validity.
Before you start
Intended runner: Node.js 22.
No package installation is required; the recipe uses CommonJS and Node.js built-ins.
Input must be UTF-8 JSON whose top-level value is an array.
Build and check
Save the supplied code in a `.cjs` file.
Prepare a JSON array containing event ID strings and select a file path or `-` for stdin.
Run the Node.js command and inspect the JSON report and exit status.
Use the reported positions to correct or review invalid and repeated values.
Limits to understand
The rule accepts one or more hexadecimal digits after an optional case-insensitive `0x` prefix; it does not impose a fixed byte length.
An empty string and a prefix without following hex digits are invalid.
Duplicate comparison uses lowercase text with the optional prefix removed; it performs no numeric conversion.
This validates only textual hexadecimal syntax, not event existence, chain membership, or any chain-specific ID rules.
The program reads the entire input into memory and is intended for bounded JSON inputs.
Input values are treated only as data; the program does not execute them or modify files.
What this example adds
Provides a configurable file-or-stdin interface, explicit structural and per-item validation, canonicalized duplicate detection, and position-oriented JSON output. Invalid input is reported without printing potentially unexpected input values; malformed IDs are identified by position and value type.
Run it
Save the code as `validate-hex-event-ids.cjs`, then run `node validate-hex-event-ids.cjs ids.json`. Use `node validate-hex-event-ids.cjs - < ids.json` to read standard input. The program prints a JSON report; it exits 0 when all entries are valid, 2 when one or more entries are invalid, and 1 for usage, file, or JSON parse errors.
Code license: MIT. Keep the license notice with your copy.
Example version 0.1.0 · Documentation reviewed 2026-10-12.
Executed 6 synthetic local fixture cases in isolated Node.js 22.23.3 on GitHub Actions, without network access. Declared exit codes, output assertions and input integrity checks passed. Live services, installed third-party packages and production behavior remain outside scope. Editorial approval is still required.