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

gun-schema

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gun-schema

Validate GUN DB data with JSON-Schema

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

gun-schema

Validate Gun data with JSON-Schema.

Validation is done by is-my-json-valid

Getting started

Browser

Download the js bundle from the releases page and add it to the page. As well as attaching the required methods to Gun, it will create a GunSchema global.

Server

Install from NPM

npm install --save gun-schema

Require the module to have it attach itself to GUN

var Gun = require("gun");
require("gun-schema");

Example

// Get a reference to your schema from wherever
// This one was taken from the is-my-json-valid page
var schema = {
    required: true,
    type: 'object',
    properties: {
        hello: {
            required: true,
            type: 'string'
        }
    }
};

var gun = Gun();

gun.schema("example", schema);

// This is valid so it'll get put into the graph
gun.save("example", {
    hello: "World!"
});

// This will throw an error because `hello` was set as `required`
gun.save("example", {
    goodbye: "World!"
});

API

After the plugin has been properly initialized, it adds the following methods on gun instances

gun.schema(name, schema, options)

Adds a new type that will be recognized by gun.save()

  • name : The unique name for this type of node
  • schema : The JSON Schema definition to use for validating this type
  • options : Optional argument which gets passed down to is-my-json-valid

gun.schemas(map, options)

Adds a bunch of schemas in one go.

  • map : A map of name-schema pairs that get passed on to gun.schema()
  • options : Optional argument which gets passed down to gun.schema() to configure is-my-json-valid

gun.save(name, value)

Similar to gun.put(), but uses the schema associated with name to ensure that value is valid.

  • name : The name of the schema that value should match
  • value : The value that should be validated before being put() into the DB.

If name does not point to a schema name that has been registered, then an error will be thrown. If value doesn't validate against the schema, then an error will be thrown with a errors property which contains the list of things that are wrong with value.

GunSchema(gun)

This is what gets exported by the module in CommonJS and what is added as the GunSchema global in the bundle. It takes a gun instance and adds schema functionality to it.

  • gun The gun instance to attach to. Not, this isn't the Gun constructor, but an actual instance or Gun.chain.

Building

You can build the browser bundle yourself by cloning the repo and executing:

`npm install`
`npm install gun`
`npm run bundle`

You will then have a file called bundle.js which you can embed in a webpage. The building is facilitated by Browserify

Keywords

FAQs

Package last updated on 03 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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