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

@ckpack/parameter

v2.9.0

Published

JSON validator for Node.js and browser

Downloads

9

Readme

Parameter

JSON validator for Node.js and browser by using simple configuration rule

Usage

API

Class

Parameter Class

  • constructor([options]) - New Class Parameter instance
    • options.isUseDefault - If use default value, default to true
    • options.isRemoveAdditional - Delete is not defined in the rule as an attribute, default to false
    • options.isCoerceTypes - Change data type of data to match type keyword, default to false
    • options.emptyValues - The contained value will be treated as a empty value, default to [null, undefined, NaN, '']
  • schema(rule, [options]) - Returns a validate with fixed parameters
  • validate(rule, value, [options]) - Validate the value conforms to rule. return an array of errors if break rule
    • rule - The rule of the verified json
    • value - JSON to be verified
    • options - Override the definition of constructor([options])
  • addRule(type, checkFunction) - Add custom rules.
    • type - Rule type, required and must be string type.
    • checkFunction(rule, value) - Custom check rule function.
import { Parameter } from '@ckpack/parameter';

const parameter = new Parameter();

// check rule
const rule = {
  isAdmin: {
    type: 'boolean',
  },
  age: {
    type: 'int',
    min: 0,
  },
  role: {
    type: 'enum',
    enum: ['am', 'pm'],
  }
  ids: {
    type: 'array',
    itemType: 'int',
    itemRule: {
      min: 1
    }
  }
}

// check value
const data = {
  isAdmin: true,
  age: 18,
  role: 'am',
  ids: [1, 2, 3]
};

const errors = parameter.validate(rule, data);

Function

setLocale 设置错误信息语言

设置默认语言为中文

import { zhLocale, setLocale, Parameter } from '@ckpack/parameter';

setLocale(zhLocale);

const parameter = new Parameter();

// parameter.validate(rule, data);

RULE

common rules

  • type: The rule type
  • message: Custom error message
  • isRequired: If check the empty value, default to true
  • default: Is isUseDefault is true, the empty value will be set default value, default to undefined

int

If type is int, There are the following options rules

  • max - The maximum of the value, value must <= max
  • min - The minimum of the value, value must >= min
{
  score: 'int',
}
// or
{
  score: {
    type: 'int',
    min: 0,
    max: 200,
  }
}

number

If type is number, There are the following options rules

  • max - The maximum of the value, value must <= max
  • min - The minimum of the value, value must >= min

example

{
  score: 'number',
}
// or
{
  score: {
    type: 'number',
    min: 0,
    max: 100,
  }
}

string

If type is string, There are the following options rules

  • regexp - A RegExp to check string's format
  • max - The maximum length of the string
  • min - The minimum length of the string

example

{
  username: 'string',
}
// or
{
  username: /\S{4,20}/,
}
// or
{
  username: {
    type: 'string',
    regexp: /\S{4,20}/
  }
}

boolean

If type is boolean

example

{
  isAll: 'boolean',
}
// or
{
  isAll: {
    type: 'boolean',
  }
}

array

If type is array, There are the following options rules

  • itemType - The type of every item in this array
  • itemRule - The rule of every item in this Rule
  • itemChecker- The checker of every item, in this case you can omit itemType and itemRule
  • max - The maximun length of the array
  • min - The minimum length of the array

example

{
  ids: {
    itemType: 'int',
    itemRule: {
      min: 1,
      max: 1000,
    },
    min: 0,
    max: 100,
  }
}

enum

If type is enum, There are the following rules

  • enum- An array of data, value must be one on them

example

{
  sex: ['man', 'woman']
}
// or
{
  sex: {
    type: 'enum'
    enum: ['man', 'woman']
  }
}

object

If type is object, There are the following rules

  • rule- An object that validate the properties ot the object

example

{
  people: {
    type: 'object',
    rule: {
      name: 'string',
      age: {
        isRequired: false,
        type: 'int',
        min: 1,
        max: 200
      }
    }
  }
}

custom rule

definition custom rule, example

// custom check
parameter.addRule('even', (rule, value) => {
  return value % 2 === 0 ? null : `${value} is not even`;
});

use the custom rule

{
  someNumber: 'even'
}
//or
{
  someNumber: {
    type: "even",
  }
}

you alse can add options rules

import { Parameter } from '@ckpack/parameter';
const parameter = new Parameter();
parameter.addRule('times', (rule, value) => {
  const { times } = rule;
  return value % times === 0 ? null : `not an integer multiple of ${times}`;
});

// rule
{
  someNumber: {
    type: "times",
    times: 3,
  }
}

complex example