@socketdb/plugin-validate
TypeScript icon, indicating that this package has built-in type declarations

8.2.0 • Public • Published

@socketdb/plugin-validate

Table of Contents

About

Adds schema validation to SocketDB to only allow saving data that matches the schema.

It uses ajv under the hood.

Installing

Using pnpm:

pnpm add @socketdb/plugin-validate

or npm:

npm i @socketdb/plugin-validate

Usage

Create a schema and add plugin to the SocketDB server.

import { SocketDBServer } from 'socketdb';
import validate, { JSONSchemaType } from '@socketdb/plugin-validate';

type Data = {
	[key: string]: {
		player: {
			[id: string]: {
				name: string;
			};
		};
	};
};
const schema: JSONSchemaType<Data> = {
	type: 'object',
	required: [],
	additionalProperties: {
		type: 'object',
		required: ['player'],
		properties: {
			player: {
				type: 'object',
				required: [],
				additionalProperties: {
					type: 'object',
					required: ['name'],
					properties: {
						name: {
							type: 'string',
						},
					},
				},
			},
		},
	},
};

SocketDBServer({
	plugins: [validate(schema)],
});

See: https://ajv.js.org/ for more information on how to create schemas.

Package Sidebar

Install

npm i @socketdb/plugin-validate

Homepage

socketdb.com

Weekly Downloads

1

Version

8.2.0

License

MIT

Unpacked Size

534 kB

Total Files

7

Last publish

Collaborators

  • timobechtel