用于 Node.js 的验证库

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

  • 如果是字符串、日期、数字等类型
  • 最大和最小长度
  • 电子邮件,电话
  • 等等。
61416 次浏览

不是在变量级别,而是在函数参数级别:

Http://github.com/torvalamo/argtype.js

当前需要以“ object”类型传递的日期。这绝对是一件我已经忘记了的事情,我会把它列入待办事项清单。;)

不支持特定的最大和最小长度,可能不会实现(但谁知道)。电子邮件,电话和所有可以检查的正则表达式。请参见 github 页面上的示例,其中包含一个(简单的) regex 示例。

我想这就是 模式模块要做的事情。注意,它被标记为“正在开发”(标记为 v0.1 a)。我自己还没有试过,但从 README 中显示的示例来看,它看起来相当不错。

我最近通过 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('&lt;a&gt;').entityDecode();     //'<a>'

Node-validator 是一个字符串验证、过滤和消毒方法库。

因此,如果你想更好地支持数字和数组,你可以试试 谢谢:

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
}

我想要红宝石在轨道和 Cakephp 风格验证。我知道这是我会反复使用的东西,所以我做了这个快速 npm 模块: https://npmjs.org/package/iz

它的语义读起来很像茉莉花,可以用于客户端或服务器端。这意味着它提供了对 commjs 和 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

我建议 Valida有缺乏的文档,但它是相当简单的理解,看看 例子

Valida 的特点是:

  • 消毒
  • 同步和异步验证
  • 团体
  • 可扩展的

我正在完成一个关于 Javascript 验证(包括节点和浏览器)的库的编写,接下来几天我将编写文档,但是代码已经差不多准备好了: https://github.com/wilkerlucio/composed-validations

如果你对此有任何问题/建议,请告诉我:)