0.2.1 • Published 7 years ago

bjv v0.2.1

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

BJV (the Billionth Javascript Validation Utility)

BJV Example

What is it?

A simple API to help you validate your variables.

Why?

Because there isn't enough javascript validation libraries available.

...jk, I made this for the fun of it.

Installation

npm install bjv

Introduction

Here's some notes on how bjv works:

  • BJV's type functions, when called, return validator functions. Validator functions are asynchronous (i.e. they return a promise), and are used to validate and parse input data.
  • Each validator function has a set of option methods defined on the function itself. The option methods available vary from type to type.
  • Each option method, when called, returns a new validator function. This helps when you want to reuse validation logic.
  • BJV exports and uses the following error classes:
    • SchemaError: Thrown whenever an option method is used incorrectly.
    • ValidationError: Thrown whenever a validator is called with invalid data.

Type functions and option methods

any

any() validators don't typecheck the input. They provide the following option methods, each of which is available on all validator functions (regardless of type):

  • named(str): Sets the validator name. Useful when not using object validators, which automatically assign validator names. Names are displayed in validation error messages, but aren't used otherwise.
    Default: '<any>', '<array>', '<boolean>', '<number>', '<object>', or '<string>'
  • fallback(anything): Sets the validator fallback to anything. The validator fallback is returned when the data being validated is undefined. If the validator fallback is an async function, it's called on-demand to get the actual fallback value. If the fallback is undefined, there is no fallback, and validation isn't stopped early. Fallback values don't undergo validation or parsing.
    Default: undefined
  • optional(bool = true): Sets whether or not undefined is considered valid input data.
    Default: false
  • nullable(bool = true): Sets whether or not null is considered valid input data.
    Default: false
  • custom(func): Adds a custom validator to the custom validation chain. If func is null, the chain is reset to empty. Custom validators accept input data as an argument, and return true if the data is valid, false if not. They are run in the order they are defined in the schema.
    Default: empty ([])
  • parse(func): Adds a parsing function to the parsing chain. If func is null, the chain is reset to empty. Parsing functions accept input data as an argument, and return the new, parsed input. They are run in the order they are defined in the schema.
    Default: empty ([])

array

array() validators check that the input is an array. They provide the following additional option methods:

  • minLength(num): Sets the minimum length of a valid array to num.
    Default: 0
  • maxLength(num): Sets the maximum length of a valid array to num.
    Default: Number.POSITIVE_INFINITY
  • type(func): Sets the type validator function used to validate each item in the array. If func is null, the type validator function is unset. An array validator without a type validator will only check that input data is an array.
    Default: null

boolean

boolean() validators check that the input is a boolean value. They don't provide any additional option methods.

number

number() validators check that the input is a number value. They provide the following additional option methods:

  • min(num): Sets the minimum valid value to num.
    Default: Number.NEGATIVE_INFINITY
  • max(num): Sets the maximum valid value to num.
    Default: Number.POSITIVE_INFINITY
  • integer(bool = true): Sets whether or not valid values must be integers.
    Default: false

object

object() validators check that the input is an object. They provide the following additional option method:

  • keys(obj): Sets the schema for the object validator to obj. The schema is an object of key-validator function pairs. If obj is null, the schema is unset. An object validator without a schema will only check that input data is an object.
    Default: null

string

string() validators check that the input is a string value. They provide the following additional option methods:

  • minLength(num): Sets the minimum valid string length to num.
    Default: 0
  • maxLength(num): Sets the maximum valid string length to num.
    Default: Number.POSITIVE_INFINITY
  • choices(arr): Sets the valid string choices to those in arr. Input strings that aren't in the choices array are considered invalid. If arr is null, the choices array is unset.
    Default: null
0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago