5.0.0 • Published 5 years ago

simply_valid v5.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

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'
}
5.0.0

5 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.2.3

6 years ago

3.2.2

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago