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

@dock365/validator

v0.13.2

Published

[![Build Status](https://travis-ci.org/dock365/validator.svg?branch=master)](https://travis-ci.org/dock365/validator) [![npm Version](https://img.shields.io/npm/v/@dock365/validator.svg)](https://www.npmjs.com/package/@dock365/validator)

Downloads

62

Readme

Validator

Build Status npm Version

A general purpose validation library to validate strings or numbers.

Install

# Using yarn package manager
$ yarn add @dock365/validator

# Using npm package manager
$ npm install --save @dock365/validator

Usage

Initialize new validator instance with default configurations

import Validator from "@dock365/validator";

const validator = new Validator();

Initialize new validator instance with custom fail messages.

  • $field will be replaced with field name.

  • $maxLength, $include etc will be replaced with defined validation criteria values.

  • $value will be replaced with value to be validated.

import Validator from "@dock365/validator";

const validator = new Validator({
  failMessages: {
    include: "Custom validation message for $field to validate the value contain '$include'",
    maxLength: "custom validation message to validate $field max length",
    maxValue: "custom validation message to validate $field max value",
    minLength: "custom validation message to check $field min length",
    minValue: "custom validation message to check $field min value",
    noTrailingSpace: "custom validation message to check $field has trailing spaces",
    required: "custom validation message to check $field is required",
    type: "custom validation message to check $field type",
    regx: "custom validation message to check $regx type",
    /* All values are optional, if not defined default will be used */
  }
});

Validate a value with initially defined/default fail message

// String
validator.string(
  "Title", // Field Name
  "Some Value", // Value to be validated
  {
    required: true,
    /* validation criterias*/
  },
)

// Number
validator.number(
  "Title", // Field Name
  25, // Value to be validated
  {
    required: true,
    /* validation criterias*/
  },
)

Validate a value with custom fail message

// String
validator.string(
  "Title", // Field Name
  "Some Value", // Value to be validated
  {
    required: true,
    /* validation criterias*/
  },
  {
    required: "If the $field doesnt have a value this message will be displayed",
    type: "If the $field is not a string this message will be displayed",
    /* All values are optional, if not defined default will be used */
  }
)

// Number
validator.number(
  "Title", // Field Name
  25, // Value to be validated
  {
    required: true,
    /* validation criterias*/
  },
  {
    required: "If the $field doesnt have a value this message will be displayed",
    type: "If the $field is not a number this message will be displayed",
    /* All values are optional, if not defined default will be used */
  }
)

Sample response

{
  success: true,
  messages: [ /*...validation messages if fail*/ ],
}

Options

Initialization configurations

| Name | Type | Default | Description | | :----------------- | :----- | :----------------- | :------------------------------------------------------------------- | | failMessages | Object | Default fail messages | Here define custome valiadtion messages and all other configurations |

failMessages

| Name | Type | Default | Description | | :--------------- | :----- | :-------------------------------------------------- | :---------------------------------------------------------------------- | | required | string | $field is required! | validation fail message if field is required and it's not present | | regx | string | $field must match $regx! | validation fail message if field does not match regx | | type | string | $field must be a $type! | validation fail message if type check fails | | minLength | string | $field can't be less than $minLength charectors! | validation fail message if field length is less than specified value | | maxLength | string | $field can't be greater than $maxLength charectors! | validation fail message if field length is greater than specified value | | minValue | string | $field can't be less than $minValue! | validation fail message if field value is less than specified value | | maxValue | string | $field can't be greater than $maxValue! | validation fail message if field value is greater than specified value | | include | string | $field doesn't contain '$include'! | validation fail message if value doesn't contain specified value | | noTrailingSpaces | string | $field must not contain any trailing spaces! | validation fail message if value contain trailing space |

Validation criterias

Base

| Name | Type | Default | Description | | :------- | :------ | :------ | :------------------------------------------------------------------------ | | required | Boolean | false | return success as false if the field is required and value is not present |

String

| Name | Type | Default | Description | | :--------------- | :------ | :-------- | :-------------------------------------------------------------------------------- | | minLength | number | undefined | return success as false if the field value length is less than specified value | | maxLength | number | undefined | return success as false if the field value length is greater than specified value | | regx | regx | undefined | return success as false if the field doesn't match specified regx | | include | string | undefined | return success as false if the field doesn't contain specified include string | | noTrailingSpaces | boolean | undefined | return success as false if the field contains trailing space |

Number

| Name | Type | Default | Description | | :------- | :----- | :-------- | :------------------------------------------------------------------------- | | minValue | number | undefined | return success as false if the field value is less than specified value | | maxValue | number | undefined | return success as false if the field value is greater than specified value |

Contributing!

All contributions are super welcome!

License

Validator is MIT licensed.