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

dee-validator

v2.0.2

Published

Object fields validator

Downloads

12

Readme

Dee Validator

npm npm Coverage Status dependency Status devDependency Status Build Status Known Vulnerabilities node

Validator for NodeJS.

Table of contents

Migration to v2

The v1 doesn't support async validators meaning the API is synchronous. For migration to v2, await getErrors and hasErrors methods.

Usage

To use the validator just create new validator instance with an object for validation as a parameter. Use property method to choose a value and state methods to validate the value.

Example of code:

const Validator = require('dee-validator');
const validator = new Validator({
    field1: 'test',
    field2: 10,
    field3: true
});

validator.property('field1').isNotEmpty().isEqual('1');

validator.property('field2').isNotEmpty().isInteger({
    min: 1,
    max: 20
});

validator.property('field3').isNotEmpty().isString().withMessage('field3 should be a special string.');

await validator.hasErrors(); // true
await validator.getErrors();
// [
//    { path: 'field1', value: 'test', errorMessage: 'field1 should be equal 1' },
//    { path: 'field3', value: true, errorMessage: 'field3 should be a special string.' }
// ]

API

Validator

Validator.extend(customValidators)

Register new custom validators.

Args:

  • customValidators [Object]: object with custom field validators.

Example:

// here we create two custom validators.
// First validator: isTest. It checks if value equal to 'test' string. In case it's not, error message will be = 'should pass isTest validation'
// Second validator: isDivisibleBy. It accepts value and divisible number and check if value is divided by the number. In case it's not - custom error message will be created.
// If user doesn't pass any divisible number, 2 will be used as default.
{
  isTest: {
    execute: value => value === 'test'
  }
  isDivisibleBy: {
    execute: (value, divizor) => typeof value === 'number' && value % divizor === 0,
    getErrorMessage: (divizor) => return `should be divided by ${divizor}`,
    defaultOpts: 2
  }
}

Validator instance

validatorInstance.getValidationObject()

Get object for validation.

validatorInstance.property(propertyPath)

Create new validator state instance and return it for chaining.

Args:

  • propertyPath [String]: path to property in validation object (lodash.get is used to resolve a value)

validatorInstance.hasErrors()

Return Promise<true> if validator has errors. In the other case return Promise<false>.

validatorInstance.getErrors()

Return promise with all validation errors.

Example of an error:

{
    path: 'a',
    value: 'test',
    errorMessage: 'value should be a number'
}

Validator state instance

validatorStateInstance.optional()

Apply all checks only if a value is not undefined or null.

validatorStateInstance.withMessage(errorMessage)

Set custom error message instead of default one.

See other methods available on state instance.

Error message format

The validator creates default error message for each property. It's quite readable (you can see how the message is created in each field validator implementation). If you are not satisfied with default error message, you can use withMessage method to create a new one.

Fields validators

  • isArray() - check if a value is array.
  • isArrayLength(opts) - check if an array value has correct length. opts is an object which defaults to { min: 0, max: undefined }. min and max options set acceptable range for the array length.
  • isBase64String() - check if a string is in base64 format.
  • isBoolean(opts) - check if a value is boolean. opts is an object which defaults to { convert: true }. If convert = true string values like 'true'/'false' are accepted as booleans.
  • isDate(opts) - check if a value is date. opts is an object which defaults to { before: undefined, after: undefined }. after and before options set acceptable range for the date.
  • isEachIn(inArray) - check if each item from value is in inArray.
  • isEmail() - check if a string value is an email.
  • isEqual(equalTo) - check if a value is equal to equalTo.
  • isFloat(opts) - check if a value is a float. opts is an object which defaults to { min: 0, max: undefined, convert: true }. min and max options set acceptable range for the float value. If convert = true and value is a string the value will be converted to a float.
  • isIn(inArray) - check if a value is in inArray.
  • isInteger(opts) - check if a value is an integer. opts is an object which defaults to { min: 0, max: undefined, convert: true }. min and max options set acceptable range for the integer value. If convert = true and value is a string the value will be converted to an integer.
  • isIpString(opts) - check if a string value is correct ip address. opts is an object which defaults to { v4: undefined, v6: undefined}. v4 and v6 are formats for checking. If both are undefined, the string value should be in ipv4 or ipv6 formats.
  • isJsonString() - check if a value is json string(JSON.parse is used).
  • isLength(opts) - check if a string value has correct length. opts is an object which defaults to { min: 0, max: undefined }. min and max options set acceptable range for the string length.
  • isLowerCaseString() - check if all letters in a string are lowercase.
  • isMatch(regexp) - check if a string value is matched to regexp.
  • isNotEmpty() - check if a string is not empty.
  • isNumericString() - check if a string contains only numbers.
  • isString() - check if a value is a string.
  • isUpperCaseString() - check if all letters in a string are uppercase.
  • isUrlString() - check if a string value is a correct url.

What's in a name?

Dee is one of my favorite detective characters - Judge Dee.

Author

Ilya Markevich - @ilya_mark91