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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mc-schema

v0.0.20

Published

Very Fast JSON Schema Validator

Readme

mc-schema Build Status Dependency Status

Very Fast JSON Schema Validator. See benchmark generated with z-schema benchmark tool.

This package is collection of ideas json-model and some code z-schema.

It compile JSON schema to JavaScript code, like template engines (see below).

Using

var mcSchema = require('mc-schema');

var result = mcSchema.validate({
    type: 'object',
    required: ['foo', 'bar']
}, {
    foo: 1,
    bar: 2
});

result.valid; // true

result = mcSchema.validate({
    type: 'object',
    required: ['foo', 'bar']
}, {
    foo: 1
});

result.valid; // false

Precompile

For best speed you can compile schemas before validating:

var schema = mcSchema.compile({
    type: 'object',
    required: ['foo', 'bar']
});

schema.validate({}).valid; // false

Compilation looks like this:

var schema = {
    type: 'number',
    minimum: 2
};

// compiles to function like

function validate(data) {
    if (typeof data != 'number') { throw 'invalid type'; }

    if (data > 2) { throw 'minumum'; }
}

Schema default property

It can be define default properties for undefined values by schema if property is required:

var data = {};
var schema = mcSchema.compile({
    type: 'object',
    required: ['foo']
    properties: {
        foo: {
            default: 1
        },
        bar: {
            default: 2
        }
    }
});

var result = schema.validate(data);

result.valid === true;
result.errors.length === 0;
result.data === data;
//
// data === {
//  foo: 1 // only `foo` sets to `default` because it is required
// }
//

Coerce after validate

You can fix invalid JSON objects by using schema.coerce. It tries to fix value when INVALID_TYPE error happens, delete invalid properties and items on OBJECT_ADDITIONAL_PROPERTIES or ARRAY_ADDITIONAL_ITEMS errors. In other cases it deletes all invalid values. If attemption to fix value is failed, it returns undefined;

Simple case:

var schema = mcSchema.compile({
    type: 'object',
    additionalProperties: false,
    required: ['foo'],
    properties: {
        foo: { type: 'boolean' },
        bar: { type: 'string' }
    }
});

var result = schema.coerce({
    foo: 'false',
    bar: 1,
    baz: {}
});

// result = {
//  foo: false,
//  bar: '1'
// };

var result2 = schema.coerce({
    bar: 'baz'
});

//
// result2 = undefined
//

Complex case:

var schema = mcSchema.compile({
    type: 'object',
    additionalProperties: false,
    properties: {
        foo: {
            type: 'array',
            additionalItems: false,
            items: [{ type: 'boolean' }]
        },
        bar: {
            type: 'array',
            items: {
                type: 'object',
                required: ['baz'],
                properties: {
                    baz: {
                        type: 'integer'
                    }
                }
            },
            minItems: 1 
        }
    }
});

var result = schema.coerce({
    foo: ['false', 1],
    bar: [{}]
});

// result = {
//  foo: [false]
// }
// 
var result2 = schema.coerce({
    foo: ['false', 1],
    bar: [{ baz: '2.1'}]
});

// result2 = {
//  foo: [false],
//  bar: [{baz: 2}]
// }

Custom type coerce

To add custom type or redefine coerce, use mcSchema.addTypeCoerce:

mcSchema.addTypeCoerce('object', function(value) {
    try {
        return JSON.parse(value);
    } catch (e) {}
});

var schema = mcSchema.compile({
    type: 'object',
    properties: {
        foo: { type: 'number' }
    }
});

var result = schema.coerce('{"foo":"1"}');
// result = { foo: 1 }

Collect errors

By default mc-schema stops validating after first error. Use collectErrors option for prevent it behaviour in validate:

var schema = mcSchema.compile({
    type: 'array',
    items: {
        type: 'integer'
    }
});

var result = schema.validate([1, 2, '3', false], { collectErrors: true });
// result.errors = [
//  {
//      code: 'INVALID_TYPE',
//      expected: ['integer'],
//      actual: 'boolean',
//      dataPath: '#/3'
//  },
//  {
//      code: 'INVALID_TYPE',
//      expected: ['integer'],
//      actual: 'string',
//      dataPath: '#/2'
//  }
// ]

Tests

This validator uses JSON Schema Test Suite.