首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Node.js的验证库

Node.js的验证库
EN

Stack Overflow用户
提问于 2010-11-03 23:30:24
回答 7查看 60.5K关注 0票数 75

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

如果类型为字符串、日期、数字等,请使用

  • email的最大和最小长度,phone
  • etc...
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-12-24 06:10:56

我最近发现了chrisonode-validator

示例

代码语言:javascript
复制
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>'
票数 87
EN

Stack Overflow用户

发布于 2012-09-30 11:01:38

我想要ruby on rails和cakephp风格的验证。我知道我会反复使用它,所以我做了这个快速的npm模块:https://npmjs.org/package/iz

它在语义上读起来很像jasmine,可以在客户端或服务器端使用。这意味着它支持commonjs和amd,以及通过JSON传入的验证规则。

它经过了很好的单元测试,没有生产依赖关系,范围仅限于验证。我们现在似乎有一个小社区在运行。想法,反馈和拉取请求都是受欢迎的。

当前库函数:

代码语言:javascript
复制
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
票数 15
EN

Stack Overflow用户

发布于 2012-07-24 19:48:26

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

因此,如果你想对数字和数组有更好的支持,你可以试试Chai.js。下面是一些示例:

代码语言:javascript
复制
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
}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4088723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档