当前位置:  开发笔记 > 编程语言 > 正文

Node.js的验证库

如何解决《Node.js的验证库》经验,为你挑选了4个好方法。

是否有一个很好的验证框架用于验证变量的node.js:

如果它是一种String,Date,Number等类型

最大和最小长度

电子邮件,电话

等等...

Baggz.. 82

我最近发现了chriso的node-validator.

var check = require('validator').check,
    sanitize = require('validator').sanitize

//Validate
check('test@email.com').len(6, 64).isEmail();       //Methods are chainable
check('abc').isInt();                               //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt();      //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);

//Sanitize / Filter
var int = sanitize('0123').toInt();                  //123
var bool = sanitize('true').toBoolean();             //true
var str = sanitize(' \s\t\r hello \n').trim();      //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a');        //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('<a>').entityDecode();     //''

是,但节点验证器侧重于字符串验证.因此,检查变量的类型,如"是Date类型吗?" 是[不是这个库的意思](https://github.com/chriso/node-validator/issues/52). (6认同)

这是一种耻辱.我真的很喜欢他们的验证如何工作的想法,我认为是一个方便的,但我希望有一种方法来进行更严格的验证. (2认同)


Parris.. 15

我想要ruby on rails和cakephp样式验证.我知道这是我会反复使用的东西,所以我制作了这个快速的npm模块:https://npmjs.org/package/iz

它在语义上与茉莉一样,可以在客户端或服务器端使用.这意味着它支持commonjs和amd以及通过JSON传递的验证规则.

它经过了单元测试,没有生产依赖性,并且范围被锁定为仅验证.我们现在似乎有一个小社区.欢迎提出想法,反馈和拉取请求.

当前库函数:

iz.alphaNumeric(*);               // Is number or string(contains only numbers or strings)
iz.between(number, start, end);   // Number is start or greater but less than or equal to end, all params numeric
iz.blank(*);                      // Empty string, undefined or null
iz.boolean(*);                    // true, false, 0, 1
iz.cc(*);                         // Luhn checksum approved value
iz.date(*);                       // Is a data obj or is a string that is easily converted to a date
iz.decimal(*);                    // Contains 1 decimal point and potentially can have a - at the beginning
iz.email(*);                      // Seems like a valid email address
iz.extension(ob1, ob2);           // If obj2's methods are all found in obj1
iz.fileExtension(arr, value);     // Checks if the extension of value is in arr. An obj can be provide, but must have indexOf defined.
iz.fileExtensionAudio(value);     // Check against mp3, ogg, wav, aac
iz.fileExtensionImage(value);     // Check against png, jpg, jpeg, gif, bmp, svg, gif
iz.inArray(arr, value);           // If * is in the array
iz.int(*, bool (optional));       // Is an int. If the 2nd variable is true (false by default) a decimal is allowed
iz.ip(str);                       // str resembles an IPV4 or IPV6 address
iz.minLen(val, min);              // val (str or arr) is greater than min
iz.maxLen(val, max);              // val (str or arr) is shorter than max
iz.multiple(num, mult);           // Number is multiple of another number
iz.number(*);                     // Is either an int or decimal
iz.ofType(obj, typeName);         // If it is a named object, and the name matches the string
iz.phone(str, canHaveExtension?); // Is an american phone number. Any punctuations are allowed.
iz.postal(*);                     // Is a postal code or zip code
iz.ssn(*);                        // Is a social security number


Trantor Liu.. 9

Node-validator是一个字符串验证,过滤和清理方法的库.

所以,如果你想拥有数字和阵列更好的支持,您可以尝试Chai.js.以下是一些例子:

var expect = require('chai').expect;
try {
    expect([1, 2, 3]).to.have.length.below(4);
    expect(5).to.be.within(3,6);
    expect('test').to.have.length(4);
} catch (e) {
    // should not occur
}

没有冒犯,但是,对我来说,对我来说似乎毫无用处.我在写一首诗还是一个节目?`if(the("string").I.want.to.validate.is.shorter.than(123)=== false){console.log('字符串太长'); }` (11认同)

因为Chai被设计用于编写单元测试,所以会有超长且无用的方法调用. (6认同)


intuited.. 5

我认为这是架构模块要做的事情.请注意,它被标记为"正在开发中"(标记为v0.1a).我自己没有尝试过,但从README中显示的示例看起来相当不错.



1> Baggz..:

我最近发现了chriso的node-validator.

var check = require('validator').check,
    sanitize = require('validator').sanitize

//Validate
check('test@email.com').len(6, 64).isEmail();       //Methods are chainable
check('abc').isInt();                               //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt();      //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);

//Sanitize / Filter
var int = sanitize('0123').toInt();                  //123
var bool = sanitize('true').toBoolean();             //true
var str = sanitize(' \s\t\r hello \n').trim();      //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a');        //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('<a>').entityDecode();     //''


是,但节点验证器侧重于字符串验证.因此,检查变量的类型,如"是Date类型吗?" 是[不是这个库的意思](https://github.com/chriso/node-validator/issues/52).
这是一种耻辱.我真的很喜欢他们的验证如何工作的想法,我认为是一个方便的,但我希望有一种方法来进行更严格的验证.

2> Parris..:

我想要ruby on rails和cakephp样式验证.我知道这是我会反复使用的东西,所以我制作了这个快速的npm模块:https://npmjs.org/package/iz

它在语义上与茉莉一样,可以在客户端或服务器端使用.这意味着它支持commonjs和amd以及通过JSON传递的验证规则.

它经过了单元测试,没有生产依赖性,并且范围被锁定为仅验证.我们现在似乎有一个小社区.欢迎提出想法,反馈和拉取请求.

当前库函数:

iz.alphaNumeric(*);               // Is number or string(contains only numbers or strings)
iz.between(number, start, end);   // Number is start or greater but less than or equal to end, all params numeric
iz.blank(*);                      // Empty string, undefined or null
iz.boolean(*);                    // true, false, 0, 1
iz.cc(*);                         // Luhn checksum approved value
iz.date(*);                       // Is a data obj or is a string that is easily converted to a date
iz.decimal(*);                    // Contains 1 decimal point and potentially can have a - at the beginning
iz.email(*);                      // Seems like a valid email address
iz.extension(ob1, ob2);           // If obj2's methods are all found in obj1
iz.fileExtension(arr, value);     // Checks if the extension of value is in arr. An obj can be provide, but must have indexOf defined.
iz.fileExtensionAudio(value);     // Check against mp3, ogg, wav, aac
iz.fileExtensionImage(value);     // Check against png, jpg, jpeg, gif, bmp, svg, gif
iz.inArray(arr, value);           // If * is in the array
iz.int(*, bool (optional));       // Is an int. If the 2nd variable is true (false by default) a decimal is allowed
iz.ip(str);                       // str resembles an IPV4 or IPV6 address
iz.minLen(val, min);              // val (str or arr) is greater than min
iz.maxLen(val, max);              // val (str or arr) is shorter than max
iz.multiple(num, mult);           // Number is multiple of another number
iz.number(*);                     // Is either an int or decimal
iz.ofType(obj, typeName);         // If it is a named object, and the name matches the string
iz.phone(str, canHaveExtension?); // Is an american phone number. Any punctuations are allowed.
iz.postal(*);                     // Is a postal code or zip code
iz.ssn(*);                        // Is a social security number



3> Trantor Liu..:

Node-validator是一个字符串验证,过滤和清理方法的库.

所以,如果你想拥有数字和阵列更好的支持,您可以尝试Chai.js.以下是一些例子:

var expect = require('chai').expect;
try {
    expect([1, 2, 3]).to.have.length.below(4);
    expect(5).to.be.within(3,6);
    expect('test').to.have.length(4);
} catch (e) {
    // should not occur
}


没有冒犯,但是,对我来说,对我来说似乎毫无用处.我在写一首诗还是一个节目?`if(the("string").I.want.to.validate.is.shorter.than(123)=== false){console.log('字符串太长'); }`
因为Chai被设计用于编写单元测试,所以会有超长且无用的方法调用.

4> intuited..:

我认为这是架构模块要做的事情.请注意,它被标记为"正在开发中"(标记为v0.1a).我自己没有尝试过,但从README中显示的示例看起来相当不错.

推荐阅读
yzh148448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有