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

validator-pyrobyte

v1.0.1

Published

Validator on VanilaJS

Downloads

89

Readme

validator-pyrobyte

Installation

npm install --save validator-pyrobyte

Example


import {Validator} from 'validator-pyrobyte';

let rules = {
  text: {
    required: true,
    minLength: 10,
  },
  phone: {
    phone: true,
  },
};

let validator = new Validator(rules);
let validationText = validator.check('text', valueText); // return IResultValidation
let validationAll = validator.checkAll({
  text: 'text',
  phone: '+79998887766',
}); // return IResultValidation

Constants

type RULES_VALIDATION:

type TRuleValidation<Data = any> = {
  handler(v: Data, param?: number): boolean,
  errorText(param: number): string,
};

interface IRulesValidation {
  [nameRule]: TRuleValidation,
}
import {RULES_VALIDATION} from 'validator-pyrobyte';

let rules: IRulesValidation = RULES_VALIDATION;

Methods

Result methods validation check and checkAll:


interface IResultValidation {
  passed: boolean,
  errors: string[],
}

Check


type check<Data> = (nameRule: keyof Rules, data: Data) => IResultValidation;

CheckAll

import {IRules} from "./index";

type ValidatorData<Rules> = {
  [Property in keyof Rules]: string;
};

type  checkAll<Data> = (data: ValidatorData<Rules>) => IResultAllValidation<Rules>;

ChangeRule

type changeRule<Data> = (
  nameRule: string,
  handler: ValidatorHandler<Data>,
  errorText: string
) => void;
import {Validator} from 'validator-pyrobyte';

let validator = new Validator({
  text: {
    isText: true,
  },
});
// new rule
validator.changeRule('new', (v) => !!v, 'some error text');

// change default rule
validator.changeRule('isText', (v) => /^(\d/.test(v), 'new error text');

Default handlers RULES_VALIDATION:



/**
 * Validation required
 *
 * @param value {string}
 * @return {boolean}
 */
function required(value) {
  return value || value === '0';
}

/**
 *
 * @param value {string}
 * @param min {number}
 * @return {boolean}
 */
function minLength(value, min) {
  return value.length >= min;
}

/**
 *
 * @param value {string}
 * @param max {number}
 * @return {boolean}
 */
function maxLength(value, max) {
  return value.length <= max;
}

/**
 *
 * @param value {string}
 * @return {boolean}
 */
function phone(value) {
  // delete mask symbols
  let numbers = value.replace(/[^\d]/g, '');
  return /^[\d]{10}$/.test(numbers);
}

/**
 *
 * @param value {string}
 * @return {boolean}
 */
function email(value) {
  return /^.+@.+$/.test(value);
}

/**
 *
 * @param value {string}
 * @return {boolean}
 */
function password(value) {
  return /(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{6,}/g.test(value);
}

/**
 *
 * @param value {string}
 * @return {boolean}
 */
function isNumber(value) {
  return /^[0-9]+$/.test(value);
}

/**
 *
 * @param value {string}
 * @return {boolean}
 */
function isText(value) {
  if (value.length > 2) {
    return /^[a-zA-Zа-яА-ЯёЁ'][a-zA-Zа-яА-ЯёЁ\-' ]+[a-zA-Zа-яА-ЯёЁ']?$/.test(
      value,
    );
  } else {
    return /^[a-zA-Zа-яА-ЯёЁ']*$/.test(value);
  }
}

/**
 *
 * @param value {string}
 * @return {boolean}
 */
function isSymbols(value) {
  return /^([a-zA-Zа-яА-ЯёЁ0-9!\-.?_ ]+)$/gm.test(value);
}