Skip to main content

Schema validation

RxDB has multiple validation implementations that can be used to ensure that your document data is always matching the provided JSON schema of your RxCollection.

The schema validation is not a plugin but comes in as a wrapper around any other RxStorage and it will then validate all data that is written into that storage. This is required for multiple reasons:

  • It allows us to run the validation inside of a Worker RxStorage instead of running it in the main JavaScript process.
  • It allows us to configure which RxDatabase instance must use the validation and which does not. In production it often makes sense to validate user data, but you might not need the validation for data that is only replicated from the backend.
warning

Schema validation can be CPU expensive and increases your build size. You should always use a schema validation in development mode. For most use cases, you should not use a validation in production for better performance.

When no validation is used, any document data can be saved but there might be undefined behavior when saving data that does not comply to the schema of a RxCollection.

RxDB has different implementations to validate data, each of them is based on a different JSON Schema library. In this example we use the LocalStorage RxStorage, but you can wrap the validation around any other RxStorage.

validate-ajv​

A validation-module that does the schema-validation. This one is using ajv as validator which is a bit faster. Better compliant to the jsonschema-standard but also has a bigger build-size.

import { wrappedValidateAjvStorage } from 'rxdb/plugins/validate-ajv';
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage';

// wrap the validation around the main RxStorage
const storage = wrappedValidateAjvStorage({
storage: getRxStorageLocalstorage()
});

const db = await createRxDatabase({
name: randomCouchString(10),
storage
});

validate-z-schema​

Both is-my-json-valid and validate-ajv use eval() to perform validation which might not be wanted when 'unsafe-eval' is not allowed in Content Security Policies. This one is using z-schema as validator which doesn't use eval.

import { wrappedValidateZSchemaStorage } from 'rxdb/plugins/validate-z-schema';
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage';

// wrap the validation around the main RxStorage
const storage = wrappedValidateZSchemaStorage({
storage: getRxStorageLocalstorage()
});

const db = await createRxDatabase({
name: randomCouchString(10),
storage
});

validate-is-my-json-valid​

WARNING: The is-my-json-valid validation is no longer supported until this bug is fixed.

The validate-is-my-json-valid plugin uses is-my-json-valid for schema validation.

import { wrappedValidateIsMyJsonValidStorage } from 'rxdb/plugins/validate-is-my-json-valid';
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage';

// wrap the validation around the main RxStorage
const storage = wrappedValidateIsMyJsonValidStorage({
storage: getRxStorageLocalstorage()
});

const db = await createRxDatabase({
name: randomCouchString(10),
storage
});

Custom Formats​

The schema validators provide methods to add custom formats like a email format. You have to add these formats before you create your database.

Ajv Custom Format​

import { getAjv } from 'rxdb/plugins/validate-ajv';
const ajv = getAjv();
ajv.addFormat('email', {
type: 'string',
validate: v => v.includes('@') // ensure email fields contain the @ symbol
});

Z-Schema Custom Format​

import { ZSchemaClass } from 'rxdb/plugins/validate-z-schema';
ZSchemaClass.registerFormat('email', function (v: string) {
return v.includes('@'); // ensure email fields contain the @ symbol
});

Performance comparison of the validators​

The RxDB team ran performance benchmarks using two storage options on an Ubuntu 24.04 machine with Chrome version 131.0.6778.85. The testing machine has 32 core 13th Gen Intel(R) Core(TM) i9-13900HX CPU.

IndexedDB Storage (based on the IndexedDB API in the browser):

IndexedDB StorageTime to First insertInsert 3000 documents
no validator68 ms213 ms
ajv67 ms216 ms
z-schema71 ms230 ms

Memory Storage: stores everything in memory for extremely fast reads and writes, with no persistence by default. Often used with the RxDB memory-mapped plugin that processes data in memory an later persists to disc in background:

Memory StorageTime to First insertInsert 3000 documents
no validator1.15 ms0.8 ms
ajv3.05 ms2.7 ms
z-schema0.9 ms18 ms

Including a validator library also increases your JavaScript bundle size. Here's how it breaks down (minified + gzip):

Build Size (minified+gzip)Build Size (IndexedDB)Build Size (memory)
no validator73103 B39976 B
ajv106135 B72773 B
z-schema125186 B91882 B

RxDB: The Fastest and Most Reliable Local Database

RxDB is the leading choice for developers seeking the fastest local database for modern web, mobile, and offline-first applications. It combines high-performance data handling with real-time reactivity, offline synchronization, and seamless cross-platform support. Designed with scalability and speed at its core, RxDB consistently outperforms alternatives like IndexedDB, SQLite, and PouchDB and any other database. It is the easiest to set up and learn. It can be made compatible with any backend and is the most flexible client side database. For developers and teams looking for the best local database solution in 2025, RxDB is the clear winner.

✕