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

middle-out

v1.0.0-beta.15

Published

![Visual Studio Team services](https://img.shields.io/vso/build/derekworthen/28f9ce90-1a76-4c9b-b520-b4a0dd24477d/4.svg) ![Coveralls github](https://img.shields.io/coveralls/github/dworthen/middle-out.svg) ![npm](https://img.shields.io/npm/v/middle-ou

Downloads

11

Readme

Middle Out

Visual Studio Team services Coveralls github npm

Agnostic data validation.

API

Data Validation with ES Classes and Decorators

import * as MO from 'middle-out';

class Person {

    @MO.required()
    @MO.typeOf({dataType: 'string'})
    @MO.stringLength({min: 3, max: 20})
    name = "Derek";
    
    @MO.mobilePhone()
    phone = 5553336543;

    @MO.emailAddress()
    email = "[email protected]";
    
    @MO.range({min: 0, max: 150})
    age = 55;

    @MO.compare({
        comparingProperty: 'age',
        predicate: (target, parents, age) => (target[parents] > target[age])
    })
    parentsAge = 78;
    
    @MO.creditCard()
    creditCard = "4444333322221111";    
}

let person = new Person();

let [isValid, errors] = MO.validate(person); 
// true, {}

person.age = -10;

[isValid, errors] = MO.validate(person);
// false, {age: {range: ...}}

Data Validation with ES5/Plain Object

var obj = {
    name: "Derek",
    age: 55
};

MO.required()(obj, "name");
MO.emailAddress()(obj, "name");
MO.range({min: 0, max: 150})(obj, "age");

var results = MO.validate(obj);

Installation

npm install middle-out

Usage

NodeJS/CommmonJS

var MO = require('middle-out');

ES Modules

import { required } from 'middle-out/dist/esm/required';

Note on build tools. Webpack and rollup use ES modules by default so it is fine to write import { required } from 'middle-out';.

Script Tag/AMD

<script src="https://unpkg.com/[email protected]/dist/umd/index.js"></script>
<script>
    var obj = {
        name: "Derek",
        age: 55
    };

    // library exposed as MiddleOut global
    MiddleOut.required()(obj, "name");
    var results = MiddleOut.validate(obj);
</script>

Custom Validators

// canDrive.js
const canDrive = MO.registerValidator("canDrive", (target, property, config) => {
    return target[property] >= config.drivingAge;
});

// someOtherFile.js
import { canDrive } from './canDrive';

class Person {
    @canDrive({drivingAge: 16})
    age = 15;
}

Default Config Object

registerValidator returns a decorator factory, (config?) => ((target, property) => void), a function that accepts an optional config object and returns a function suitable for decorating an object property. Config is optional, provide a default object and validate.

const canDrive = MO.registerValidator("canDrive", (target, property, config) => {

    // set default properties for config object
    config = Object.assign({
        permitAge: 15,
        drivingAge: 16
    }, config || {} /* config is optional so it may be undefined */);

    // Validate config. User may have passed in {drivingAge: "Hello"}
    if(typeof config.drivingAge !== "number") {
        throw new TypeError(`Invalid parameter config.drivingAge. Expecting number but received ${typeof config.drivingAge}`);
    }

    if(typeof config.permitAge !== "number") {
        throw new TypeError(`Invalid parameter config.permitAge. Expecting number but received ${typeof config.permitAge}`);
    }

    return target[property] >= config.drivingAge;
});

Target and Property

Validators can be called on plain objects, i.e., required()(obj, 'age');. Validators need to ensure target and property exist.

const canDrive = MO.registerValidator("canDrive", (target, property, config) => {
    if(typeof target !== 'object') {
        throw new TypeError(`Invalid argument 'target'. Expecting object but received ${typeof target}.`);
    }
 
    if(typeof property !== 'string') {
        throw new TypeError(`Invalid argument property. Expecting string but received ${typeof property}.`);
    }
    // or use the utility function MO.checkTargetAndProperty(target, property);

    // Validate the type being validated
    if(typeof target[property] !== 'number') {
        throw new TypeError("candDrive works with numbers only!!");
    }
    ...
}

Compose Validators

Validators should do one thing.

const canDrive = MO.registerValidator("canDrive", (target, property, config) => {
    // check target and property exist
    MO.checkTargetAndProperty(target, property);

    // return true if there is nothing to validate
    if(target[property] === undefined || target[property] === null) {
        return true; //use @required validator to enforce a property exists
    }

    // Validate the type being validated
    if(typeof target[property] !== 'number') {
        throw new TypeError("candDrive works with numbers only!!");
    }

    // set default properties for config object
    config = Object.assign({
        permitAge: 15,
        drivingAge: 16
    }, config || {} /* config is optional so it may be undefined */);

    // Validate config. User may have passed in {drivingAge: "Hello"}
    if(typeof config.drivingAge !== "number") {
        throw new TypeError(`Invalid parameter config.drivingAge. Expecting number but received ${typeof config.drivingAge}`);
    }

    if(typeof config.permitAge !== "number") {
        throw new TypeError(`Invalid parameter config.permitAge. Expecting number but received ${typeof config.permitAge}`);
    }

    return target[property] >= config.drivingAge;
});