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

jsonschema-extra

v1.2.0

Published

Extends jsonschema validator with some common custom types and attributes

Downloads

9,341

Readme

jsonschema-extra

NPM version David deps node version npm download

Extends jsonschema validator with some common custom types and attributes

Usage

Installation

npm install jsonschema-extra --save

Example

var jsonschema = require('jsonschema');
var extra = require('jsonschema-extra');

var validator = new (jsonschema.Validator)();
extra(validator);

// regexp
validator.validate(/abc/, { type: 'regexp' });

// error
validator.validate(new Error(), { type: 'error' });

// mongodb objectid
validator.validate('123456789012345678901234', { type: 'objectId' });

Attributes

See jsonschema documentation for more detailed documentation on custom attributes.

validate

Custom validator when jsonschema just isn't what you are after. This one is my absolute favourite! It comes in handy whenever you are trying to do something complex.

One of the features of jsonschema is that you can share it across client and server which is exactly what I do! However, if your schema contains a validate attribute it will no longer be valid json. This is not a big issue if you are ok for client side code to not contain all your validation logic if schema is sent from the server.

Single validator

var schema = {
	validate: function(instance, schema, options) {
		// do your crazy validation here
			// return an error string if not valid
		return 'is not a valid instance';
	}
};

Multiple validators

var schema = {
	validate: [
		function(instance, schema, options) {
			// do your crazy validation here
				// return an error string if not valid
			return 'is not a valid instance';
		},
		function(instance, schema, options) {
			// do your crazy validation here
				// return an error string if not valid
			return 'is not a valid instance';
		}
	]
};

Types

Supported types:

  • error (instanceof Error)
  • regexp (instanceof RegExp)
  • function (Function - also works for Generator Function)
  • generatorFunction (ES6 Generator Function)
  • objectId (MongoDb objectId)
  • plainObject (calls _.isPlainObject)
validator.validate(new Error(), { type: 'error' });

validator.validate(/regexp/, { type: 'regexp' });

validator.validate(function() {}, { type: 'function' });

validator.validate(function *() {}, { type: 'generatorFunction' });

validator.validate('123456789012345678901234', { type: 'objectId' });
validator.validate(new ObjectID(), { type: 'objectId' });

validator.validate({ a: 'a', b: 'b' }, { type: 'plainObject' });

js-quantities types

Quantity types supported by js-quantities

  • qty.acceleration
  • qty.activity
  • qty.angle
  • qty.angular_velocity
  • qty.area
  • qty.capacitance
  • qty.charge
  • qty.conductance
  • qty.currency
  • qty.current
  • qty.energy
  • qty.force
  • qty.frequency
  • qty.illuminance
  • qty.inductance
  • qty.length
  • qty.luminous_power
  • qty.magnetism
  • qty.magnetism
  • qty.magnetism
  • qty.mass
  • qty.mass_concentration
  • qty.memory
  • qty.molar_concentration
  • qty.potential
  • qty.power
  • qty.pressure
  • qty.radiation
  • qty.resistance
  • qty.speed
  • qty.substance
  • qty.temperature
  • qty.time
  • qty.unitless
  • qty.viscosity
  • qty.viscosity
  • qty.volume
validator.validate('1 meter', { type: 'qty.length' });

validator.validate('45.3 seconds', { type: 'qty.time' });


// can also validate Qty instances
var Qty = require('js-quantities');
var qty = Qty.parse('2 meters');

validator.validate(qty, { type: 'qty.length' });

Testing

Install mocha globally

$ npm install mocha -g

Run tests

$ npm test

Changelog

v1.2.0 (22 Dec 2014)

v1.1.1 (19 Dec 2014)

  • Minor doc updates

v1.1.0 (18 Sep 2014)

  • Allow multiple validators in the validate property

v1.0.2 (30 Sep 2014)

  • Fixed so it works on non-Windows systems
  • Removed mongodb dev dependency

v1.0.1 (27 Sep 2014)

  • Updated docs
  • Updated package.json

v1.0.0 (27 Sep 2014)

  • Changed public API - jsonschema is no longer a dependency. This lets jsonschema to be updated in userland without this module requiring an update.
  • Name of module changed to jsonschema-extra
  • Removed conditionalEnum custom attribute since this has been fixed in the latest versions of jsonschema
  • Removed speed type