Détail du package

simply_valid

dhershman162MIT5.0.0

A simple data driven validation utility library

validation, simple, data, simply

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) ```js 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.

```js
// Passing Validation
{ isValid: true }

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

changelog

Change Log

v5.0.0

BREAKING CHANGES

  • story is no longer a value included with the return, it is just an object.
    • simply_valid will return on the first failure with the information about that failure
  • Importing/requiring the module is a bit different please see the README for info
    • Schema will No longer take string values, you must import and pass the functions you want to use.
      • Why? This offers a cleaner and more direct experience, less the library has to worry about so less overhead!
      • This also opens the door for you to be able to pass in your own functionality!
  • Auto recursion dropped for nested complex data

    • Simply recall validate within an object to support nested objects
    • example: ```js const data = { a: 1, b: { c: 3 } } const schema = { a: isNumber, b: validate({ c: isNumber }) }

    validate({ schema }, data) ```

  • Changed how isBetween params work instead of an object it expects 2 numbers now
    • Example: isBetween(min, max, value)

Removed

  • isVisaCard: Should lean more towards relying on proper CC libs
  • isVisPanCard: Should lean more towards relying on proper CC libs
  • isAmexCard: Should lean more towards relying on proper CC libs
  • isMasterCard: Should lean more towards relying on proper CC libs
  • isDiscoverCard: Should lean more towards relying on proper CC libs
  • creditCard: Since the above were also removed
  • meetsCVN: Lean more towards proper CC validation
  • meetsCVNAmex: Lean more towards proper CC validation
  • cvn: Since the two cvn methods were removed
  • isNotToShort: Use isAboveMin with the length of a value
  • isNotToLong: Use isBelowMax with the length of a value
  • isCorrectLength: Use isBetween with the length of a value
  • meetsMinMax: Use isBetween

Improved

  • Converted more functionality to use Kyanite
  • Re wrote how the app handles all validation for better performance

v4.0.2

New

  • Added Kyanite library to help with utility functionality

Improved

  • Cleaned up code a bit using the library

v4.0.1

New

  • Re vamped how the documentation scripts work to make keeping docs up-to-date much simpler
    • Now if I need to push an update I don't need to worry about the documentation falling behind!

v4.0.0

BREAKING CHANGES

  • isVin, isEmail, and meetsPassReq no longer accept a regex overwrite the built in ones are optimized and specialized for each of these fields
  • Regex options removed from available options lists to send in for the same as above reasons
  • hasValue (As stated in the fixed section) will no longer consider the number or sting 0 as a falsey value
  • Removal of long deprecated isAmericanExpressCard in favor of isAmexCard
  • Marked v1.0.0 - v2.2.0 as deprecated
  • You now need to be more specific about nested objects in the schema if they're nested via your data

New

  • Extreme Architecture cleanup, flattened all of the file systems
  • Improved Flow/Optimizations
  • Converted from eslint to standardjs
  • Added maxLen and minLen to options in the object
  • Added isNotTooShort which verifies if the value is longer or equal to minLen
  • Added isNotTooLong which verifies if the value is shorter or equal to maxLen
  • Added isCorrectLength which verifies if the value is between or equal to maxLen or minLen
  • There is now a non compressed version of the built module if you want to use it in a dev environment located with the compressed version in dist/simply-valid.js
  • Converted back to tape from ava

Updated

  • Updated documentation scripts to be faster and have a little more info
  • Slight documentation cleanup
  • The reponse of the main validation functionality will be much more consistent { isValid: Boolean, story: Array }

Fixed

  • hasValue Rule will no longer treat the number 0 as a falsy value
  • Some documentation pieces had copy/paste errors that needed addressed
  • Nested objects not being validated as expected
  • Methods that could break on an undefined will no longer bomb out
  • Methods isAboveMin and isBelowMax edge case fixed if you passed 0 for the min or max property when sending an object