node-modules / parameter Goto Github PK
View Code? Open in Web Editor NEWA parameter verify tools.
License: Other
A parameter verify tools.
License: Other
例如用户预约日期字段,用户原先选择了一个日期,保存成功。
后来用户想暂时移除该日期,于是删除了选择的日期,此时前端将该字段以 NULL 形式告知后端,该字段已被删除。
但此时 parameter 做校验时,即使标识了 required = false, allowEmpty = true
,仍然会报错。
https://github.com/node-modules/parameter/blob/master/index.js#L101
[
{
message: 'name should be a string',
code: 'invalid',
field: 'name'
}
]
to
[
{
message: 'should be a string',
code: 'invalid',
field: 'name'
}
]
这样一来,message 的内容是固定的,就可以写 I18n 对应其他语言了。
addRule,完全不知道check作为fun的时候是否对返回值有要求,什么情况下是验证通过,什么情况下是验证不通过
假设 我有两个字段, 只需要存在其中一个字段即可.
这个怎么去写 Rule.
Hey there!
I'd like to report a security issue but cannot find contact instructions on your repository.
If not a hassle, might you kindly add a SECURITY.md
file with an email, or another contact method? GitHub recommends this best practice to ensure security issues are responsibly disclosed, and it would serve as a simple instruction for security researchers in the future.
Thank you for your consideration, and I look forward to hearing from you!
(cc @huntr-helper)
大多数情况下,要验证某个字段,一般去验证其类型。必填字段占比并不高。
那么每次大多数字段我都要 设置 required: false
太麻烦
一般写ts的时候会先申明一个interface(这个interface只能够解决ts编译前的校验),可以利用这个interface来生成rule,校验运行时的参数,比如对一个request做参数校验:
interface ParamRequest {
id: number,
page: string,
hehe: boolean,
haha: Array<string>,
}
let rule: ParamRequest;
const param: ParamRequest = this.request.body;
const isError = this.validate.validator(rule, param);
比如 type:int 允许 "1"、"2",type:boolean 允许 0 、1 等等
有支持的计划吗?pr 我可以提
Line 626 in 9c305ec
I feel there is something wrong with this line of code in function checkArray()
....
const subRule = rule.itemType === 'object'
? rule
: rule.rule || formatRule(rule.itemType);
.....
========should be changed to:============
const subRule = rule.itemType === 'object'
? rule.rule
: rule.rule || formatRule(rule.itemType);
const Parameter = require('parameter');
var parameter = new Parameter({
translate: function() {
var args = Array.prototype.slice.call(arguments);
console.log(args);
},
validateRoot: true, // restrict the being validate value must be a object
});
var data = {
name: 'yong',
age: 12,
gender: 'male',
email: '[email protected]'
};
var rule = {
name: 'string',
age: {type:'int', max: 150, min: 0},
gender: ['male', 'female', 'unknown'],
email: {type:'email', format: Parameter.EMAIL_RE}
};
var errors = parameter.validate(rule, data);
if(errors)
console.log(errors);
email正确的情况下,为什么translate函数中还要打印出[ 'should be an email' ]
另外,什么时候能够支持手机号码验证?
rule:
{ category: { type: 'array', itemType: 'int', required: false, } }
data:
{ category: [ '403', '407', '303', '307' ] }
比如在检索字符串模糊查询的时候,输入/\
字符,则会破坏正则表达式,是否可以在校验的时候添加一个自定义过滤的方法解决这样的问题呢?
someField: {
type: 'string',
required: false,
allowEmpty: true,
min: 6,
max: 20,
}
这个field可为空,如果不为空则限定长度。
BTW, 这个issue和 #20 有关吗?
谢谢。
不好意思,实在没看明白自定义规则如何使用。
const rule = { title: 'myRule' };
let r = ctx.validate(rule, ctx.request.body);
r 一直是undefined
Is it possible to add ISO format support in validation rules of type dateTime
?
定义一下类型库, 方便智能提示可以么
The docs describe the required
default to true, but for my project, the conditions required false
are more than true
.
May provide a config like :
var parameter = new Parameter({
defaultRequired: false,
});
{
unit: ['y', 'w', 'd', 'm']
}
// or
{
unit: { type: ['y', 'w', 'd', 'm'], required: false, empty: true }
}
such as:
{
color: ['red', 'blue', 'black']
}
过滤未知字段是一个很常用且有用的功能啊,前端传参时不一定针对每个接口都pick。如果需要更严格的时候, 存在未知字段就返回错误。这样经过校验的参数都可以放心使用。不然参数经过校验后,还需要去判断存不存在一些未知字段?
example:
const rule = {
username: 'string',
token: 'string',
role: 'string',
attributes: 'object',
offset: {
delete: true,
},
count: 'int',
};
let params = {
username: 'username',
token: 'token',
role: 'role',
attributes: 'attributes',
offset: 'offset',
count: 'count',
count1: 'count1',
}
console.log(validate(rule, params));
console.log(delete(rule, params));
out:
false || Error();
{
username: 'username',
token: 'token',
role: 'role',
attributes: 'attributes',
count: 'count',
};
不支持自定义message,请参考https://github.com/jzaefferer/jquery-validation ,很多时候messages节点内提示的文案内容是自定义的
const loginRule = {
rules: {
username: {
type: 'email'
},
password: {
required: true
}
},
messages: {
username: {
required: "请输入用户名.",
email: "请输入正确的邮箱格式"
},
password: {
required: "请输入如密码."
},
}
};
when i check numeric types,like {price:'number'},the module convert the value of the price to number.And then,check.
The problem is checking numeric enum,like {type: 'enum', values: [1, 2]}.It seems that converting is
ineffective.
Related issue: #25 (in chinese).
Use case:
const obj = {test: null};
parameter.validate({
test: {
type: "integer",
required: false
}
}, obj);
The above code will give the error should be an integer
even if the required: false rule is defined.
Where is the problem:
https://github.com/node-modules/parameter/blob/master/index.js, row 61 to 63
var has = obj.hasOwnProperty(key);
if (!has) {
This condition should be replaced with:
var rule = formatRule(rules[key]);
var value = obj[key];
if (value == undefined) {
if (rule.required) {
/* push required error */
}
continue;
}
Is there any possible to add a function type?
const arr = [{ key: ' ' }];
const rules = {};
validate(rules, arr);
No matter how to set the rule can not validate the array
验证数据类型为 int ,
当POST提交的数据为 aa , 验证不通过
当POST提交的数据为 aa123 , 验证不通过
当POST提交的数据为 123aa , 验证通过
这是不是属于个 bug
const {ctx}=this; let rules={ fdate:{type:'string'}, fdept:{type:'int'}, fid:{type:'string'}, }; try{ ctx.validate(rules,ctx.request.body); }catch(err){ ctx.body={ code: -1, msg: err }; return; }
`
var Rule = {
name: { type: 'string' },
password: { type: 'string' },
};
var shouldThrowError = {
"name":"test",
"password":"test",
"password2":"test"
}
`
实际上会验证成功,至少应该有可以配置不能有多余参数的配置
name: {type: 'regexp', exp: '/babababa/'}
如题,请问如何对上传的文件、压缩包进行校验?是否支持?
{ name: { type: 'string', required: true, format: /[
!@#$^&*()=|{}':;',[].<>《》/?!@#¥……&*()——|{}【】‘;:”“'。,、?]/ }
}
`
例子好比是这样,怎么写才能校验不能匹配规则算通过,匹配上这个正则算不通
I would be great to have the ability to remove not permitted attributes. For example:
const params = { age: 25, foo: "bar" }
const rules = { age: { type: "int", max: 200 } }
const errors = parameter.validate(rules, params)
make params
to be equal to {age: 25}
, not to {age: 25, foo: "bar"}
.
建议修改默认抛出的错误信息,可以return一个错误信息或者抛出一个非直接返回的异常
能不能自定义message啊
我现在在项目中自己写定义文件,但是有些东西总是写不好,如果直接支持那就好了
例如:
var data = {
mail: ''
};
var rule = {
mail: {
required: false,
allowEmpty: true,
type: 'email'
}
};
var errors = parameter.validate(rule, data);
这个时候会验证失败
如题
试着用了koa的框架集成进来 做一个参数验证的中间件 试了几次 发现静态的数据传过去没有问题
但是通过路由传来的数据貌似不起作用
随后发现源代码中 validate的这个方法检查属性用的是hasownproperty 这个方法 不能检查对象原型链中的属性
于是将hasownproperty 换成了 key in obj 才可以使用
var has = key in obj
I need check param is uuid, but lib not support.
My example code:
var data = {
id: '1b671a64-40d5-491e-99b0-da01ff1f3341',
name: 'foo',
age: 24,
gender: 'male'
};
var rule = {
id: 'uuid', // need support
name: 'string',
age: 'int',
gender: ['male', 'female', 'unknown']
};
var errors = parameter.validate(rule, data);
i think it support in next version.
tks so much
parameter.addRule({
type: 'email',
format: /^\w+@\w+$/
});
parameter.addRule({
type: 'password',
check: function (value, ruleConfig, obj) {
if (value !== obj[ruleConfig.compare]) {
return 'should equal to ' + ruleConfig.compare;
}
}
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.