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

json-schema-valid-component

v0.1.2

Published

JSON Schema validator

Downloads

7

Readme

json-schema-valid

Please note this library is not ready for production use.

A modular javascript JSON Schema validator

Installation

component:

$ component install ericgj/json-schema-valid

npm:

$ npm install json-schema-valid-component

Examples

There are two basic modes: as a simple function, and as a Correlation binding.

In either case, the validate() function returns boolean, and an Emitter is used for error handling (optional).

Simple function:

  var Validator = require('json-schema-valid')
    , Emitter = require('emitter')

  // error handling via external emitter
  var emitter = new Emitter();
  emitter.on('error', function(e){
    console.error('Error: %o', e);
  })

  var validator = new Validator(emitter)

  // if schema has already been parsed
  var valid = validator.validate(schema,instance);
  
  // if raw schema object
  var valid = validator.validateRaw(rawSchema,instance);
  

Correlation binding:


  var core = require('json-schema-core')
    , Schema = core.Schema
    , plugin = require('json-schema-valid')
   
  // attach plugin to Schema 
  Schema.use(plugin);

  // error handling via 'default' emitter
  var emitter = plugin.emitter();
  emitter.on('error', function(e){
    console.error('Error: %o', e);
  })

  // once you have a correlation, you can call validate() on it
  var valid = correlation.validate();

  // subschema for given instance property
  // resolving valid combination conditions (allOf, anyOf, oneOf)
  var subschema = correlation.subschema('foo');

  // resolved URI-template links, including for valid combination conditions
  var links = correlation.links();

API

Validator.prototype.validate( schema:Schema, instance:Object, [desc:String], [callback:Function] )

Validate given instance against given schema. Takes optional description string (for error handling) and/or callback function. Callback receives array of valid schemas (i.e., the root-level schema plus any schemas valid through combination conditions). Callback is only run if validation succeeds (valid).

Validator.prototype.validateRaw( schema:Object, instance:Object, [desc:String], [callback:Function] )

Validate given instance against given raw schema (parsing schema first).

Validator.addType( key:String, validator:Function )

Add custom validation function. See type/*.js for examples of how to write validation functions.

Validator.addFormat( format:String, validator:Function|Regexp )

Add custom format validation function or regular expression to match. Note specifying a regular expression here is essentially like having named schema pattern properties.

Validator.emitter()

Returns a (singleton) Emitter instance, used by default when a validator is not initialized with an external emitter.

Correlation#validate( [desc:String], [callback:Function] )

Validate correlation instance against correlation schema.

Correlation#resolveLinks()

Validate, and return links merged from all valid schemas, if valid.

Intended to be used with the hyperschema plugin, to provide links(), rel(), etc. methods to the correlation, when combination conditions are specified. If the hyperschema plugin is not used, this method returns undefined.

See test/tests.js for usage examples.

Correlation#subschema( property:String )

Validate, and get the subschema describing the given instance property. If multiple subschemas are valid, the subschema is normalized as a single allOf condition.

Intended to be used as the basis for correlation.getPath() for co-traversing the schema and instance, when combination conditions are specified.

See test/tests.js for usage examples.

Running tests

  1. Run make to generate JSON Schema test suite files and build the component.

  2. Browse the file test/index.html. Tests are run via in-browser mocha.

A note on dereferencing

Dereferencing schemas is not implemented here. It is assumed that schemas with JSON references ($ref) will have already been dereferenced, making use of an http client library. See for example json-schema-agent, which provides both correlation (via HTTP headers) and schema dereferencing. The underlying Schema data structure does provide an interface for manipulating references, so it is also possible to roll your own dereferencing.

My view is that dereferencing necessarily involves external resources (the HTTP stack) and thus should be cleanly separated from validation.

TODO

  • add common format validators
  • make error data compatible with tv4 errors
  • consider emitting schema-level errors or 'error trees'
  • more complete walk-through of how to use with json-schema-hyper, json-schema-agent, etc.
  • bower and npm installation

License

MIT