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

validolphin

v1.2.2

Published

A 0 dependencies system to avoid the tedious data validation

Downloads

11

Readme

Validolphin

Validolphin is a lightweight npm package that provides a zero-dependency system for avoiding tedious data validation. Usable both in FRONTEND and in BACKEND

Installation

You can install Validolphin using npm. Run the following command:

npm install validolphin

Alternatively, you can import Validolphin using this HTML tag

<script type="application/javascript" src="https://cdn.jsdelivr.net/gh//leonardocrociani/Validolphin/Validolphin-Frontend.js"></script>

Usage

Import the package main class

const Validolphin = require('validolphin');

Create a configuration variable

const config = {
  unexpectedKeyError : <String>,      /* A CUSTOM ERROR WHEN AN OBECT TO VALIDATE HAS A KEY THAT IS NOT EXPECTED */
  missingKeyError : <String>,         /* A CUSTOM ERROR WHEN AN OBECT TO VALIDATE HAS A KEY THAT IS MISSING */
  errorsAtTime : <Number>,            /* THE NUMBER OF ERRORS TO DISPLAY IN THE RETURNED OBJECT, DEFAULT = 3 */
  validables : [                      /* AN ARRAY OF VALUES THAT DEFINES THE VALIDABLES OBJECTS */
    ...
    {
      name : <String>                 /* THE NAME OF THE FUNCTION TO CALL IF YOU WANT TO VALIDATE THE ASSOCIATED OBJECT */,
      schema : {                      /* THE VALIDATION SCHEMA OF THE OBJECT */
        field : {
          type: <Number | String | Array | Boolean | [Validolphin.types]>, 
          minVal : <Number>,            /* SET ONLY IF type == Number */  
          maxVal : <Number>,            /* SET ONLY IF type == Number */ 
          minLen : <Number>,            /* SET ONLY IF type == String */ 
          maxLen : <Number>,            /* SET ONLY IF type == String */ 
          elCount: <Number>,            /* SET ONLY IF type == Array */ 
          minEl  : <Number>,            /* SET ONLY IF type == Array */
          maxEl  : <Number>,            /* SET ONLY IF type == Array */
          homogen: <Boolean>,           /* SET ONLY IF type == Array */ 
          checkRule: <Function>,        /* A function to check al the element in the array. SET ONLY IF type == Array */
          matchDomain : <String>,       /* SET ONLY IF type == Validolphin.types.Email */
          notAllowedArr : <Array>,      /* The array of not allowed domains (check the example below) SET ONLY IF type == Validolphin.types.Email */
          nullable: <Boolean>           /* DEFAULT : false */
          err: <String>                 /* CUSTOM ERROR */ 
        },
        ...
    }
    ...
  ]
}

NOTE: here's a list of all Validolphin types. Every type use a regex for validation. All the regex are available in Validolphin.utils.*

Validolphin.types.Email;
Validolphin.types.Password;
Validolphin.types.Domain;
Validolphin.types.Phone;
Validolphin.types.Iban;
Validolphin.types.CreditCard;

Create the validation object

const validator = new Validolphin(config);

Now, you can call the functions with

validator.<name_of_the_validable_object>( object_to_validate )

Example

Here's an example of how to use Validolphin in your JavaScript code:

const Validolphin = require('validolphin');

// Create an instance of Validolphin by passing a configuration object
const config = {
  validables: [
    {
      name: 'user',
      schema: {
        username: { type: String, minLen: 4, maxLen: 20 },                           // LEGAL IF value is a string and has length >= 4 and <= 20
        email: { type: Validolphin.types.Email, notAllowedArr: ['legit.com'] },      // LEGAL IF value is an email and has domain != 'legit.com'. E.g. '[email protected]' -> NON LEGAL
        parentEmail : { type: Validolphin.types.Email, matchDomain : 'noxes.it' },   // LEGAL IF value is an email and has domain == 'noxes.it'. E.g. '[email protected]' -> NON LEGAL 
        age: { type: Number, minVal: 18, maxVal: 100 },                              // LEGAL IF value is a number and is >= 18 and <= 100
        medals : { type: Array, nullable: true, homogen: true, checkRule: function (el) { return ['GOLD', 'SILVER', 'BRONZE'].includes(el) } }                      // IF defined (nullable:true) the value is LEGAL IF has type Array and has all the element of the same type
      }
    }
  ]
};

const validator = new Validolphin(config);

// Validate an object using the defined validation function
const user = {
  username: 'john',
  email: '[email protected]',
  age: 25
};

const validationResult = validator.user(user);

if (validationResult.valid) {
  console.log('Validation successful!');
} else {
  console.error('Validation failed:', validationResult.error);
}

License

This project is licensed under the Creative Commons Attribution 4.0 International License (CC-BY-4.0).

Issues

If you encounter any issues or have any suggestions, please feel free to open an issue on the GitHub repository.

Enjoy using Validolphin for your data validation needs!