npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

o-gun-schema

v0.1.2

Published

Validate GUN DB data with JSON-Schema

Downloads

5

Readme

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 o-gun-schema

Require the module to have it attach itself to GUN

var Gun = require("gun");
require("o-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.add("example", schema);

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

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

API

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

gun.schema.add(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.schema.put(name, value)

Similar to gun.put(), but uses the schema associated with name to ensure that value is valid. This is different syntax from the original gun-schema.

  • 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.

gun.schema.set(name, value)

Similar to gun.set(), but uses the schema associated with name to ensure that value is valid. The above applies.

gun.schema.addMany(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.schema.getSchemas()

gets all your active schemas

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.