param-checkdeprecated

A js library for runtime parameter checking.

Usage no npm install needed!

<script type="module">
  import paramCheck from 'https://cdn.skypack.dev/param-check';
</script>

README

param-check

Build Status Standard - JavaScript Style Guide

Npm Info

运行时数据校验工具 | a javascript run-time data verification tool.

Install

npm install param-check --save

Usage

Basic usage

import check from 'param-check'

function fn (arg1, arg2) {
    check(arg1).isString()
    check(arg2, 'arg2').greaterThan(1).lessThan(2)
}

Import of specific check

import check from 'param-check/naked'
import isStringCheck from 'param-check/lib/checks/isString'

check.registerCheck(isStringCheck)

function fn (arg) {
    check(arg, 'arg').isString()
}

Custom check

import check from 'param-check'
import isNumber from 'lodash/isNumber'

function isEven (target, name) {
  return isNumber(target) && !(target % 2)
}

check.registerCheck('isEven', isEven)

function fn (arg) {
    check(arg, 'arg').isEven()
}

Custom linkable check

import check from 'param-check'
import isNumber from 'lodash/isNumber'

function isEven (target, name) {
  return isNumber(target) && !(target % 2)
}

function isEventNext (target) {
  return return target + 1
}

check.registerCheck('isEven', isEven, isEventNext)

function fn (arg) {
    check(arg, 'arg').isEven().isEven() // error occurred
}