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

@patterninc/laravel_js_validator

v1.0.18

Published

Validate values against Laravel validation rules

Downloads

534

Readme

Laravel style validators in JS

This package implements laravel styled validation rules in javascript. It is a standalone package and does not require any other package or framework to work. We can use this with ES6 and commonjs type of modules. Refer usage section for more details on the usage.

Installation

Add follwing to your package.json

"@patterninc/laravel_js_validator": "^1.0.1"

Usage

  • commonjs
const laravel = require('@patterninc/laravel_js_validator')
laravel.Validator.validate(data, rules)
  • es6
import { Validator } from '@patterninc/laravel_js_validator'
  • Example

import { Validator } from '@patterninc/laravel_js_validator'

{
    "data": {
      "a1": "test",
      "a2": null,
      "a3": "",
      "a4": [],
      "a5": 10,
      "a6": ["a", "b", "c"],
      "a7": [1,2,3,4,5,6,7,8,9,10],
      "a8": "*&^R$",
      "a9": "test123",
      "a10": "abc12312",
      "a11": "[email protected]",
      "a12": "abc",
      "a13": "regex123"
    },
    "rules": {
      "a1": "required",
      "a2": "required",
      "a3": "required",
      "a4": "required",
      "a5": "required|numeric|max:10|min:4",
      "a6": "required|array|in:a,b,c",
      "a7": "required|array|between:1,10",
      "a8": "required|alpha",
      "a9": "required|alpha_num",
      "a10": "required|alpha",
      "a11": "required|email",
      "a12": "required|email",
      "a13": "required|regex:/^regex[0-9]+$/"
    }
}

const result = Validator.validate(data, rules)
console.log(result)
  • Result

{
    "success": false,
    "errors": {
        "a2": [
            "The a2 field is required."
        ],
        "a3": [
            "The a3 field is required."
        ],
        "a4": [
            "The a4 field is required."
        ],
        "a8": [
            "The a8 may only contain letters."
        ],
        "a10": [
            "The a10 may only contain letters."
        ],
        "a12": [
            "The a12 must be a valid email address."
        ]
    }
}

Rules Implemented

{
  after: 'The :attr must be a date after :date.',
  alpha: 'The :attr may only contain letters.',
  alpha_dash: 'The :attr may only contain letters, numbers, and dashes.',
  alpha_num: 'The :attr may only contain letters and numbers.',
  array: 'The :attr must be an array.',
  before: 'The :attr must be a date before :date.',
  between: {
    numeric: 'The :attr must be between :min and :max.',
    string: 'The :attr must be between :min and :max character(s).',
    array: 'The :attr must have between :min and :max item(s).',
  },
  boolean: 'The :attr field must be true or false.',
  date: 'The :attr is not a valid date.',
  date_format: 'The :attr does not match the format :format.',
  different: 'The :attr and :other must be different.',
  email: 'The :attr must be a valid email address.',
  in: 'The selected :attr is invalid.',
  integer: 'The :attr must be an integer.',
  max: {
    numeric: 'The :attr may not be greater than :max.',
    string: 'The :attr may not be greater than :max character(s).',
    array: 'The :attr may not have more than :max item(s).',
  },
  min: {
    numeric: 'The :attr must be at least :min.',
    string: 'The :attr must be at least :min character(s).',
    array: 'The :attr must have at least :min item(s).',
  },
  not_in: 'The selected :attr is invalid.',
  numeric: 'The :attr must be a number.',
  regex: 'The :attr format is invalid.',
  required: 'The :attr field is required.',
  same: 'The :attr and :other must match.',
  size: {
    numeric: 'The :attr must be :size.',
    string: 'The :attr must be :size character(s).',
    array: 'The :attr must contain :size item(s).',
  },
  string: 'The :attr must be a string.',
  url: 'The :attr format is invalid.',
}