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

@eduinlight/input-validator

v1.6.5

Published

This library is a tool for forms validation and can be used for frontend and backend.

Downloads

72

Readme

Input Validator

npm type definitions npm bundle size browser support decorators supported

This library was written using the validator functions. The main reason why this library come up was for simplicity on input validations.

For more detailed documentation please visit https://github.com/eduinlight/input-validator/tree/main/docs

Install

npm install @eduinlight/input-validator

Browser support

<script src="https://unpkg.com/@eduinlight/input-validator/dist/index.umd.min.js"></script>

Usage

Defining a Schema

A Schema can be defined on the following form:

const schema = {
  attribute1: Rule[],
  attribute2: Rule[],
  .
  .
  .
}

Defining a list of Rules

A Rule can be any of the string-check functions of the validator library mentioned above: equals, contains, matches, email, url, ip, alpha, numeric, alphaNumeric, base64, hexadecimal, hexcolor, lowercase, uppercase, int, float, divisibleBy, required, minLength, maxLength, date, afterDate, beforeDate, in, creditCard, json, ascii, boolean, locale, mongooseId, currency.

A Rule can be of tree types, a string rule an object rule and a nested schema rule.

You can define a list of string rules as follow:

const rules = ['required', 'email'....]; // any rule with 0 params

An object rule has the following form:

const objRule = {
  rule: Rule, // any mentioned above rules
  params?: [param1, param2, param3], // the list of params needed by the rule
  message?: string
}

A nested schema rule has the following form:

const objRule = {
  rule: Schema, // here you can use a nested schema to test child objects
}

Full schema example

const schema = {
  name: ['required'],
  email: ['required', 'email'],
  age: ['required', 'int', {rule: 'min', params: [20]}],
  professional: [
    'required',
    {
      rule: {
        address: ["required"],
        // any check to nested professional object
      }
    }
  ]
}

Defining a schema using decorators

This library fully support the use of decorators. The declaration list is in reverse order of declaration. Please see the next example:

import {
  IsInt,
  IsRequired,
  MinValue,
  NestedSchema,
  ValidationSchema
} from '@eduinlight/input-validator/dist/decorators'

@ValidationSchema() //use this special decorator to generate a schema of your class
class Author {
  @IsRequired()
  name: string;

  @MinValue(10) //and second check if the value is bigger than 10
  @IsInt()      //check if is int first
  age: number;
}

@ValidationSchema()
class Book {
  @IsRequired()
  name: string;

  @NestedSchema(Author) //use this spcial decorator to generate a nested schema
  author: Author
}

Validate

First you need to import the validate function.

import {validate} from '@eduinlight/input-validator'

The validate function receive 3 parameters, the data, the schema, and options.

const form = {
  name: 'Eduin Garcia Cordero',
  email: '[email protected]',
  age: 33
}

const response = validate(form, schema, {exact: true})
// both expressions are equals
// const response = validate(form, schema)

if(response.valid) {
  // instructions for a valid form
} else {
  console.log(response.errors)
  // instructions for an invalid form
}

options is an object in the form:

{
  // default to true
  // verify if the form has no other attributes than the ones specified in the schema
  exact?: boolean,
  // default to false
  // insert into the response the value attribute that contains all validated data
  returnValues?: boolean,
}

Validate using a Decorated Schema

const form = {
  name: 'cuba',
  author: {
    name: 'eduin',
    age: 10
  }
}

const valid = validate(form, Book)

Errors

Errors are described recursively as follow:

[{ field: string, error: string | Errors }]

Tests

Tests are using jest, to run the tests use:

$ npm run test

Maintainers

License (MIT)

Copyright (c) 2018 Chris O'Hara <[email protected]>

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.