UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

158 lines (157 loc) 6.73 kB
import { ValueError } from "../error/ValueError.js"; import { PARTIAL } from "../schema/DataSchema.js"; import { getNamedMessage } from "../util/error.js"; import { validateData } from "../util/validate.js"; import { AsyncThroughProvider, ThroughProvider } from "./ThroughProvider.js"; /** Validate a synchronous source provider (source can have any type because validation guarantees the type). */ export class ValidationProvider extends ThroughProvider { identifier; schemas; constructor(id, schemas, source) { super(source); this.identifier = id; this.schemas = schemas; } /** Get a named schema. */ getSchema(collection) { return this.schemas[collection]; } getItem(collection, id) { return _validateItem(collection, super.getItem(collection, id), this.identifier, this.getSchema(collection), this.getItem); } async *getItemSequence(collection, id) { const schema = this.getSchema(collection); for await (const item of super.getItemSequence(collection, id)) yield _validateItem(collection, item, this.identifier, schema, this.getItemSequence); } addItem(collection, data) { return super.addItem(collection, validateData(data, this.getSchema(collection).props)); } setItem(collection, id, data) { super.setItem(collection, id, validateData(data, this.getSchema(collection).props)); } updateItem(collection, id, updates) { super.updateItem(collection, id, _validateUpdates(collection, updates, this.getSchema(collection), this.updateItem)); } deleteItem(collection, id) { super.deleteItem(collection, id); } countQuery(collection, query) { return super.countQuery(collection, query); } getQuery(collection, query) { return _validateItems(collection, super.getQuery(collection, query), this.identifier, this.getSchema(collection), this.getQuery); } setQuery(collection, query, data) { super.setQuery(collection, query, this.getSchema(collection).validate(data)); } updateQuery(collection, query, updates) { super.updateQuery(collection, query, _validateUpdates(collection, updates, this.getSchema(collection), this.updateQuery)); } deleteQuery(collection, query) { super.deleteQuery(collection, query); } async *getQuerySequence(collection, query) { const schema = this.getSchema(collection); for await (const items of super.getQuerySequence(collection, query)) yield _validateItems(collection, items, this.identifier, schema, this.getQuerySequence); } } /** Validate an asynchronous source provider (source can have any type because validation guarantees the type). */ export class AsyncValidationProvider extends AsyncThroughProvider { identifier; schemas; constructor(id, schemas, source) { super(source); this.identifier = id; this.schemas = schemas; } /** Get a named data schema for this database. */ getSchema(collection) { return this.schemas[collection]; } async getItem(collection, id) { return _validateItem(collection, await super.getItem(collection, id), this.identifier, this.getSchema(collection), this.getItem); } async *getItemSequence(collection, id) { const schema = this.getSchema(collection); for await (const item of super.getItemSequence(collection, id)) yield _validateItem(collection, item, this.identifier, schema, this.getItemSequence); } addItem(collection, data) { return super.addItem(collection, validateData(data, this.getSchema(collection).props)); } setItem(collection, id, data) { return super.setItem(collection, id, validateData(data, this.getSchema(collection).props)); } updateItem(collection, id, updates) { return super.updateItem(collection, id, _validateUpdates(collection, updates, this.getSchema(collection), this.updateItem)); } deleteItem(collection, id) { return super.deleteItem(collection, id); } countQuery(collection, query) { return super.countQuery(collection, query); } async getQuery(collection, query) { return _validateItems(collection, await super.getQuery(collection, query), this.identifier, this.getSchema(collection), this.getQuery); } async *getQuerySequence(collection, query) { const schema = this.getSchema(collection); for await (const items of super.getQuerySequence(collection, query)) yield _validateItems(collection, items, this.identifier, schema, this.getQuerySequence); } setQuery(collection, query, data) { return super.setQuery(collection, query, validateData(data, this.getSchema(collection).props)); } updateQuery(collection, query, updates) { return super.updateQuery(collection, query, _validateUpdates(collection, updates, this.getSchema(collection), this.updateQuery)); } deleteQuery(collection, query) { return super.deleteQuery(collection, query); } } function _validateItem(collection, item, identifier, schema, caller) { if (!item) return undefined; try { return validateData(item, { id: identifier, ...schema.props }); } catch (thrown) { if (typeof thrown !== "string") throw thrown; throw new ValueError(`Invalid data for "${collection}"\n${thrown}`, { item, caller }); } } /** * Validate a set of entities for this query reference. * @throws `ValueError` if one or more items did not validate (conflict because the program is not in an expected state). */ function _validateItems(collection, items, identifier, schema, caller) { return Array.from(_yieldValidItems(collection, items, { id: identifier, ...schema.props }, caller)); } function* _yieldValidItems(collection, items, validators, caller) { const messages = []; for (const item of items) { try { yield validateData(item, validators); } catch (thrown) { if (typeof thrown !== "string") throw thrown; messages.push(getNamedMessage(item.id, thrown)); } } if (messages.length) throw new ValueError(`Invalid data for "${collection}"\n${messages.join("\n")}`, { items, caller }); } function _validateUpdates(collection, updates, schema, caller) { try { return validateData(updates, PARTIAL(schema).props); } catch (thrown) { if (typeof thrown !== "string") throw thrown; throw new ValueError(`Invalid updates for "${collection}"\n${thrown}`, { updates, caller }); } }