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

untie

v0.3.1

Published

Simple validators for your objects

Downloads

18

Readme

Readme

GitHub stars GitHub forks npm GitHub issues Maintainer

Untie is a simple validator for JS-objects.

Code Climate Test Coverage Issue Count Build Status David

Usage

const { Type, Validator } = require('untie');

const input = {
  user: {
    id: 13,
    nickname: 500,
    profile: {
      firtname: 'Sergey',
      lastname: 'Sova',
    },
  },
};

class UserValidator extends Validator {
  static schema = {
    id: Type.number.Required,
    name: Type.string.Required,
    profile: Type.shape({
      firstname: string,
      lastname: string,
    }),
  };
}

console.log(UserValidator.validate(input));

Simple docs

validate method returns:

  • null if passed document is valid.
  • Error if document has errors.
  • object of Errors if document nested fields has errors.

Installation

NPM

Minimum node

npm install --save untie

Usage with Express

validators/user.js

const { Type, Validator } = require('untie');

module.exports =
class UserValidator extends Validator {
  static schema = {
    login: Type.string.Required,
    email: Type.string.Required,
    password(dataObject, property) {
      const value = dataObject[property];

      if (typeof value !== 'string') {
        return new Error('Invalid property `password`, expected type `string`');
      }

      if (value.length < 8) {
        return new Error(`Property password must be 8 symbols minimum.`);
      }
    }
  };
}

for Node < v6

var untie = require('untie');

module.exports = untie.createValidator({
  login: untie.Type.string.Required,
  email: untie.Type.string.Required,
  password: function(dataObject, property) {
    var value = dataObject[property];

    if (typeof value !== 'string') {
      return new Error('Invalid property `password`, expected type `string`');
    }

    if (value.length < 8) {
      return new Error('Property password must be 8 symbols minimum.');
    }
  }
});

routes/register.js

const UserValidator = require('validators/user');

module.exports = function register(req, res, next) {
  const error = UserValidator.validate(req.body); // return null or error object

  if (error) {
    return next(error);
  }

  // create user model and save
  req.status(201).json({ user: req.body.user });
}

Validators

  • array

  • bool

  • func

  • number

  • object

  • string

  • symbol

  • any

  • arrayOf

  • objectOf

  • oneOf

  • OneOfType

  • shape

More examples

const {
  array,
  bool,
  func,
  number,
  object,
  string,
  symbol,

  any,
  arrayOf,
  objectOf,
  oneOf,
  OneOfType,
  shape,
} = require('untie').Type;


const exampleValidationSchema = {
  optionalArray: array,
  optionalBool: bool,
  optionalFunc: func,
  optionalNumber: number,
  optionalObject: object,
  optionalString: string,
  optionalSymbol: symbol,

  optionalAnyValue: any,

  // Value must one of that values
  optionalEnum: oneOf(['hello', 'world', 'foo', 'bar']),

  // Value must correspond one of type
  optionalUnion: oneOfType([string, number, bool, object, arrayOf(string)]),

  // each item in array must be a number
  optionalNumbersArray: arrayOf(number),

  // Value of each key must be function
  optionalMethods: objectOf(func),

  // Object must correspond defined struct
  optionalPlayer: shape({
    name: string,
    lifes: number,
    uid: number,
  }),

  // That method must be defined
  requiredFunc: func.Required,

  // Should be passed any value
  requiredAny: any.Required,

  customProperty: function(dataObject, property) {
    if (dataObject[property] >= 1200) {
      return new Error('Custom property must be more than 1199.');
    }
  },

  // Required all props in required shape
  requiredShape: shape.Required({
    target: number.Required,
    amount: number.Required,
    message: string.Required,
  }),
};