2.0.4 • Published 5 years ago

validazz v2.0.4

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

validazz

Generated with nod NPM version Build Status Coverage Status

Magical, Flexible and Extendible Javascript Validation.

Install

npm:

npm i validazz

Yarn:

yarn add validazz

Usage

import Validator, { rules } from 'validazz'

const mySuperCoolValidator = Validator.factory()

// Let's add some rules
mySuperCoolValidator.addRule(rules.isRequired)
mySuperCoolValidator.addRules([rules.minLength(2), rules.maxLength(8)])
mySuperCoolValidator.addRule(rules.isString)

// How about a custom rule?
const customRule = {
  runWithValue: value => {
    if (value !== '🤪') return false
    return true
  },
  message: 'Houston, we got a problem',
}
mySuperCoolValidator.addRule(customRule)

// Okay let's start validating
const { success, failed } = mySuperCoolValidator.runWithValue('hello')
if (success) {
  console.log('Wow, this was validated just like that')
} else {
  const { message } = failed
  console.log(`Okay so here's the error message: ${failed}`)
}

For a list of all the included rules, be sure to check the RULES.md file

API

validazz

Generated with nod NPM version Build Status Coverage Status

Magical, Flexible and Extendible Javascript Validation.

Install

npm:

npm i validazz

Yarn:

yarn add validazz

Usage

import Validator, { rules } from 'validazz'

const mySuperCoolValidator = Validator.factory()

// Let's add some rules
mySuperCoolValidator.addRule(rules.isRequired)
mySuperCoolValidator.addRules([rules.minLength(2), rules.maxLength(8)])
mySuperCoolValidator.addRule(rules.isString)

// How about a custom rule?
const customRule = {
  runWithValue: value => {
    if (value !== '🤪') return false
    return true
  },
  message: 'Houston, we got a problem',
}
mySuperCoolValidator.addRule(customRule)

// Okay let's start validating
const { success, failed } = mySuperCoolValidator.runWithValue('hello')
if (success) {
  console.log('Wow, this was validated just like that')
} else {
  const { message } = failed
  console.log(`Okay so here's the error message: ${failed}`)
}

For a list of all the included rules, be sure to check the RULES.md file

API

Table of Contents

Validator

Validation Factory, where all the validation magic happens

Type: Validator

Parameters

addRule

Add a rule to the factory

Parameters

Returns Validator Validator instance

addRules

Add a rules to the factory

Parameters

Returns Validator Validator instance

runWithValue

  • See: validate

Run the factory and validate!

Parameters
  • value string The string to be validated

Returns ValidatorResult The validation outcome

Meta

  • deprecated: Use validate(value: string) instead. Depricated since v1.1

validate

Validates a string

Parameters
  • value String The string to be validated
Examples
const { success, failed } = Validator.factory(rules.isRequired).validate(
  'hello'
)

Returns ValidatorResult The validation outcome

factory

Create a new validation factory

Parameters
Examples
const validator = Validator.factory([])

Returns Validator Validator instance

runWithValue

The validation function for this rule. It takes the a string/integer value and returns a boolean.

Type: Function

Parameters

  • value string Value of array element

Examples

const rule = {
  runWithValue(value) {
    return value != null
  },
}

Returns Boolean If it returns true, the field is valid.

ValidationRule

A validation rule

Type: Object

Properties

  • message string A custom error message for this validation rule
  • runWithValue runWithValue Validation callback

Examples

// Basic Example
const validationRule = {
  message: 'This field is required',
  runWithValue(value) {
    return value != null
  },
}

// Example with parameters
const minimum = min => ({
  message: `Amount must be greater than ${min}`,
  runWithValue(value) {
    const value = Number(value)
    return value > min
  },
})

ValidatorResult

The validation result

Type: Object

Properties

  • success Boolean The outcome of the validation
  • failed ValidationRule An optional value. Returns the rule that failed to validate

Examples

const { success, failed } = Validator.factory(rules.isRequired).validate(
  'hello'
)
if (success) {
  alert('validated')
} else {
  const { message } = failed
  alert(`Failed: ${message}`)
}

License

MIT © Jesse Onolememen

Table of Contents

License

MIT © Jesse Onolememen

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.0

5 years ago

0.0.1

5 years ago