Ratgeber · JSON Schema 2020-12
JSON-Schema-format: die wichtigsten Built-in-Formate
format ist optional im Standard. Ajv-formats-Package nötig. date-time nach RFC 3339, email mit Vorsicht (kein vollständiger RFC 5322).
Was format macht (und was nicht)
format prüft Strings auf semantische Korrektheit. Beispiel: ein String der als "email" markiert ist, soll auch das Format einer E-Mail-Adresse haben. WICHTIG: format ist im Standard optional. Ajv 8 bringt die Formate nicht mit: Ohne das Plugin ajv-formats wirft compile() im Strict Mode (Standard) einen Fehler wie unknown format "email" ignored in schema. Nur mit strict: false läuft das Schema durch, und das Format wird dann tatsächlich nicht geprüft.
format aktivieren
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
const ajv = new Ajv();
addFormats(ajv); // Ohne diesen Aufruf wirft compile() bei format: 'email'
const validate = ajv.compile({ type: 'string', format: 'email' });
console.log(validate('max@example.com')); // true
console.log(validate('kein-email')); // false
Die wichtigsten Built-in-Formate
| Format | Was geprüft wird | Beispiel-Pattern |
|---|---|---|
| date-time | RFC 3339 Date-Time | "2026-05-14T12:30:00Z" |
| date | RFC 3339 Date | "2026-05-14" |
| time | RFC 3339 Time (mit Zeitzone) | "12:30:00Z" |
| Vereinfachte Prüfung | "max@example.com" | |
| uri | RFC 3986 URI | "https://example.com/path" |
| uri-reference | URI oder relative Reference | "/path/sub" |
| uuid | RFC 4122 UUID | "550e8400-e29b-41d4-a716-446655440000" |
| ipv4 | IPv4-Adresse | "192.168.1.1" |
| ipv6 | IPv6-Adresse | "2001:db8::1" |
| hostname | RFC 1123 Hostname | "server.example.com" |
| regex | ECMA-262-Regex | "^[a-z]+$" |
Warum email tricky ist
Eine vollständig RFC-5322-konforme E-Mail-Regex ist extrem lang und erlaubt skurrile Adressen wie "max@[127.0.0.1]" oder "a@b". ajv-formats nutzt einen vereinfachten Check, der die meisten echten E-Mails akzeptiert und solche Sonderfälle ablehnt (beide Beispiele oben werden abgelehnt).
Für strenge Anwendungsfälle: format: email + zusätzlich eigene Validation. Oder besser: pragmatisch den Mail-Server entscheiden lassen (Bounce-Handling).
date-time im Detail
RFC 3339 ist ein Subset von ISO 8601:
- Muss vollständig sein: Datum + Uhrzeit + Zeitzone
- Zeitzone als "Z" für UTC oder "+02:00"
- Sekundenbruchteile optional: "2026-05-14T12:30:00.123Z"
- Das Datum muss existieren: "2026-02-30T00:00:00Z" wird im Standardmodus abgelehnt
NICHT akzeptiert: "2026-05-14T12:30:00" (keine Timezone), "14.05.2026" (deutsches Format). Großzügig ist ajv-formats beim Trennzeichen: Auch ein Leerzeichen statt T ("2026-05-14 12:30:00Z") wird akzeptiert, weil RFC 3339 das ausdrücklich erlaubt.
Eigene Formate definieren
import Ajv from 'ajv';
const ajv = new Ajv();
ajv.addFormat('iban', /^[A-Z]{2}[0-9]{2}[A-Z0-9]{4,30}$/);
const schema = {
type: 'object',
properties: {
iban: { type: 'string', format: 'iban' }
}
};
const validate = ajv.compile(schema);
console.log(validate({ iban: 'DE89370400440532013000' })); // true
console.log(validate({ iban: 'kein-iban' })); // false
So lassen sich domänenspezifische Constraints (IBAN, USt-IdNr, ISBN) als Format einbauen - sauberer als jedes Mal pattern manuell zu schreiben. Die Regex im Beispiel prüft nur den Aufbau, keine IBAN-Prüfziffer. Dafür gibt addFormat auch eine Objektform mit eigener validate-Funktion.
Wann format, wann pattern?
| Situation | Empfehlung |
|---|---|
| Standard-Format (email, date-time, ...) | format |
| Domain-spezifisch ohne Standard (IBAN, ISBN, User-ID-Format) | pattern oder eigenes format |
| Free-Form-Text mit grobem Constraint | pattern |
| Cross-Language-Schema | format (Standard-Compliance) |
format-Validation-Modi
ajv-formats kennt zwei Modi:
- "full" (Standard): strenger, prüft zum Beispiel, ob ein Datum im Kalender existiert
- "fast": schlankere Regexe, akzeptiert mehr Edge-Cases ("2026-02-30" gilt dann als gültiges date)
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
const ajv = new Ajv();
addFormats(ajv, { mode: 'fast', formats: ['email', 'date'] }); // nur diese beiden Formate