Registry / devops / tv4-formats

tv4-formats

JSON →
library3.0.6jsnpmunverified

tv4-formats (v3.0.6) provides common JSON Schema string format constraints as tv4 validator callbacks. It is a plug-in for the tv4 JSON Schema validator. Supports formats: credit-card-number, date, date-time, duration, time-offset, email, guid, uri, url. Last updated in 2020, no active development. Alternative is ajv-formats for the AJV validator.

npm install tv4-formats
INSTALL
IMPORT
SIG · TV4-FORMATS
T
tv4-formats
devopsjavascriptv3.0.6
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const formats = require('tv4-formats');
import formats from 'tv4-formats';
This package does not ship ESM; only CommonJS require works.
default
import formats from 'tv4-formats';
const { addFormat } = require('tv4-formats');
Default export is a function that adds formats to tv4, not named exports.
tv4
const tv4 = require('tv4'); const formats = require('tv4-formats'); tv4.addFormat(formats);
const tv4 = require('tv4'); const { addFormat } = require('tv4-formats'); addFormat(tv4);
The correct API is tv4.addFormat(formats) or tv4.freshApi().addFormat(formats).

Shows how to install and use tv4-formats with tv4 to validate a date format string.

const tv4 = require('tv4'); const formats = require('tv4-formats'); const validator = tv4.freshApi(); validator.addFormat(formats); const schema = {type: 'string', format: 'date'}; console.log(validator.validate('2014-02-11', schema)); // true console.log(validator.validate('invalid-date', schema)); // false
Debug
Known issues
breakingtv4-formats versions 2.x and earlier had different export format; upgrade to v3 requires changing how formats are added.
fix
Use require('tv4-formats') default export directly (no .addFormat methods on the export).
affects: <3.0.0
gotchatv4-formats does not validate formats like 'hostname', 'ipv4', 'ipv6', 'regex', 'uuid'; they are not implemented.
fix
Use alternative library like ajv-formats if you need those formats.
affects: *
gotchaThe 'credit-card-number' format uses Luhn algorithm but may not detect all invalid numbers; not a replacement for payment processor validation.
fix
Do not rely solely on this library for credit card validation; use a dedicated service.
affects: *
deprecatedThis package is no longer actively maintained; no updates for several years. Consider migrating to AJV with ajv-formats.
fix
Replace tv4 and tv4-formats with AJV and ajv-formats for active maintenance and broader format support.
affects: *
Errors
Common errors & fixes
TypeError: formats.addFormat is not a function
In versions 2.x, the default export was a function that you called with tv4, but in v3 you pass the export directly to tv4.addFormat().
fix
Ensure you are using tv4-formats v3 and do: const formats = require('tv4-formats'); tv4.addFormat(formats);
Error: Cannot find module 'tv4-formats'
tv4-formats not installed or missing from node_modules.
fix
Run: npm install --save tv4 tv4-formats
ValidationError: Unknown format: "email"
tv4's internal formats do not include email; you need to add tv4-formats to tv4.
fix
Call tv4.addFormat(require('tv4-formats')); before validation.
Upgrade
Version history
3.0.6latest on npm
Audit
Dependencies
tv4requiredpeer dependency: tv4 is required to use tv4-formats as its format callbacks
Agent activity
16 hits · last 30 days
node
16
Resources
tv4-formats — npm install tv4-formats · libregistry