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

@ololoepepe/validator

v0.0.14

Published

Data validator

Downloads

25

Readme

Validator

Data validator

Example

const {createValidator} = require('@ololoepepe/validator');
const DB = require('../lib/database');

const UserValidator = createValidator({
  type: 'object',
  fields: {
    nickname: {
      type: 'string',           // Nickname must be a string
      minLength: 4,                   // with a minimum length of 4
      maxLength: 20,                  // and maximum length of 20
      regexp: /^[a-zA-Z0-9_]+$/ // consisting of latin letters, digits and underscores
    },
    password: {
      type: 'string', // password must be a string
      minLength: 8,         // with a minimum length of 8
      maxLength: 32,        // and maximum length of 32
      validate: (pwd, done) => {
        // A password must contain at least one digin
        if (!/[0-9]/.test(pwd)) {
          done({code: 'PASSWORD_HAS_NO_DIGITS'});

          return;
        }

        // A password must contain at least one lowercase letter
        if (!/[a-z]/.test(pwd)) {
          done({code: 'PASSWORD_HAS_NO_LOWERCASE_LETTERS'});

          return;
        }

        // A password must contain at least one uppercase letter
        if (!/[A-Z]/.test(pwd)) {
          done({code: 'PASSWORD_HAS_NO_UPPERCSE_LETTERS'});

          return;
        }

        done(null, pwd);
      },
      transformAfter: async (pwd, done) => {
        try {
          const salt = await DB.from('salt').select('value').where('type', '=', "'password'");

          // A password must be hashed after validation
          done(null, Crypto.createHash('sha1').update(pwd).update(salt).digest('hex'));
        } catch (err) {
          done({code: 'INTERNAL_ERROR'});
        }
      }
    },
    kittens: {
      type: 'number',
      minValue: 0,
      transformBefore: (count, done) => {
        // A value may be passed as a string, and it will be converted to number before any validation
        done(null, Number(count));
      }
    }
  }
});

let user1 = {
  nickname: 'Vasia',
  password: 'pwd123PWD',
  kittens: 1
};

let user2 = {
  nickname: 'Petia)))',
  password: 'pwd321PWD',
  kittens: 0
};

let user3 = {
  nickname: 'Slava',
  password: '123',
  kittens: '0'
};

let user4 = {
  nickname: 'Slava',
  password: 123,
  kittens: '2'
};

UserValidator.validate(user1, (err, result) => {
  console.log(err, result);

  /* null, {
    nickname: 'Vasia',
    password: 'f3e4c83aa7c6a1da18f1facf63dcdf21c3f8a881'
  } */
});

UserValidator.validate(user2, (err, result) => {
  console.log(err, result);

  /* {
    code: 'INVALID_STRING',
    data: 'Petia)))',
    path: ['nickname'],
    info: {}
  } */
});

UserValidator.validate(user3, (err, result) => {
  console.log(err, result);

  /* {
    code: 'PASSWORD_HAS_NO_LOWERCASE_LETTERS',
    data: '123',
    path: ['password'],
    info: {}
  } */
});

UserValidator.validate(user4, (err, result) => {
  console.log(err, result);

  /* {
    code: 'TYPE_MISMATCH',
    data: 123,
    path: ['password'],
    info: {
      expectedType: 'string'
    }
  } */
});

API

createValidator(template, {customTypes = [], maxDelayNsecs = 1000000} = {})

Creates a validator. Accepts validation template, an object in the following form:

{
  type: <type>,
  missingStartegy: [MissingStrategy], // optional
  extraStrategy: [ExtraStrategy], // optional
  defaultValue: [default_value], // optional, only required if missingStartegy is MissingStrategy.Default
  values: Array|Set, // optional, an array or set of values allowed, only valid for types 'string', 'boolean', 'number'
  validateBefore: (data, done, options) => {}, // optional, a custom validation function called before other validators
  validateAfter: (data, done, options) => {}, // optional, a custom validation function called after other validators
  transformBefore: (data, done, options) => {}, // optional, a custom function to transform the object before validation
  transformAfter: (data, done, options) => {}, // optional, a custom function to transform the object after validation
  // ...fields specific for the <type>
}

validateBefore validates data after checking the type, but before any default validators. validateAfter validates data after default validators, just before transformAfter.

Example:

{
  type: 'string',
  regexp: /\d+/,
  transformAfter: (value, done) => done(null, Number(value))
}

validate(data, done, {context = {}, maxDelayNsecs = validatorMaxDelayMsecs} = {})

Performs validation.

  • data -- data to be validated
  • done -- callback function called after validation with either (null, newData) or (error)

Possible options are:

  • context -- a context passed to custom validation functions (empty array by default)
  • maxDelayNsecs -- if validation takes longer than this value (in nanoseconds), next portion of validation will be delayed until next event loop iteration if possible

ValidatorErrorCode

Constant containing the error codes:

{
    BooleanNotInValues: 'BOOLEAN_NOT_IN_VALUES',
    CustomError: 'CUSTOM_ERROR',
    ExtraFields: 'EXTRA_FIELDS',
    InvalidArrayLength: 'INVALID_ARRAY_LENGTH',
    InvalidNumber: 'INVALID_NUMBER',
    InvalidString: 'INVALID_STRING',
    InvalidStringLength: 'INVALID_STRING_LENGTH',
    MissingField: 'MISSING_FIELD',
    NumberNotInteger: 'NUMBER_NOT_INTEGER',
    NumberNotInValues: 'NUMBER_NOT_IN_VALUES',
    NoMatchingVariant: 'NO_MATCHING_VARIANT',
    StringNotInValues: 'STRING_NOT_IN_VALUES',
    TypeMismatch: 'TYPE_MISMATCH',
    UnallowedNull: 'UNALLOWED_NULL'
}

Types

There are several builtin types.

object

A JavaScript object.

Example:

{
  type: 'object',
  fields: {
    // nested fields
  }
}

array

A JavaScript array.

Example:

{
  type: 'array',
  element: {
    // element definition
  }
}

string

A string.

Example:

{
  type: 'string',
  minLength: [M], // optional, minimum string length
  maxLength: [N], // optional, maximum string length
  length: [K], // optional, exact string length
  regexp: [R] // optional, a regular expression to match the string against
}

number

A number.

Example:

{
  type: 'number',
  minValue: [M], // optional, minimum value
  maxValue: [N], // optional, maximum value
  isInteger: [true|false] // optional, specifies if number must be an integer
}

boolean

A boolean.

Example:

{
  type: 'boolean'
}

variant

Used to validate against multiple templates. The result of first successful validation is used.

Example:

{
  type: 'variant',
  variants: [{
    type: 'string',
    regexp: /\d+/,
    transformAfter: (data, done) => done(null, Number(value))
  }, {
    type: 'number'
  }]
}

If hint function is provided to template variants, raw data is checked against that function. Templates with hint returning false-ish values are excluded from validation. Templates are used in the same order they are specified. This may be used to optimize large template validation. Example:

[{
  type: 'object',
  hint: (rawValue, done) => done(rawValue.kind === 'KIND_A'),
  fields: {
    kind: {
      type: 'string',
      values: ['KIND_A']
    }
    // Dozens of fields for kind A
  }
}, {
  type: 'object',
  hint: (rawValue, done) => done(rawValue.kind === 'KIND_B'),
  fields: {
    kind: {
      type: 'string',
      values: ['KIND_B']
    }
    // Dozens of fields for kind B, different from the ones for kind A
  }
}]

If no variant matches the data, a NoMatchingVariant error occurs.

Advanced

isNullable

Any type can be allowed to be null.

Example:

{
  type: 'string',
  isNullable: true
}

Object prototype (class) as "type"

Example:

class CustomClass {
  constructor() {
    //
  }
}

{
  type: CustomClass
}

customTypes may be used to validate instances of a class and preserve them as is. If no validator is specified, 'object' validator is used by default. This leads to loss of some properties (like instance methods) as objects are mapped (no reference equality of input and output data).