npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kserver-check

v1.0.4

Published

A common data validator use custom rules

Downloads

2

Readme

kserver-check

a powerful data validator use a set of simple template rule syntax

Installation

  • npm
npm install kserver-check --save-dev
  • or use yarn
yarn add kserver-check --dev

Usage:

check(templateRule, data, options?) : CheckResult

Rules

Object

  • key start with '*' indicate this prop is required

  • value type indicate this prop value type

  • value equal null means this prop value can be any type

Note: if value is function with parameters, it's a custom validator, and the function receive data value and options arguments and return boolean or CheckResult

// example
const templateRule = {
    '*name': 'name'
    'address': 'here address',
    'number': 10,
    'external': null,
    
    'hotel': { '*name': 'hotel name' },

    'callback'(){}
    'days'(days, options){ return days<10&&days>=3 },

    'list': [{}]
}
/* 
this Rule means data 
    require 'name' prop
    'name'/'address' expect String value
    'number' expect Number value (if strict set true, see Options)
    'external' can be all Types
    if exists 'hotel', 'hotel.name' is required
    'callback' expect a function
    'days' validate by custom function
*/

Array

  • first item: item rules
  • second item: options
const templateRule = [
    { '*name': 'name' },
    { min: 3, max: 10 }
]

/*
this Rule means data
    is Array
    Array item require 'name' prop
    Array need at least 3 item and at most 10 item;
*/

CheckResult

  • valid: indicate data is valid
  • data: if valid is true, data give you a filtered and converted result data (see Options)
  • error: if valid is false, error show you which prop/field check valid and error type
// example
{ valid: false, error: { 'name': 1, 'list.0.name': 2 } }

Options

|field|description|default| |:---:|:---|:---:| |requirePrefix|required field prefix|'*'| |filter|if set true, the data in CheckResult will filter all non-defined props in template Rule|false| |strict|if set false, the validator will try convert String to Number(or Number to String) before compare value type, and output the converted value in CheckResult |false

Error Code

|code|description| |:---:|:---| |1|required field |2|incorrect type |3|array out of range |4|custom check valid |5|null value

Example:

// or use es6
// import check from 'kserver-check';
const check = require('kserver-check');

check({'*name':'name'}, {}) // result: {valid:false, error:{ 'name':1 } }
check([{'*name':'name'}], [{name:'wang'}, {}]) // result: {valid:false, error:{'1.name':1}}

License

MIT