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

is-my-schema-valid

v1.0.3

Published

Simple function that validates data according to JSONSchema

Downloads

41

Readme

is-my-schema-valid

build status npm version Dependency Status Download Count

Simple function that validates data according to JSONSchema spec (under the hood it is powered by is-my-json-valid which uses code generation to be extremely fast).

Install

npm install is-my-schema-valid --save

Usage

validate(data: Object, schema: Object, options: ?Object) -> { valid: Boolean, errors: ?Array<Object> }

Validate data object against passed schema. Function alwats returns object with boolean valid field, if valid is false there will be additional errors field with the list of error objects.

Options

  • filter - filter away fields that are not in the schema, defaults to false
  • filterReadonly - filter away fields that are marked as readonly: true in schema, defaults to false

If you search for express middleware take a look on is-express-schema-valid.

Example

import validate from 'is-my-schema-valid';

const schema = {
    email: {
        type: 'string',
        required: true,
        format: 'email'
    },
    password: {
        type: 'string',
        required: true,
        minLength: 1
    }
};

const notValidData = {email: 'foo', password: 'bar'};
const result1 = validate(notValidData, schema);
console.log(result1.valid); // false
console.log(result1.errors); // list of errors

const validData = {email: '[email protected]', password: '123456'};
const result2 = validate(validData, schema);
console.log(result2.valid); // true

Define schemas

When defining a schema you are able to pass a plain object. In this case is-my-schema-valid will automagically populate your schema with default object properties:

const schema = {
    foo: {
        type: 'string',
        required: true
    }
};

// will be passed to validator as:
// { 
//   type: 'object', 
//   required: true, 
//   additionalProperties: false, 
//   properties: { 
//     foo: { 
//       type: 'string', 
//       required: true 
//     }
//   }
// }

In other cases when you need a different type use a full schema. For example, when payload needs to be an array:

const schema = {
    type: 'array',
    uniqueItems: true,
    items: {
        type: 'number'
    }
};

// it will be used as is by validator

Formats

There are several additional formats added for easy validating the requests:

  • "mongo-object-id" - check if a string is a valid hex-encoded representation of a MongoDB ObjectId
  • "alpha" - check if a string contains only letters (a-zA-Z)
  • "alphanumeric" - check if a string contains only letters and numbers
  • "numeric" - check if a string contains only numbers
  • "hexadecimal" - check if a string is a hexadecimal number
  • "hexcolor" - check if a string is a hexadecimal color
  • "base64" - check if a string is Base64 encoded
  • "decimal" - check if a string is a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
  • "int" - check if a string is an integer
  • "float" - check if a string is a float
  • "uuid" - check if a string is UUID
  • "data-uri - check if a string is data uri format

In the example below we can ensure that id is valid MongoDB ObjectId:

import validate from 'is-my-schema-valid';

const schema = {
    id: {
        type: 'string',
        format: 'mongo-object-id'
    }
};

Just a reminder that there are default built-in formats supported by JSONSchema:

JSONSchema

In order to get comfortable with JSONSchema spec and its' features I advice you to check the book "Understanding JSON Schema" (also PDF version) or look at examples.


MIT Licensed