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

validatar

v0.0.9

Published

A simple way to validate data.

Downloads

8

Readme

validatar

Javascript data validator. A simple way to validate date and customerize the constraint.

Installation

npm install validatar

Links

npm package Github page async-validatar

Usage

Example

const Validatar = require('validatar');

Validatar.register('required', '%{key} is required', (v) => v !== undefined);
Validatar.register('isString', '%{key} is not a string. value:%{value} position:%{position}', (v) => (typeof (v) === 'string'));

const data = {
  str1: 'I am a string!',
  obj1: {
    str2: 456,
  },
};

const rule = {
  str1: [{ constraint: 'isString', message: 'str1 must be string' }],
  obj1: {
    str2: ['required', 'isString'],
  },
};

const result = Validatar.validate(data, rule);
console.log(result);
// expected output:
// {
//   constraintId: 'isString',
//   position: 'obj1.str2',
//   key: 'str2',
//   value: 456,
//   params: undefined,
//   message: 'str2 is not a string. value:456 position:obj1.str2',
// }

Pass parameters to check function

const Validatar = require('validatar');

Validatar.register('isString', '%{key} is not a string. value:%{value} position:%{position}', (v) => (typeof (v) === 'string'));
Validatar.register('checkLength', '%{key} length error.', (v, params) => (v.length >= params[0] && v.length <= params[1]));

const data = {
  str1: 'I am a string!',
};

const rule = {
  str1: [
    'isString', {
      constraint: 'checkLength',
      params: [5, 12],
    },
  ],
};

const result = Validatar.validate(data, rule);
console.log(result);
// expected output:
// {
//   constraintId: 'checkLength',
//   position: 'str1',
//   key: 'str1',
//   value: 'I am a string!',
//   params: [5, 12],
//   message: 'str1 length error.',
// }

Main Function

Validatar.register

/**
 * Register a constraint
 * @param {string} constraintId
 * @param {Any truthy value} message: The message of the constraint. Will return when the constraint is violated.
 * @param {function} checkFunction: If the return value of this function is not true, the constraint is violated.
 */
Validatar.register('checkLength', '%{key} length error.', (v, params) => (v.length >= params[0] && v.length <= params[1]));

Validatar.validate

/**
 * Validate the input data
 * @param {Object} data: The input data
 * @param {Object} rule: The rule to check the input data
 * @return {undefined | Object} return undefined if pass the rule.return object if any constraint in the rule is violated. 
 * {
 *   constraintId: {string} The constraint id that is violated.
 *   position: {string} The position where the constraint is violated.
 *   key: {string} The key where the constraint is violated.
 *   value: {Any} The value where the constraint is violated.
 *   params: {Any}
 *   message: {Any} The message of the constraint
 * }
 */
const result = Validatar.validate(data, rule);

Another Example

const Validatar = require('validatar');

Validatar.register('required', new Error('Wrong input format'), (v) => v !== undefined);
Validatar.register('isString', '%{key} is not a string. value:%{value}', (v) => (typeof (v) === 'string'));
Validatar.register('isEmail', '%{key} is not email. value:%{value}', (v) => (/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(String(v).toLowerCase())));

const correctData = {
  str1: 'I am a string!',
  obj1: {
    email: '[email protected]',
  },
};

const rule = {
  str1: [{ constraint: 'required', message: 'str1 not exists' }, { constraint: 'isString', message: new Error('str1 must be string') }],
  obj1: {
    email: ['required', 'isString', 'isEmail'],
  },
};

const correctResult = Validatar.validate(correctData, rule);
console.log(correctResult);
// expected output: undefined

const wrongData = {
  str1: 'I am a string!',
};

const wrongResult = Validatar.validate(wrongData, rule);
console.log(wrongResult);
// expected output:
// {
//   constraintId: 'required',
//   position: 'obj1.email',
//   key: 'email',
//   value: undefined,
//   params: undefined,
//   message: new Error('Wrong input format')
// }