# @startupjs/sharedb-schema

> Racer plugin for schema validation module for ShareDB

Latest version **0.55.8** (published 2023-12-16) · 0 weekly downloads

## Install

```sh
npm install @startupjs/sharedb-schema
pnpm add @startupjs/sharedb-schema
yarn add @startupjs/sharedb-schema
bun add @startupjs/sharedb-schema
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.55.8 |
| Published | 2023-12-16 |
| First published | 2020-10-14 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | cray0000, yska, zag2art, fctsvirus, maestro.sc, byshock |
| Keywords | sharedb, validation, schema |

## Links

- npm: https://www.npmjs.com/package/@startupjs/sharedb-schema
- npm.io page: https://npm.io/package/@startupjs/sharedb-schema

## Dependencies (1)

- [z-schema](https://npm.io/package/z-schema.md) ^4.2.3

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.55.8 (latest) — 2023-12-16
- 0.57.0-canary.0 (canary) — 2024-05-25
- 0.56.0-alpha.0 (next) — 2024-01-15
- 0.55.0 — 2023-11-29
- 0.55.0-alpha.12 — 2023-11-17
- 0.55.0-alpha.1 — 2023-11-08
- 0.54.1 — 2023-11-01
- 0.54.0 — 2023-10-25
- 0.53.0 — 2023-08-17
- 0.52.1 — 2023-05-31
- 0.52.0 — 2023-05-26
- 0.51.0 — 2023-05-24
- 0.50.13 — 2023-02-27
- 0.50.12 — 2023-02-13
- 0.50.5 — 2022-12-27
- … 43 more at https://npm.io/package/@startupjs/sharedb-schema/versions

## README

# ShareDB-Schema

- Racer plugin for schema validation module for ShareDB
- Uses [z-schema](https://github.com/zaggino/z-schema), which supports [JSON-Schema](http://json-schema.org/) v4
- Schema validation executes in sync ``validate`` hook, so you validate the actual result of operation
- Supports custom validators with async and sync logic
- Custom validators can preload data in async hook to use it later in sync

## Known Issues
- Works only with JSON OT type
- Format functions should be sync
- For every op the whole doc validates each time, because it\`s the only case when z-schema returns full path to wrong fields

### Installation
```
yarn add @startupjs/sharedb-schema
```

### Setting

#### Step 1. Options
``` js
var options = {
  schemas: {
    users: {
      title: 'Example Schema',
      type: 'object',
      properties: {
        nickname: {
          type: 'string',
          format: 'xstring', // custom format
          minLength: 1,
          maxLength: 10,
        },
        email: {
          type: 'string',
          format: 'email',
        },
        age: {
          description: 'Age in years',
          type: 'integer',
          minimum: 0,
        },
        roleId: {
          type: 'string',
          collection: 'roles', // additional field for 'join' custom validator
          validators: ['join'], // custom validators
        },
        hobbies: {
          type: 'array',
          maxItems: 3,
          items: {
            type: 'string',
          },
          uniqueItems: true,
        },
      },
      required: ['email'],
    }
  },
  // JSON-Schema formats can be added here. They should be sync
  formats: {
    xstring: function(str) {
      return str !== 'xxx';
    }
  },
  // Custom validators
  validators: {
    // join - is working example of custom validator. It ensures that value is id of doc of specific collection
    join: {
      async: function (context, done) {
        var id = context.value // here is value for this op
        if (!id) return done()
        var collection = context.schema.collection // context.schema - is schema of current property
        var model = this.store.createModel()
        var $entity = model.at(collection + '.' + id)
        model.fetch($entity, function (err) {
          if (err) return done(err)
          if (!$entity.get()) {
            return done(Error('No ' + collection + ' with id ' + id))
          }
          done()
        })
      },
    },
    // this is example of custom validator, that preloads data and uses it later
    preload: {
      async: function (context, done) {
        var model = this.store.createModel() // that`s how to get model
        var $someData = model.at('some.path')
        model.fetch($someData, function (err) {
          if (err) return done(err)
          var data = $someData.get()
          done(null, data) // pass data as second parameter
        })
      },
      sync: function (value, context) {
        var data = context.data // preloaded data is here

        return true || false
      },
    },
  },
}

```

#### Step 2. Plugin

Add the `sharedbSchemaInit` function to your `server/index.js`

``` js
const sharedbSchemaInit = require('@startupjs/sharedb-schema')
```

and then call this function in the backend event
``` js
eventEmitter.on('backend', async backend => {
  sharedbSchemaInit(backend, options)
})
```

## The MIT License

Copyright (c) 2016 Pavel Zhukov

---
_Source: https://npm.io/package/@startupjs/sharedb-schema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
