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

simply_valid

v5.0.0

Published

A simple data driven validation utility library

Downloads

46

Readme

npm David David Travis Coverage Status

main-mid

A simple and lightweight validation system. Ships with prebuilt rules and accepts custom rules.

Documentation

Find individual documentation per function on the site: You can click here to go there

Content

Philosophy

The idea behind simply_valid was a ui free data driven validation system. It started as something I wanted at work over our current validation library and then grew into this. (You can see the inspiration of some of the validation types that lives currently in the functions)

I wanted this module to be fast, easy to use, and above all as plug and play as I could get it.

With the schema system in place for the module you can easily validate complex objects such as form data put into an object, applying an array of rules or even just a single rule to your data value. Making it easy to create validation instances for different forms or multiple data styles.

Parameters

  • schema - Object: An object of rules to overwrite the default rules
  • data - String|Array|Object: Data is the value sent in with the 2nd call made to simplyValid (curried call)

Usage

Using Standard JS

import { validate } from 'simply_valid'

validate(schema, data)

// Or
const valid = validate(schema)

valid(data)

Using commonjs

const { validate } = require('simply_valid')

validate(schema, data)

// Or
const valid = validate(schema)

valid(data)

Using a CDN

<!-- It is recommended to replace @latest with a strict version number -->
<script src="https://cdn.jsdelivr.net/npm/simply_valid@latest/dist/simply-valid.min.js"></script>
<script>
  validate(schema, data)

  // Or
  const valid = validate(schema)

  valid(data)
</script>

In the browser

<script src="path/to/dist/simplyValid.min.js"></script>
<script>
  validate(schema, data)

  // Or
  const valid = validate(schema)

  valid(data)
</script>

Schema

Simply_Valid supports a schema system, the schema should be either an Array or Object type. Even when using just one function

Note If you are validating an object the schema MUST also be an object

Examples:

import { validate, hasValue, isNumber, isPositive, hasLetters, hasNumbers, isZip, noNumbers } from 'simply_valid'

// Single/Primitive data value
validate([isNumber], 2) // => { isValid: true }
validate([isNumber, isPositive], 3) // => { isValid: true }

// Array of Data
validate([isNumber], [1, 2, 3]) // => { isValid: true }
validate([isNumber, isPositive], [1, 2, 3]) // => { isValid: true }
validate([isNumber, isPositive], [1, 2, -3]) // => { isValid: false, rule: 'isPositive', data: [1, 2, -3] }

// Object of Data
validate({
  zip: isZip,
  address: [hasLetters, hasNumbers]
}, {
  zip: 11234,
  address: '123 test dr'
}) // => { isValid: true }

// Object with nested data
validate({
  zip: isZip,
  address: validate({ num: isNumber, name: [hasLetters, noNumbers] })
}, {
  zip: 11234,
  address: {
    num: 123,
    name: 'test dr'
  }
}) // => { isValid: true }

Custom Rules

Simply_Valid also supports the use of custom rules

  • Custom rules returns will be treated on a true/false basis so try to have them return a boolean
  • If you want a multi param rules name to show up in a failure make sure you name the inner function
  • The inner function should be the same name but with an underscore at the start (it will be formatted out)
import { validate } from 'simply_valid'

const isEven = val => val % 2 === 0
// For multi param functions you need to use the function keyword
// If you want the name to show up in failures, it also relies on partial execution
const notMin = function notMin (min) {
  // The inner function should be named the same but with a _ in front of it
  // (This gets removed when you get the rule)
  // This ensures you get an accurate rule back in your object
  return function _notMin (val) {
    return val !== min
  }
}
const schema = {
  foo: isEven,
  bar: [isEven, notMin(4)]
}

validate(schema, { foo: 4, bar: 6 }) // => { isValid: true }
validate(schema, { foo:4, bar: 4 }) // => { isValid: false, rule: 'notMin', data: 4 }
validate(schema, { foo:4, bar: 5 }) // => { isValid: false, rule: 'isEven', data: 5 }

Return

Simply_Valid will return upon the first failing rule it finds, with information about the failure.

// Passing Validation
{ isValid: true }

// Failing returns will look like this
{
  isValid: false,
  prop: 'propName',
  rule: 'functionName'
  data: 'cool'
}