@varasto/validator-storage
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

@varasto/validator-storage

npm

Implementation of storage that performs validation on the inserted data, using Yup schemas.

Installation

$ npm install --save @varasto/validator-storage

Usage

Validator storage acts as an wrapper for another storage. Before data is allowed to be inserted into the wrapped storage, an validation is performed on it against Yup schema that is mapped to an namespace.

Inserting data to an namespace that is not mapped to a schema is not allowed and will result in UnrecognizedNamespaceError.

import * as Yup from 'yup';
import { createRemoteStorage } from '@varasto/remote-storage';
import { createValidatorStorage } from '@varasto/validator-storage';

const personSchema = Yup.object({
  name: Yup.string().required(),
  age: Yup.number().required().positive().integer()
});

const taskSchema = Yup.object({
  title: Yup.string().required(),
  completed: Yup.boolean().required()
});

const namespaces = {
  people: personSchema,
  tasks: taskSchema
};

const remoteStorage = createRemoteStorage({ host: 'https://example.com' });
const validatorStorage = createValidatorStorage(remoteStorage, namespaces);

// This insertion will succeed because the given data matches with task schema.
await validatorStorage.set('tasks', 'eat', { title: 'Eat', done: false });

// This insertion will fail because the given data does not match with person
// schema.
await validatorStorage.set('people', 'john', { name: 'John', age: -5 });

// This insertion will fail because the namespace is not recognized.
await validatorStorage.set('stuff', 'junk', { description: 'Random junk.' });

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.0.00latest

Version History

VersionDownloads (Last 7 Days)Published
3.0.00
2.0.00
1.0.00

Package Sidebar

Install

npm i @varasto/validator-storage

Weekly Downloads

0

Version

3.0.0

License

MIT

Unpacked Size

12.8 kB

Total Files

12

Last publish

Collaborators

  • rauli