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

typology

v1.3.0

Published

A data validation library for Node.js and the browser.

Downloads

538

Readme

Typology

Typology is a lightweight data validation library for Node.js and the browser (with or without Browserify).

It can validate variables against native JavaScript types as well as against custom types you can define.

Installation

Install with npm:

// Latest release
npm install typology

// Development version
npm install git+https://github.com/jacomyal/typology.git

Usage

Get the native type of a given variable

var types = require('typology');

types.get(true);
>>> 'boolean'

types.get(/abc/);
>>> 'regexp'

// Native types being:
// 'boolean', 'number', 'string'
// 'function', 'array', 'arguments'
// 'regexp', 'date', 'object'
// 'null', 'undefined', 'primitive'
// 'map', 'set', 'weakmap', 'weakset', 'symbol'

Deal with custom types

A custom type can be either be defined by a function returning a boolean or an expressive string or object describing the type you want to check.

Example

// Type defined by a function
var customType = function(variable) {
  // Here is an example to know if a variable is an integer:
  return typeof variable === 'number' && variable === (variable | 0);
};

// Type defined by an expressive string
var customType = '?string'; // Means you want an optional string
var customType = 'string|number'; // Means you want either a string or a number

// Type defined by a complex object
var customType = {
  firstname: 'string',
  lastname: 'string',
  age: 'number'
};

Custom types syntax

|Expression|Description|Examples|Validates| |---|---|---|---| |'type'|required|'string'|'hello'| |'?type'|optional|'?string'|'hello', undefined, null| |'type1\|type2'|multi-types|'string\|number'|'hello', 45, 2.34| |{prop: 'type'}|complex|{firstname: 'string'}|{firstname: 'Joachim'}| |['type']|lists|['number']|[1, 2, 3]| |'!type'|exclusive|'!string'|42|

Note also that expression can be combined. For instance '?string|number' means an optional string or number variable and '!string|object' means anything but a string or an object.

Overkill example

var myCustomType = {
  firstname: 'string',
  pseudo: '?string',
  account: {
    total: '?number|string',
    bills: ['number']
  }
}

Using functions to perform ad-hoc validation

var myCustomType = {
  age: 'number',
  name: function(v) {
    return v === 'Jack' || v === 'John';
  }
};

Validate a variable against a custom type

var types = require('typology');

types.check(myType, myVariable);

// Example
types.check('number', 1);
>>> true

types.check(
  {
    firstname: 'string',
    lastname: 'string',
    age: 'number'
  },
  {
    firstname: 'Joachim',
    lastname: 'Murat'
  }
);
>>> false

Getting more information about what does not match

var types = require('typology');

types.scan(myType, myVariable);

// Example
types.scan('number', 1);
>>> { expected: 'number',
>>>   type: 'number',
>>>   value: 1 }

types.scan(
  {
    firstname: 'string',
    lastname: 'string',
    age: 'number'
  },
  {
    firstname: 'Joachim',
    lastname: 'Murat'
  }
);
>>> { error: 'Expected a "number" but found a "undefined".',
>>>   expected: 'number',
>>>   type: 'undefined',
>>>   value: undefined,
>>>   path: [ 'age' ] }

Create your own typology to add custom types

var Typology = require('typology');

var myTypology = new Typology();

// Then add custom definitions
myTypology.add(myCustomType);

// Example
myTypology.add('User', {
  firstname: 'string',
  lastname: 'string',
  age: '?number'
});

// Then you can use it likewise
myTypology.check('User', {hello: 'world'});
>>> false

// And use it in other types' definition
myTypology.check('User|number', myVar);

Checking whether a custom type's definition is valid

var types = require('typology');

types.isValid(customType);

// Example
types.isValid('?string');
>>> true

types.isValid('randomcrap');
>>> false

Contribution

Build Status

Contributions are welcome. Please be sure to add and pass unit tests if relevant before submitting any code.

To setup the project, just install npm dependencies with npm install and run tests with npm test.

Code style

We use prettier for the code style, with its default setup. It is plugged as a precommit hook, so you don't have to worry about it.

Also, as an internal convention, please:

  • Write __myVar for any global private variable
  • Write _myVar for any instance private variable
  • Write myVar any local variable

License

Typology is under a MIT license.