Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ckvv committed Feb 25, 2021
0 parents commit 2cadeca
Show file tree
Hide file tree
Showing 11 changed files with 1,613 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
34 changes: 34 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:

publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

publish-gpr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.git_token}}
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 chenkai0520

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
136 changes: 136 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Parameter
Base on [parameter](https://www.npmjs.com/package/parameter)

Added tip parameter, which is used to prompt errors when parameter verification fails.

# Install
```
npm i @c_kai/parameter --save
```

# Usage

`Parameter` Class

- `constructor([options])` - new Class `Parameter` instance
- `options.convert` - 数据的类型进行转换,如 string:`'5'` => number `5`, default to `false`
- `strict` - 如果为`true` 如果数据中的key在对应rule中不存在会被删除,default to `false`
- `validate(rule, value, opts)` - 校验 `value` 是否符合 `rule`. 如果不符合会返回错误数组
- `addRule(type, check)` - 增加自定义规则
- `type` - 规则类型
- `check(rule, value, obj)` - 回调函数,返回的信息表示错误

# Rule

#### common rule

- `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.
- `default` - The default value of property, once the property is allowed non-required and missed, parameter will use this as the default value. **This may change the original input params**.

#### int

If type is `int`, there has tow addition rules:

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

Default `convertType` is `int`.

#### number

If type is `number`, there has tow addition rules:

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

Default `convertType` is `number`.

#### string

If type is `string`, there has four addition rules:

- `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.
- `max` - The maximum length of the string.
- `min` - The minimum length of the string.
- `trim` - Trim the string before check, default is `false`.

Default `convertType` is `string`

#### boolean

Match `boolean` type value.

Default `convertType` is `boolean`.

#### enum

If type is `enum`, it requires an addition rule:

- `values` - An array of data, `value` must be one on them. ***this rule is required.***

#### array

If type is `array`, there has four addition rule:

- `itemType` - The type of every item in this array.
- `rule` - An object that validate the items of the array. Only work with `itemType`.
- `max` - The maximun length of the array.
- `min` - The minimun lenght of the array.


# example
```js

let parameter = new Parameter({
convert: true,
});

parameter.addRule('even', (rule, value)=>{
if(value % 2 !== 0){
return '不是一个偶数';
}
return null;
});

var data = {
email: 'chenkai@mapplat.com',
name: 'xiao hong',
age: 24,
gender: 'male',
arr: ['1','2', '3'],
arr2: [1,2,3,9,10,4,19],
evenNum: 8,
};
var rule = {
email: /^[0-9a-zA-Z_.-]{2,20}@[0-9a-zA-Z_-]{1,20}(\.[a-zA-Z0-9_-]{2,8}){1,2}$/,
name: {
type: 'string',
min: 0,
},
age: {
type: 'int',
min: 0,
msg: '需要是一个大于0的整数',
convert: false,
},
gender: ['male', 'female', 'unknown'],
arr: {
type: 'array',
min: 0,
itemType: 'int',
},
evenNum: {
type: 'even',
}
};

let errors = parameter.validate(rule, data, {
strict: true,
});

```


5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 2.0.0
+ 精简了一些代码
+ 可以通过msg自定义返回错误
+ 增加了array的类型转换

53 changes: 53 additions & 0 deletions examples/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
let Parameter = require("./parameter");

let parameter = new Parameter({
convert: true,
});

parameter.addRule('even', (rule, value)=>{
console.log(arguments)
if(value % 2 !== 0){
return '不是一个偶数';
}
return null;
});

let data = {
email: 'chenkai@mapplat.com',
name: 'xiao hong',
age: 24,
gender: 'male',
arr: ['1','2', '3'],
arr2: [1,2,3,9,10,4,19],
evenNum: 8,
};

let rule = {
email: /^[0-9a-zA-Z_.-]{2,20}@[0-9a-zA-Z_-]{1,20}(\.[a-zA-Z0-9_-]{2,8}){1,2}$/,
name: {
type: 'string',
min: 0,
},
age: {
type: 'int',
min: 0,
msg: '需要是一个大于0的整数',
convert: false,
},
gender: ['male', 'female', 'unknown'],
arr: {
type: 'array',
min: 0,
itemType: 'int',
},
evenNum: {
type: 'even',
}
};

let errors = parameter.validate(rule, data, {
strict: true,
});

console.log(JSON.stringify(errors));
console.log(data);
Loading

0 comments on commit 2cadeca

Please sign in to comment.