@jarenjs/formats
v0.9.2
Published
Jaren Formats for JSON Schema Validation
Downloads
180
Readme
@jarenjs/formats
Format validators for the JSON Schema format keyword, built on the text validators of @jarenjs/core. Includes all standard string formats (date-time, date, time, duration, email, idn-email, hostname, idn-hostname, ipv4, ipv6, uri, uri-reference, uri-template, iri, iri-reference, uuid, regex) plus many extras (isbn10, mac, base64, alpha, color, ...) and numeric formats (int8 ... uint64, float16 ... float64).
The JSON addressing formats are grouped separately in jsonFormats: json-pointer, json-pointer-uri-fragment and relative-json-pointer (RFC 6901), and json-path, which validates query strings against the complete RFC 9535 grammar using the parser of the JSONPath compiler in @jarenjs/json.
The name → predicate bindings live in one canonical table, exported as formatTesters (plus the per-group stringFormatTesters, jsonFormatTesters, dateTimeFormatTesters, numberFormatTesters): bare synchronous predicates without validator coupling. The format compilers above wrap these testers in the validator contract, and @jarenjs/forms merges its rendering hints over the same table for per-keystroke field validation — one registry, so the two layers can never drift apart.
Usage
import { JarenValidator } from '@jarenjs/validate';
import * as formats from '@jarenjs/formats';
const jaren = new JarenValidator()
.addFormats(formats.stringFormats)
.addFormats(formats.numberFormats)
.addFormats(formats.dateTimeFormats)
.addFormats(formats.jsonFormats);
const validate = jaren.compile({ type: 'string', format: 'json-path' });
validate('$.store.book[[email protected] < 10]'); // trueFormat assertion follows the specification per draft: asserted through draft 2019-09, annotation-only from draft 2020-12 on unless enabled via the formatAssertion option (new JarenValidator({ formatAssertion: true })) or a metaschema that declares the format-assertion vocabulary.
✍ The complete format list
✍ Formats for strings
These format validators are based on the json-schema.org website. They are grouped in stringFormats (with the date/time formats also available separately as dateTimeFormats).
🗨 Formats for datetime
date-time| according to RFC3339, time-zone is mandatorydate| according to RFC3339, time-zone is mandatorytime| according to RFC3339, time-zone is mandatoryduration| duration from RFC3339iso-date-time| ISO 8601 date-time with optional timezoneiso-time| ISO 8601 time with optional timezone
Note: All date time formats can use formatMinimum / formatMaximum and formatExclusiveMinimum and formatExclusiveMaximum
🗨 Formats for url's, hostnames and emails
url| full URLurl--full| same asurl, but more comprehensiveuri| full URIuri--full| same asuri, but more comprehensiveuri-reference| URI reference, including full and relative URIsuri-reference--full| same asuri-reference, but more comprehensiveuri-template| URI template according to RFC6570iri| full URI with international charactersiri-reference| full URI reference with with international charactersemail| email addressemail--full| same as email, but more comprehensivehostname| host name according to RFC1034idn-hostname| host name with international charactersidn-email| email address with international characters
🗨 Formats for identifiers
uuid| Universally Unique IDentifier according to RFC4122guid| Globally Unique IDentifier according to Microsoftidentifier| C-type identifierhtml-identifier| html elementidattribute identifier according to RFC7992css-identifier| css class name identifier according to RFC7993mac| ethernet interface identifier (EUI-48) according to IEEE820ipv4| IP v4 address according to RFC791ipv6| IP v6 address according to RFC2460
🗨 Formats for json pointers and paths
These are grouped in jsonFormats.
json-pointer| JSON-pointer according to RFC6901json-pointer-uri-fragment| JSON-pointer fragment according to RFC6901relative-json-pointer| relative JSON-pointer according to draft-luff-relative-json-pointer-00json-path| JSONPath query according to RFC9535, checked against the complete grammar (including filter well-typedness) by the parser of the JSONPath compiler in@jarenjs/json
🗨 Miscellaneous formats
alpha| allow only ASCII alpha characters (a-zA-Z)numeric| allow only numeric characters (0-9)alphanumeric| allow only ASCII alpha numeric charactershexadecimal| allow only hexadecimal characters (0-9a-fA-F)uppercase| allow only upper case alpha characterslowercase| allow only lower case alpha characterscolor| web color hex string (starts with #, must be 3 or 6 hax characters)regex| tests whether a string is a valid regular expressionbase64| base64 encoded databyte| same asbase64formatisbn10| International Standard Book Number 10 digit numberisbn13| International Standard Book Number 13 digit numbercountry2| Country code by alpha-2 according to ISO3166-1 !No tests exists!iban| International Bank Account Number !No tests exists!
✍ Formats for numbers
These are grouped in numberFormats. Formats for numbers validate both numbers and strings as number types; combine them with the type keyword (e.g. { "type": "integer", "format": "int32" }) when only real number types should be allowed.
🗨 Formats integer numbers
int8| signed 8 bit integeruint8| unsigned 8 bit integerint16| signed 16 bit integeruint16| unsigned 16 bit integerint32| signed 32 bit integeruint32| unsigned 32 integerint64| signed 64 integeruint64| unsigned 64 integer
🗨 Formats floating point numbers
float16| 16 bit floating point numberfloat32| 32 bit floating point numberfloat64| 64 bit floating point numberfloat| 32 bit floating point numberdouble| 64 bit floating point number
Development
Unit tests live in test/formats/ at the repository root; test/formats/testers.test.js enforces that every compiler registry's key set equals its tester group's, so the validator layer and the bare-predicate layer can never drift. The predicates themselves are implemented and tested in @jarenjs/core. See the repository README for the monorepo picture and the ROADMAP for planned formats.
