ValidationDBProviderclass
new ValidationDBProvider<I, T>()
| Return | |
|---|---|
ValidationDBProvider<I, T> | Database provider that validates data flowing to and from an asynchronous source provider. |
Database provider that validates data flowing to and from an asynchronous source provider.
- Wraps a
sourceprovider (which may have any type, because validation guarantees the type) and runs every value through the relevantCollectionschema before writing and after reading. - Written data is validated against the collection's data schema; read data is validated against the item schema, so trusted, correctly-typed values reach the rest of the app.
- Validation failures here are program-state errors, so they throw a typed
ValueErrorrather than a raw validationstring.
A wrapping provider that validates data against the collection schema. ValidationDBProvider validates data on the way in (writes) and on the way out (reads) — so bad data never reaches the backend, and corrupt data from the backend surfaces as a ValueError rather than propagating silently.
Place it between the cache and the backend so cached data is always known-good.
Usage
import { ValidationDBProvider, MemoryDBProvider } from "shelving/db";
const provider = new ValidationDBProvider(new MemoryDBProvider());
// Validated against the POSTS schema before writing.
await provider.addItem(POSTS, { title: "Hello", body: "…", published: false });
// If the backend ever returns data that fails the schema, this throws ValueError.
const post = await provider.getItem(POSTS, "abc");Examples
const provider = new ValidationDBProvider(new FirestoreProvider());
await provider.setItem(users, 123, { name: "Dave", age: 40 }); // Validates before writing.