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

micro-check.js

v1.3.0

Published

Javascript error checking library

Downloads

2

Readme

Check.js

Javascript input and schema checking library.

Installation

npm install microcheck.js

Usage

The module is packaged for all module style. The main one in for ES6 import.

ES6

For general import.

import microcheck.js from 'micro-check.js';

CommonJS

const microcheck = require('micro-check.js');

IIFE (Immediatly Invocked Function Env)

This for browser environment generally. It will create a microcheck global variable.

<script src="./node_modules/micro-check.js/index.iife.js"></script>

UMD (Universal Module Definition)

see ES6 see CommonJS

Simple Schema validation

// this is the schema to check
const schema = {
  name: String, // define types you want to validate and keys name
  data: Array,
  functions: Object,
};

const schemaGood = {
  name: 'test',
  data: [],
  functions: {
    test: function() {
      return true;
    },
  },
};

const schemaBad = {
  name: 'test',
  data: {},
  functions: {},
};

const schemaBadKey = {
  name: 'test',
  data: {},
};

check.validate(schemaGood, schema)
// null

check.validate(schemaBad, schema)
// Error("data should be of type Array")

check.validate(schemaBadKey, schema)
// Error("missing keys: functions")

Variable keys

const schema = {
  name: String,
  '?data': Array,
  '?functions': Object,
};

const schemaGood = {
  name: 10,
  data: [],
};

const schemaBad = {
  name: 'test',
  data: {},
};

check.validate(schemaGood, schema)
  // null

check.validate(schemaBad, schema)
// Error("data should be of type Array")

Number Constraints


const numberSupSchema = {
  age: '>10',
};
const numberMinSchema = {
  age: '<10',
};
const numberRangeSchema = {
  age: '10<>100',
};

const numberFloatSupSchema = {
  age: '>10.15',
};
const numberFloatMinSchema = {
  age: '<10.15',
};
const numberFloatRangeSchema = {
  age: '10.4<>10.7',
};

check.validate({ age: 11 }, numberSupSchema);
// null
check.validate({ age: 11 }, numberSupSchema);
// null
check.validate({ age: -11 }, numberSupSchema);
// Error("must respect age > 10")
check.validate({ age: 10 }, numberSupSchema);
// Error("must respect age > 10")

check.validate({ age: 9 }, numberMinSchema);
// null
check.validate({ age: -9 }, numberMinSchema);
// null
check.validate({ age: 10 }, numberMinSchema);
// Error("must respect <10");

check.validate({ age: 50 }, numberRangeSchema);
// null
check.validate({ age: 11 }, numberRangeSchema);
// null
check.validate({ age: 10 }, numberRangeSchema);
// Error("must respect 10<>100")
check.validate({ age: 100 }, numberRangeSchema);
// Error("must respect 10<>100")

check.validate({ age: 10.16 }, numberFloatSupSchema);
// null
check.validate({ age: 10.16 }, numberFloatSupSchema);
// null
check.validate({ age: -11 }, numberFloatSupSchema);
// Error("must respect >10.15");
check.validate({ age: 10.14 }, numberFloatSupSchema);
// Error("must respect >10.15");

check.validate({ age: 10.14 }, numberFloatMinSchema);
// null
check.validate({ age: -9 }, numberFloatMinSchema);
// null
check.validate({ age: 10.15 }, numberFloatMinSchema);
// Error("must respect <10.15");

check.validate({ age: 10.5 }, numberFloatRangeSchema);
// null
check.validate({ age: 10.6 }, numberFloatRangeSchema);
// null
check.validate({ age: 10.2 }, numberFloatRangeSchema);
// Error("must respect 10.4<>10.7");
check.validate({ age: 10.7 }, numberFloatRangeSchema);
// Error("must respect 10.4<>10.7");

Regex or Strict equality

const schema = {
  name: new RegExp("^[a-zA-Z0-9\-_]{1,}$"),
  "?data": Object,
};

check.validate({ name: "ok" }, schema);
// null
check.validate({ name: "not ok" }, constructorSchema);
// Error("must respect {
    name: new RegExp("^[a-zA-Z0-9\-_]{1,}$"),
      "?data": Object,
}");


check.validate({ name: "test" }, { name: 'test' });
// null
check.validate({ name: "notok" }, { name: 'test' });
// TypeError;

Array difference


const arrayGood = ['test', 10, { name: 'inside_test' }, [], [10]];
// order of data is not important
//  [[], [10], 'test', 10, { name: 'inside_test' }],
//  ['test', [10], { name: 'inside_test' }, 10, []],
//  [[10], 'test', { name: 'inside_test' }, 10, []]

const arrayBad = ['test'];

check.validate({ data: arrayGood }, arraySchema);
// null

check.validate({ data: arrayBad }, arraySchema);
// Error(`must respect ${arrayGood}`)

Custom functions

const testVars = ['test', 10];
const testInput = { name: 'test' };
const schema = {
  name: input => testVars.includes(input),
};


check.validate(testInput, schema);
// null
check.validate({ name: 'test' }, schema);
// null
check.validate({ name: 'lol' }, schema)
// Error("ValidationError: "lol" did not pass input => testVars.includes(input): returned false")

License

Copyright 2018 Ciro DE CARO

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.