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

node-models

v2.1.5

Published

A minimalist models module backed by validimir

Downloads

7

Readme

SYNOPSIS

Models backed by validimir. Define, validate and sanitize without any DSLs or schema languages.

BUILD

Build Status

USAGE

const Models = require('node-models')

let v = Models.create({
  'beep.boop': Models.validators().string('boop must be a string').len(5),
  'foo.bar.bazz.quxx': Models.validators().number()
})

let data = { beep: { boop: 'k' }, foo: { bar: { bazz: { quxx: 100 } } }

let result = v(data)

if (result.length === 0) console.log('No errors!')
else console.log(result.errors)

Function Models.create(Object obj [, Object model, ...])

create(object) accepts an object with object paths for keys and validation functions for values, returns a validator function. It can also be extended with other models by passing them in as additional arguments.

let v = Models.create({
  'beep.boop': Models.validators().string('boop must be a stirng').len(3)
})

Function Models.validators([Boolean optional]])

Returns a chainable set of validator functions via validimir. Validators cover a good number of use cases, here are a few examples...

// an item is in an array
Models.validators().of(['foo', 'bar'])('foo')

// a custom pattern match
Models.validators().match(/\d/, 'A password must contain at least one number')

// a range
Models.validators().len({ gt: 3, lte: 10 })('a')

Object validate(Object object[, Boolean sanitize])

The validator function only cares about property paths that are specified in the model, that way you can be liberal with data and strict about specific details. You can also pass a bool as the second parameter which will remove any properties that have not been specified in the model.

let data = { beep: { boop: 'bla' }, bla: 100 }
let result = v(data)

if (result.length === 0) console.log('No errors!')
else console.log(result.errors)

When there are errors, they will be detailed, for example...

let v = Models.create({
  'beep.boop': Models.validators().string('boop must be a stirng').len(5)
})

let data = { beep: { boop: 'k' }, bla: 100 }

console.log(v(data))
{ "beep.boop":
   [ { "value": "k",
       "operator": "len",
       "expected": 5,
       "actual": 1,
       "message": "Expected 'k' to have length 5" } ],
  "length": 1 }

LICENSE

https://voltra.co

License