Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
The tv4 npm package is a JSON Schema validator. It allows you to validate JSON data against a JSON Schema to ensure that the data structure conforms to a specified format. This is particularly useful for data validation, configuration management, and ensuring data integrity across systems.
Basic Validation
This feature allows you to validate a simple piece of data against a schema. In this example, a string is validated against a schema that requires a string type, resulting in a successful validation.
{"var tv4 = require('tv4');\nvar schema = {\"type\": \"string\"};\nvar result = tv4.validateResult('I am a string', schema);\nconsole.log(result.valid); // true"}
Complex Schema Validation
This feature demonstrates validating a more complex JSON object against a detailed schema. The schema specifies that the object must have a 'name' property of type string and an 'age' property of type integer with a minimum value of 0. The validation passes as the data meets these criteria.
{"var tv4 = require('tv4');\nvar schema = {\"type\": \"object\", \"properties\": {\"name\": {\"type\": \"string\"}, \"age\": {\"type\": \"integer\", \"minimum\": 0}}};\nvar data = {\"name\": \"John Doe\", \"age\": 30};\nvar result = tv4.validateResult(data, schema);\nconsole.log(result.valid); // true"}
Validation with Error Reporting
This feature showcases how tv4 can report errors when validation fails. In this example, attempting to validate a string against a schema that expects a number results in a validation error, which is then logged.
{"var tv4 = require('tv4');\nvar schema = {\"type\": \"number\"};\nvar result = tv4.validateResult('not a number', schema);\nif (!result.valid) {\n console.error(result.error);\n}"}
Ajv is another popular JSON Schema validator that is known for its performance and support for the latest JSON Schema standards. Compared to tv4, Ajv offers better performance and more comprehensive support for JSON Schema Draft 6 and Draft 7 features.
Joi is a powerful schema description language and data validator for JavaScript. Unlike tv4, which focuses on JSON Schema, Joi provides a fluent API for describing data schemas and includes built-in types and validation rules, making it more intuitive for some developers.
The jsonschema package is another validator for JSON Schema. It aims to provide a simple and straightforward implementation of JSON Schema validation. While similar in functionality to tv4, jsonschema may have differences in API design and error reporting.
#Tiny Validator (for v4 JSON Schema)
All you need is tv4.js
(24KB) or tv4.min.js
(12.9KB, 3.8KB gzipped).
There is support for $ref
with JSON Pointer fragment paths (other-schema.json#/properties/myKey
).
var valid = tv4.validate(data, schema);
If validation returns false
, then an explanation of why validation failed can be found in tv4.error
.
The error object will look something like:
{
"code": 0,
"message": "Invalid type: string",
"dataPath": "/intKey",
"schemaKey": "/properties/intKey/type"
}
The "code"
property will refer to one of the values in tv4.errorCodes
- in this case, tv4.errorCodes.INVALID_TYPE
.
To enable external schema to be referenced, you use:
tv4.addSchema(url, schema);
If schemas are referenced ($ref
) but not known, then validation will return true
and the missing schema(s) will be listed in tv4.missing
.
Storing the error and missing schemas does not work well in multi-threaded environments, so there is an alternative syntax:
var result = tv4.validateResult(data, schema);
The result will look something like:
{
"valid": false,
"error": {...},
"missing": [...]
}
Normally, tv4
stops when it encounters the first validation error. However, you can collect an array of validation errors using
var result = tv4.validateMultiple(data, schema);
The result will look something like:
{
"valid": false,
"errors": [
{...},
...
],
"missing": [...]
}
Support for asynchronous validation (where missing schemas are fetched) can be added by including an extra JavaScript file. Currently, the only version requires jQuery (tv4.async-jquery.js
), but the code is very short and should be fairly easy to modify for other libraries (such as MooTools).
Usage:
tv4.validate(data, schema, function (isValid, validationError) { ... });
validationFailure
is simply taken from tv4.error
.
There are tests available, but they require PHP, so you can't see them on GitHub.
tv4.min.js
is produced using the Google Closure Compiler.
The code is available as "public domain", meaning that it is completely free to use, without any restrictions at all. Read the full license here.
It's also available under an (MIT license](http://jsonary.com/LICENSE.txt).
FAQs
A public domain JSON Schema validator for JavaScript
The npm package tv4 receives a total of 1,975,757 weekly downloads. As such, tv4 popularity was classified as popular.
We found that tv4 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.