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

m-validate

v2.2.7

Published

NodeJS simple validation functions

Downloads

2

Readme

m-validate

m-validate

npm npm stars GitHub forks license

Moblets' NodeJS simple validation to validate data based on a definition JSon.

Table of Contents generated with DocToc

Installation

$ npm install --save m-validate

Available validations

Validations were created for input type, length, required, value and string.

Input type validation

These are all HTML 5 input type validations.

number

Check input type number for integers.

Usage

Definition JSon

{
  "name": "Number of friends",
  "type": "number"
}

Error response: not_a_number

select, radio and checkbox

Check if the response belongs to the possible values.

Usage

Definition JSon

{
  "name": "position",
  "type": "radio",
  "values": {
    "left": "value_left",
    "right": "value_right",
    "up": "value_up",
    "down": "value_down"
  }
}

If the value is left, right, up or down, it will be validated.

Error response: not_in_the_select_list, not_in_the_radio_list or not_in_the_checkbox_list

color

Check if the response is a valid HTML 5, hexadecimal color, like '#FF0033'.

Usage

Definition JSon

{
  "name": "themeColor",
  "type": "color"
}

Error response: not_a_color

date

Check if the response is a valid HTML 5, 'YYYY-MM-DD' date.

Usage

Definition JSon

{
  "name": "birthDay",
  "type": "date"
}

Error response: not_a_date

time

Check if the response is a valid HTML 5, 'H:mm' time.

Usage

Definition JSon

{
  "name": "arrival",
  "type": "time"
}

Error response: not_a_time

email

Check if the response is a valid email.

Usage

Definition JSon

{
  "name": "userEmail",
  "type": "email"
}

Error response: not_an_email

url

Check if the response is a valid url.

Usage

Definition JSon

{
  "name": "siteUrl",
  "type": "url"
}

Error response: not_an_url

tel

Check if the response is a phone. It's a generic phone validator that accepts international (with plus sign) and local phones (with or without paranthesis).

Usage

Definition JSon

{
  "name": "cellNumber",
  "type": "tel"
}

Error response: not_a_tel

Value validation

These four validations are for numbers to define it's min and max values.

lessThan and moreThan

These validations require the data to be a number (int or float) that is 'less than' and/or 'more than' the chosen values.

Usage

Definition JSon

{
  "name": "zoom",
  "more-than": 0.1,
  "less-than": 10.4
}

Error response: must_be_more_than: 0.1 or must_be_less_than: 10.4

lessThanOrEqual and moreThanOrEqual

These validations require the data to be a number (int or float) that is 'less than or equal' and/or 'more than or equal' the chosen values.

Usage

Definition JSon

{
  "name": "precision",
  "more-than-or-equal": 3,
  "less-than-or-equal": 8.6
}

Error response: must_be_more_than_or_equal: 3 or must_be_less_than_or_equal: 8.6

Length validation

These two validations are for generic strings to define it's min and max length.

minLength and maxLength

These validations require the data to be a string that has a minimux or maximum number of characters.

Usage

Definition JSon

{
  "name": "pinCode",
  "min-length": 4,
  "max-length": 8
}

Error response: must_be_min: 4 or must_be_max: 8

Required validation

This validation checks if the required value is available in the POST JSon.

required

These validations can be used in any kind of field

Usage

Definition JSon

{
  "name": "name",
  "required": true
}

Error response: field_is_required

String validations

These are validations for specific kinds of strings, that must help creating validations tom many field types.

alpha

This validation require the data to be only letters and accented letters (A-z and À-ÿ).

Usage

Definition JSon

{
  "name": "firstName",
  "type": "text",
  "string": "alpha"
}

Error response: not_an_alpha

alphaSpace

This validation require the data to be only letters, accented letters and spaces (A-z, À-ÿ and ' ').

Usage

Definition JSon

{
  "name": "fullName",
  "type": "text",
  "string": "alphaSpace"
}

Error response: not_an_alpha_space

alphaDash

This validation require the data to be only letters, accented letters and dashes (A-z, À-ÿ, - and _).

Usage

Definition JSon

{
  "name": "serialNumber",
  "type": "text",
  "string": "alphaDash"
}

Error response: not_an_alpha_dash

alphaSymbol

This validation require the data to be letters, accented letters and many symbols:

| A-z | À-ÿ | - | _ | . | , | ! | @ | |-------|-------|-------|-------|-------|-------|-------|-------| | # | $ | % | ^ | { | } | [ | ] | | & | * | ( | ) | < | > | + | = | | ? | : | ; | " " |

Usage

Definition JSon

{
  "name": "address",
  "type": "text",
  "string": "alphaSymbol"
}

Error response: not_an_alpha_symbol

alphaNumeric

This validation require the data to be letters, accented letters and numbers (A-z, À-ÿ and 0-9).

Usage

Definition JSon

{
  "name": "code",
  "type": "text",
  "string": "alphaNumeric"
}

Error response: not_an_alpha_numeric

alphaNumericSpace

This validation require the data to be letters, accented letters, numbers and space (A-z, À-ÿ, 0-9 and ' ').

Usage

Definition JSon

{
  "name": "keyCode",
  "type": "text",
  "string": "alphaNumericSpace"
}

Error response: not_an_alpha_numeric_space

alphaNumericDash

This validation require the data to be letters, accented letters, numbers and space (A-z, À-ÿ, 0-9, - and _).

Usage

Definition JSon

{
  "name": "serialNumber",
  "type": "text",
  "string": "alphaNumericDash"
}

Error response: not_an_alpha_numeric_dash

alphaNumericSymbol

This validation require the data to be letters, accented letters, numbers and many symbols:

| A-z | À-ÿ | 0-9 | " " | . | , | ! | @ | |-------|-------|-------|-------|-------|-------|-------|-------| | # | $ | % | ^ | & | * | ( | ) | | { | } | [ | ] | < | > | + | = | | ? | : | ; | - | _ | | | |

Usage

Definition JSon

{
  "name": "serialNumber",
  "type": "text",
  "string": "alphaNumericSymbol"
}

Error response: not_an_alpha_numeric_symbol

numeric

This validation require the data to be numbers (0-9).

Usage

Definition JSon

{
  "name": "products",
  "type": "text",
  "string": "numeric"
}

Error response: not_a_numeric

numericFloat

This validation require the data to be numbers, -, . and , (0-9.,-).

Usage

Definition JSon

{
  "name": "price",
  "type": "text",
  "string": "numericFloat"
}

Error response: not_a_numeric_float

numericDash

This validation require the data to be numbers, -, _, . and , (0-9.,-_).

Usage

Definition JSon

{
  "name": "balance",
  "type": "text",
  "string": "numericDash"
}

Error response: not_a_numeric_dash

numericSymbol

This validation require the data to be numbers and many symbols:

| 0-9 | " " | 0-9 | _ | . | , | ! | @ | |-------|-------|-------|-------|-------|-------|-------|-------| | # | $ | % | ^ | & | * | ( | ) | | { | } | [ | ] | < | > | + | = | | ? | : | ; | - |

Usage

Definition JSon

{
  "name": "code",
  "type": "text",
  "string": "numericSymbol"
}

Error response: not_a_numeric_symbol

Pre requisites

You must have a definition json like this:

{
  "fields": [
    {
      "name": "name",
      "type": "text",
      "min-length": 3
    },
    {
      "name": "password",
      "type": "number",
      "max-length": 4,
      "min-length": 4,
      "required": true
    },
    {
      "name": "description",
      "type": "text-area"
    },
    {
      "name": "quantity",
      "type": "select",
      "values": {
        "0": "value_3",
        "1": "value_6",
        "2": "value_9"
      }
    },
    {
      "name": "themeColor",
      "type": "color"
    },
    {
      "name": "birthDay",
      "type": "date"
    },
    {
      "name": "arrival",
      "type": "time"
    }
  ]
}

This definition must have all the fields and the validations.

And you must have the data to be validated:

{
  "password": "1232",
  "description": "A great fidelity card",
  "quantity": "0"
}

Usage

First, you need to configure the language (m-validate uses i18n-2) passing a config object and the language you want your response.

i18n-2 will create the language files as they are needed, but you can set all translations using this base file.

var MValidate = require('../m-validate');
// The object is a i18n-2 config object. The second parameter is the locale
var validator = new MValidate({
  locales: [
    'en-US',
    'pt-BR'
  ],
  directory: './locales',
  extension: '.json'
},
 'pt-BR');

var fs = require('fs');

// Get the definition json here
var definitionPath = 'form.json';
var definitionContent = fs.readFileSync(definitionPath, 'utf8');
var definition = JSON.parse(definitionContent);
var definitionFields = definition.fields;

// Assuming the data comes from a post
var content = req.body;

// Response variable will have the result
var response = validator.validate(content, mobletDefinitionFields);

The validation response will be an object with two items: a boolean and an object.

The boolean is called "error" and will be true if any of the validations did not pass.

The object is called "result" and will have all the validated fields as an object. If any validation failed, this object will have the translated validation type and the result, for instance:

{
  error: true,
  response: {
    name: {},
    password: {
      required: 'field_is_required',
      'max-length': 'must_be_more_than_chars: 4'
    },
    description: {},
    quantity: {},
    position: {},
    color: {},
    themeColor: {
      type: 'not_a_color'
    },
    birthDay: {},
    arrival: {},
    email: {},
    siteaddress: {
      type: 'not_an_url'
    },
    phone: {
      type: 'not_a_tel'
    },
    cell: {}
  }
}

You can check the tests to see it in action.

License

The MIT License (MIT)

Copyright (c) 2016 Moblets

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.