tv4 is an abandoned JavaScript library designed for validating data against JSON Schema Draft v4 exclusively. Its latest release, version 1.3.0, was published in August 2015, with no subsequent updates or active maintenance. The library differentiates itself by offering synchronous, single-error validation by default, with optional methods for collecting multiple errors (`validateMultiple`) or a structured result object (`validateResult`) to better support multi-threaded environments where global state (`tv4.error`, `tv4.missing`) is problematic. It also supports `$ref` for referencing external schemas and has an optional mechanism for handling cyclical JavaScript objects. Due to its strict adherence to Draft v4 and lack of updates, it does not support newer JSON Schema drafts (like Draft 6, 7, 2019-09, or 2020-12) and is not recommended for new projects requiring modern schema features or active support.
npm install tv4Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic synchronous JSON Schema Draft v4 validation, including how to handle validation results and errors, disallow additional properties, and use the `validateResult` method for safer error handling in shared environments.
For new projects or projects requiring modern JSON Schema features, consider using actively maintained alternatives like Ajv (Another JSON Schema Validator) which supports all current drafts and offers better performance and extensibility.
Always use `tv4.validateResult(data, schema)` or `tv4.validateMultiple(data, schema)` for safer, self-contained result objects that include `valid`, `error` (or `errors`), and `missing` properties, ensuring thread-safety and consistent error reporting.
To collect all validation errors, use `tv4.validateMultiple(data, schema)`. This method returns an object containing an `errors` array with all detected issues.
When validating objects that might contain circular references, pass `true` as the third argument to any validation method (e.g., `tv4.validate(data, schema, true)`). This enables recursive checking, preventing infinite loops.
To treat unknown properties as validation errors, set the `banUnknownProperties` flag to `true` on the `tv4` object (e.g., `tv4.banUnknownProperties = true;`). Alternatively, set `"additionalProperties": false` in your schema.
If asynchronous schema fetching is required, include `tv4.async-jquery.js`. For environments without jQuery, the README suggests the code is simple enough to adapt, but no official alternatives are provided. Consider pre-loading all schemas via `tv4.addSchema` if possible to avoid this dependency.
Pass `true` as the third argument to the validation method: `tv4.validate(data, schema, true);` or `tv4.validateResult(data, schema, true);`.
Switch to `tv4.validateResult(data, schema)` which returns a self-contained result object, preventing global state conflicts.
Ensure all referenced schemas are pre-loaded using `tv4.addSchema(url, schema)` before validation. Alternatively, if asynchronous fetching is set up, ensure it's properly configured and awaited.
Set `tv4.banUnknownProperties = true;` globally or add `"additionalProperties": false` to your schema to disallow undeclared properties.
No dependency data recorded yet.