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

deal-validator

v1.0.0

Published

Provides validation for use in domain-driven design

Downloads

4

Readme

Deal

An object validator for domain-driven design.

Named after Deal, a Grateful Dead song first performed in 1971. Its about an experienced gambler, but (and this is a stretch) its first verse could apply to domain-driven design.

Since it cost a lot to win
and even more to lose
You and me bound to spend some time
wondring what to choose

My favorite performance was on February 26, 1977 at the Swing Auditorium in San Bernardino, CA.

Example Usage


var d = require('deal-validator');

> var createSong = d.define({
  artist: [d.required, d.string],
  title: [d.required, d.string],
  year: [d.optional, d.number]
});

// Given valid input, a valid object is returned
> createSong({
  artist: 'Grateful Dead',
  title: 'Deal'
});
{ artist: 'Grateful Dead', title: 'Deal' }

// If any of the validations fail, an error is thrown
> createSong({ title: 'Deal' });
Error: value cannot be undefined
    at required (/Users/ryanaghdam/Desktop/deal/index.js:28:13)
    at /Users/ryanaghdam/Desktop/deal/node_modules/ramda/dist/ramda.js:6122:33
    at f1 (/Users/ryanaghdam/Desktop/deal/node_modules/ramda/dist/ramda.js:166:27)
    at XWrap.reducer [as f] (/Users/ryanaghdam/Desktop/deal/index.js:9:45)
    at XWrap.@@transducer/step (/Users/ryanaghdam/Desktop/deal/node_modules/ramda/dist/ramda.js:701:25)
    at _arrayReduce (/Users/ryanaghdam/Desktop/deal/node_modules/ramda/dist/ramda.js:4148:46)
    at _reduce (/Users/ryanaghdam/Desktop/deal/node_modules/ramda/dist/ramda.js:4178:24)
    at Object.f3 [as reduce] (/Users/ryanaghdam/Desktop/deal/node_modules/ramda/dist/ramda.js:266:24)
    at /Users/ryanaghdam/Desktop/deal/index.js:18:16
    at repl:1:1

// Any attributes not defined in the schema are ignored
> createSong({
  artist: 'Grateful Dead',
  title: 'Playing in the Band',
  ruinedByDonna: true
});
{ artist: 'Grateful Dead', title: 'Playing in the Band' }

// Custom validator
var nonEmptyArray = function (value) {
  // validate that the input is an array
  value = d.array(value);

  if (value.length === 0) {
    throw new Error('array cannot be empty');
  }

  return value;
}

// Custom validators, using composition
var nonEmpty = function (value) {
  if (value.length === 0) {
    throw new Error('cannot be empty');
  }

  return value;
}

var nonEmptyArray = R.compose(nonEmpty, d.array);

var creator = d.define({
  p: [nonEmptyArray, d.required]
});

API

The following is a list of exported functions.

define(schema)

Returns a function that validates against the given schema.

Validators

Validators accept a single value and return it if it passes the validator. If it fails, an error is thrown.

optional(value)

Returns the given value. This function is purely syntactic; it allows for optional fields to be explicitly defined as such.

required(value)

Returns the given value if it is not undefined. Throws an error if it is undefined.

string(value)

Returns the given value if it is a string. An empty string is considered valid input.

boolean(value)

Returns the given value if it is a boolean or undefined. An error is thrown if the input is not a boolean.

number(value)

Returns the given value if it is a number or undefined. Infinity, -Infinity, and NaN are not considered valid input.

function(value)

Returns the given value if it is function or undefined.

array(value)

Returns the given value if it is an array or undefined. An empty object is considered valid input.

object(value)

Returns the given value if it is an object or undefined. An empty object is considered valid input.

Planned Enhancements

  • Support schemas with nested objects
  • Support custom error messages