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

ssb-poll-schema

v1.7.1

Published

Schema for ssb poll types, current and legacy

Downloads

48

Readme

Build Status

ssb-poll-schema

Gives parsers and validators for all known schema versions of ssb-poll messages

Motivation

As well as this being a useful module for scuttle polls, it's a spec for how to publish and version schema on ssb.

This module gives you parsers that will return a 'normalised' object, regardless of of the shape of the object to parse. The position and poll objects returned by the parsers are defined in ./schema.

A change in major version of this module means that there is a breaking change to the shape of the 'normalised' object. An example of a breaking change would be that a required property in the schema was removed.

A minor version change could be that a new property was added to a schema but that isn't a required field.

Example

var { isPoll } = require('ssb-poll-schema')

var validPoll = {
  type: 'poll',
  version: 'v1',
  title: 'what is for dinner',
  body: 'this is really important, please let me know',
  closesAt: '2018-03-15T03:40:06.222Z',
  details: {
    type: 'chooseOne',
    choices: ['lasagne', 'avos', 'tacos']
  }
}

console.log(isPoll(validPoll)) // => true

API

Validators

  • isPoll (NOTE this currently returns true / undefined - a side effect of depject)

  • isChooseOnePoll (also accessible under isPoll.chooseOne)

  • isPosition (NOTE this currently returns true / undefined - a side effect of depject)

  • isChooseOnePosition (also accessible under isPosition.chooseOne)

Parsers

  • parsePoll,
  • parsePosition,

Parser Errors (useful for debugging)

  • getPollErrors,
  • getPositionErrors,

Returns something shaped like:


{ v1:
   [ { field: 'data.title',
       message: 'is required',
       value: [Object],
       type: 'object',
       schemaPath: [] },
     { field: 'data.details',
       message: 'is required',
       value: [Object],
       type: 'object',
       schemaPath: [] 
     } 
   ] 
}

Version strings

Returns an object with version string constants useful for publishing messages.

{
  V1_SCHEMA_VERSION_STRING: 'v1'
}

Important note for mantainers of this module or if you add your own schema:

How / when should you modify schema and version numbers:

You have an existing schema but it's missing a constraint that must be added. You want existing messages to fail validation if they fail the new constraint.

  • Modify the schema by adding the constraint.
  • Don't change the schema version number.
  • Bump the patch version of this module.

You have an existing schema but it has a constraint that must be removed. You want existing messages that would have failed validation to be passed by this new schema.

  • Make a whole new schema + parsers with unique version number. e.g. copy the v1 folder to a new v2 folder and wire it up

You have an existing schema and want to add a new property that is optional (not required in the schema).

  • Modify the schema by adding the property.
  • Update the parser.
  • Don't change the schema version number.
  • Bump the patch version of this module.

Want to use your schema with this project?

There are two ways you can use your schema with this module.

Make your schema official

To contribute your schema:

  • Publish your schema as a node module
    • It must be a depject module. Check out ./v1/index.js for an example.
  • Add your module as a dependency in this module.
  • Modify index.js so that your module is passed to combine:
var sockets = combine([
  require('./v1/'),
  require('./v2/'),
  require('<your-module>') 
])

Combine these schema with some of your own in another module

This module exports it's depject combinable schema as schema so you could do something like this in your own module:

var pollSchema = require('ssb-poll-schema')
var combine = require('depject')
var {first} = require('depject/apply')

var yourSchema = require('./your-schema')

var sockets = combine(pollSchema.concat(yourSchema)) 

var isPoll = first(sockets.poll.isPoll, 'poll.isPoll')

console.log(isPoll({})) // => undefined