@socketdb/plugin-validate
v8.2.0
Published
Schema validation plugin for SocketDB
Downloads
2
Maintainers
Readme
@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.