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 🙏

© 2025 – Pkg Stats / Ryan Hefner

model-validate

v0.1.2

Published

Data validation framework for MVC

Readme

ModelValidate Circle CI

MVC validation for JavaScript

SyncValidator

src/index.js:20-71

Class for synchronous validation.

Parameters

  • validators (optional, default {})

validate

src/index.js:39-70

Validate a model according to the validators set up in mask

Parameters

  • model Object Data to be validated Ex: { phone : "123-456-7890" }

  • mask Object Validators to be applied to model. Ex: { phone : ['required', 'phone']}

    The following validators are provided by default

    • required checks for non-null, non-whitespace, non-empty strings or Numbers
    • phone validates 10 or 11 digit phone numbers reguardless of formatting characters
    • credit-card validates credit card numbers according to a LUHN-10 check
    • zip-code validates American 5-digit and 9-digit zip codes

Example

import {SyncValidator} from 'model-validate';

// Get a validator with just the built-in validators
let validator = new SyncValidator();

// sample data including some errors
let model = {
  address : {
    firstName : 'John',
    address1  : '123 SomeRoad',
    address2  : 'apt 4',
    city      : 'New York',
    state     : '    ',
    zip       : '123456',
  },
  phone : '1-(123)-123-1234',

}

// get validation results
let results = validatator.validate(model, {
  // the mask object mirrors the structure of the model
  address : {
    firstName : ['required'],
    lastName  : ['required'],
    address1  : ['required'],
    city      : ['required'],
    state     : ['required'],
    zip       : ['required', 'zip-code'],
  },
  phone : ['required', 'phone'],
})

// This returns the following structure
{
  "address": {
    "firstName": {
      "required": {
        "_valid": true
      },
      "_valid": true
    },
    "lastName": {
      "required": {
        "_valid": false,
        "reason": "\'lastName\' is null or undefined"
      },
      "_valid": false
    },
    "address1": {
      "required": {
        "_valid": true
      },
      "_valid": true
    },
    "city": {
      "required": {
        "_valid": true
      },
      "_valid": true
    },
    "state": {
      "required": {
        "value": false,
        "reason": "String is empty"
      },
      "_valid": false
    },
    "zip": {
      "required": {
        "_valid": true
      },
      "zip-code": {
        "_valid": false,
        "reason": "Zip must be 5 digits or have the full extended syntax"
      },
      "_valid": false
    },
    "_valid": false
  },
  "phone": {
    "required": {
      "_valid": true
    },
    "phone": {
      "_valid": true
    },
    "_valid": true
  },
  "_valid": false
}

There is a _valid property at every level that describes the validity of everything at that level and lower. This way you can decide to use the data or not simply based on results._valid, but you can show feedback to the user based on the valididy of a specific validation.

For example model.address.zip passes the required validator, but not the zip-code validator because it has 6 digits instead of 5. So results.address.zip.required._valid == true, but results.address.zip.zip-code._valid == false