Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@startupjs/sharedb-schema

Package Overview
Dependencies
Maintainers
7
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@startupjs/sharedb-schema - npm Package Compare versions

Comparing version 0.37.0 to 0.38.0

13

lib/Schema.js

@@ -41,13 +41,14 @@ const ZSchema = require('z-schema')

let rootSchema = this.schemas[collection]
if (rootSchema.factory) {
// we need to check the type of rootSchema because the non factory schema can have the 'factory' field
if (typeof rootSchema === 'function' && rootSchema.factory) {
// get model from factory like in @startupjs/orm: https://github.com/startupjs/startupjs/blob/master/packages/orm/lib/index.js#L77
const $doc = this.model._scope(`${collection}.${docId}`)
await $doc.subscribe()
// get model from factory like in @startupjs/orm: https://github.com/startupjs/startupjs/blob/master/packages/orm/lib/index.js#L77
const factoryModel = rootSchema($doc, this.model)
rootSchema = { type: 'object', properties: factoryModel.constructor.schema }
rootSchema = factoryModel.constructor.schema
$doc.unsubscribe()
}
if (!rootSchema) {
// throw error if current collenction have no schema
if (!rootSchema || !rootSchema.properties) {
// throw error if current collection have no schema
// error can be skiped if you add skipNonExisting flag to your options

@@ -54,0 +55,0 @@ if (this.options.skipNonExisting) return done()

{
"name": "@startupjs/sharedb-schema",
"version": "0.37.0",
"version": "0.38.0",
"description": "Racer plugin for schema validation module for ShareDB",

@@ -33,3 +33,3 @@ "main": "lib/index.js",

},
"gitHead": "d3b2d5d99672ebd0d7e9c77fcda280c6000099e4"
"gitHead": "c64c5d77146ccde980ab0327522af12f199a186a"
}

@@ -14,12 +14,2 @@ # ShareDB-Schema

## How it works
Current 0.7 version of ShareJS has 4 hooks which are executed while operation is applied.
- async ``submit`` hook - executes once before op is applied to data snapshot in db. This is convinient place to preload data and execute async validation logic. Be aware that you can not have result of operation here. It\`s ok for some ops (``model.set``, ``model.add``) because they ovewrites previous data (or add new), but for array mutators (``model.push``, ``model.pop``, etc), string mutators (``model.stringInsert``, ``model.stringRemove``) and increment (``model.increment``) it\`s impossible to predict what will be the result data and we can not use some kinds of validators here because of this, like max, min, etc. That\`s why we should use sync hook.
- sync ``preValidate`` hook - executes before op actually applied to snapshot. In high concurent cases it can be executed more than one time. We still does not have result data here, so it\`s useless for us.
- sync ``validate`` hook - executes after op is applied to snapshot, but before these changes are saved to db. Can be executed more than one time. It\`s best place for schema validation logic as we have result data here which is not saved to db yet
- async ``after submit`` hook - executes once after changes are saved to db. It\`s useless for validation, but good to trigger
some stuff
All schema validation logic executes in ``validate`` hook. Custom validators have two methods: ``.sync`` - sync and executes in ``validate`` hook and ``.async`` - async and executes in ``submit`` hook. There can be one of them or both, also you can preload data in ``.async`` method to use it later in ``.sync``
### Installation

@@ -31,2 +21,3 @@ ```

### Setting
#### Step 1. Options

@@ -38,34 +29,34 @@ ``` js

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: 'object',
properties: {
nickname: {
type: 'string',
format: 'xstring', // custom format
minLength: 1,
maxLength: 10,
},
uniqueItems: true,
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'],
required: ['email'],
}

@@ -122,3 +113,4 @@ },

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

@@ -125,0 +117,0 @@ const sharedbSchemaInit = require('@startupjs/sharedb-schema')

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc