New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

parameter

Package Overview
Dependencies
Maintainers
4
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parameter - npm Package Compare versions

Comparing version 2.4.0 to 3.0.0

6

History.md
3.0.0 / 2018-08-30
==================
**others**
* [[`f1dda26`](http://github.com/node-modules/parameter/commit/f1dda26ac3697c63a7e344228b7aed7bb55b3931)] - refactor: make rules more practical (#50) (Yiyu He <<dead_horse@qq.com>>)
2.4.0 / 2018-08-03

@@ -3,0 +9,0 @@ ==================

107

index.js

@@ -33,5 +33,4 @@ 'use strict';

if (opts.validateRoot) {
this.validateRoot = true;
}
if (opts.validateRoot) this.validateRoot = true;
if (opts.convert) this.convert = true;
}

@@ -74,3 +73,4 @@

var rule = formatRule(rules[key]);
var has = obj.hasOwnProperty(key);
var value = obj[key];
var has = value !== null && value !== undefined;

@@ -94,2 +94,3 @@ if (!has) {

convert(rule, obj, key, this.convert);
var msg = checker.call(self, rule, obj[key], obj);

@@ -184,4 +185,23 @@ if (typeof msg === 'string') {

var CONVERT_MAP = Parameter.CONVERT_MAP = {
number: 'number',
int: 'int',
integer: 'int',
string: 'string',
id: 'string',
date: 'string',
dateTime: 'string',
datetime: 'string',
boolean: 'bool',
bool: 'bool',
email: 'string',
password: 'string',
url: 'string',
};
/**
* format a rule
* - resolve abbr
* - resolve `?`
* - resolve default convertType
*

@@ -194,12 +214,57 @@ * @param {Mixed} rule

function formatRule(rule) {
rule = rule || {};
if (typeof rule === 'string') {
return { type: rule };
rule = { type: rule };
} else if (Array.isArray(rule)) {
rule = { type: 'enum', values: rule };
} else if (rule instanceof RegExp) {
rule = { type: 'string', format: rule };
}
if (Array.isArray(rule)) {
return { type: 'enum', values: rule };
if (rule.type && rule.type[rule.type.length - 1] === '?') {
rule.type = rule.type.slice(0, -1);
rule.required = false;
}
if (rule instanceof RegExp) {
return { type: 'string', format: rule };
return rule;
}
/**
* convert param to specific type
* @param {Object} rule
* @param {Object} obj
* @param {String} key
* @param {Boolean} defaultConvert
*/
function convert(rule, obj, key, defaultConvert) {
var convertType;
if (defaultConvert) convertType = CONVERT_MAP[rule.type];
if (rule.convertType) convertType = rule.convertType;
if (!convertType) return;
const value = obj[key];
// convert type only work for primitive data
if (typeof value === 'object') return;
// convertType support function
if (typeof convertType === 'function') {
obj[key] = convertType(value, obj);
return;
}
return rule || {};
switch (convertType) {
case 'int':
obj[key] = parseInt(value, 10);
break;
case 'string':
obj[key] = String(value);
break;
case 'number':
obj[key] = Number(obj[key]);
break;
case 'bool':
case 'boolean':
obj[key] = !!value;
break;
}
}

@@ -260,5 +325,5 @@

/**
* check string
* {
* allowEmpty: true, // (default to false, alias to empty)
* - check string
*- {
*- allowEmpty: true, // resolve default convertType to false, alias to empty)
* format: /^\d+$/,

@@ -279,2 +344,8 @@ * max: 100,

}
// if required === false, set allowEmpty to true by default
if (!rule.hasOwnProperty('allowEmpty') && rule.required === false) {
rule.allowEmpty = true;
}
var allowEmpty = rule.hasOwnProperty('allowEmpty')

@@ -284,11 +355,7 @@ ? rule.allowEmpty

if (!allowEmpty && value === '') {
if (!value) {
if (allowEmpty) return;
return this.t('should not be empty');
}
// if allowEmpty was set, don't need to match format
if (allowEmpty && value === '') {
return;
}
if (rule.hasOwnProperty('max') && value.length > rule.max) {

@@ -512,3 +579,3 @@ return this.t('length should smaller than %s', rule.max);

? rule
: rule.rule || formatRule.call(self, rule.itemType);
: rule.rule || formatRule(rule.itemType);

@@ -515,0 +582,0 @@ value.forEach(function (v, i) {

2

package.json
{
"name": "parameter",
"version": "2.4.0",
"version": "3.0.0",
"description": "A parameter verify tools.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -35,3 +35,4 @@ parameter

- `options.translate` - translate function
- `options.validateRoot` - config whether to validate the passed in value must be a object.
- `options.validateRoot` - config whether to validate the passed in value must be a object, default to `false`.
- `options.convert` - convert primitive params to specific type, default to `false`.
- `validate(rule, value)` - validate the `value` conforms to `rule`. return an array of errors if break rule.

@@ -42,2 +43,4 @@ - `addRule(type, check)` - add custom rules.

__Note: when `options.convert` enabled, all built-in rules check for primitive input param and convert it to rule's default `convertType`(which defined below), you can also enable this feature for specific rule by `convertType` options in each rule definition.__
### Example

@@ -78,5 +81,8 @@

- `required` - if `required` is set to false, this property can be empty. default to `true`.
- `required` - if `required` is set to false, this property can be null or undefined. default to `true`.
- `type` - The type of property, every type has it's own rule for the validate.
- `convertType` - Make parameter convert the input param to the specific type, support `int`, `number`, `string` and `boolean`, also support a function to customize your own convert method.
__Note: you can combile require and type end with a notation `?` like: `int?` or `string?` to specific both type and non-required.__
#### int

@@ -89,2 +95,6 @@

Default `convertType` is `int`.
__Note: default `convertType` will only work when `options.convert` set to true in parameter's constructor.__
#### integer

@@ -101,2 +111,4 @@

Default `convertType` is `number`.
#### date

@@ -106,2 +118,4 @@

Default `convertType` is `string`.
#### dateTime

@@ -111,2 +125,4 @@

Default `convertType` is `string`.
#### datetime

@@ -120,2 +136,4 @@

Default `convertType` is `string`.
#### boolean

@@ -125,2 +143,4 @@

Default `convertType` is `boolean`.
#### bool

@@ -134,3 +154,3 @@

- `allowEmpty`(alias to `empty`) - allow empty string, default to false.
- `allowEmpty`(alias to `empty`) - allow empty string, default to false. If `rule.required` set to false, `allowEmpty` will be set to `true` by default.
- `format` - A `RegExp` to check string's format.

@@ -140,2 +160,4 @@ - `max` - The maximum length of the string.

Default `convertType` is `string`.
#### email

@@ -147,2 +169,4 @@

Default `convertType` is `string`.
#### password

@@ -156,2 +180,4 @@

Default `convertType` is `string`.
#### url

@@ -161,2 +187,4 @@

Default `convertType` is `string`.
#### enum

@@ -186,2 +214,3 @@

- `'int'` => `{type: 'int', required: true}`
- `'int?'` => `{type: 'int', required: false }`
- `'integer'` => `{type: 'integer', required: true}`

@@ -202,5 +231,5 @@ - `'number'` => `{type: 'number', required: true}`

## `errors` examples
### `errors` examples
### `code: missing_field`
#### `code: missing_field`

@@ -215,3 +244,3 @@ ```js

### `code: invalid`
#### `code: invalid`

@@ -226,3 +255,3 @@ ```js

## Release process
### Release process

@@ -229,0 +258,0 @@ We're using [semantic-release](https://github.com/semantic-release/semantic-release) to run npm publish

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc