2010-11-03 47 views
71

是否有node.js的一个很好的解决框架,验证一个变量:验证库的Node.js

  • 如果一个String类型,日期,数量等的
  • 最大和最小长度
  • 电子邮件,电话
  • 等...
+1

因为这个问题被关闭,从HAPI joi.js是相当广泛的JavaScript的模型框架。它拥有你想要的一切以及更多。优秀的文档和优秀的用户在制作中使用它 – 2015-01-22 10:56:24

+0

我鼓励您检查提供基于模式的验证和错误处理的[contextable.js]框架(https://github.com/xpepermint/contextablejs)。这是Node.js的某种ActiveRecord。 – xpepermint 2016-11-21 14:26:31

回答

79

我最近发现​​由chriso

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

//Validate 
check('[email protected]').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>' 
+6

是的,但节点验证器专注于字符串验证。因此,检查一个变量的类型,如“是否是Date类型?”是[不是这个图书馆是什么意思](https://github.com/chriso/node-validator/issues/52)。 – sebpiq 2013-01-22 15:29:01

+2

这是一个耻辱。我真的很喜欢他们的验证应该如何工作的想法,我认为这是一个方便的,但我想有一个方法来进行更严格的验证。 – 2013-12-14 21:30:53

+0

我刚刚发布了这个新的验证框架:https://github.com/wilkerlucio/composed-validations – 2014-04-21 06:12:08

3

不是一个变量的水平,但在一个函数的参数级别:

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

当前日期需要作为类型“对象”传递。这绝对是我忘记的东西,并会列入待办事项清单。 ;)

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

5

我认为这就是schema模块所要做的。请注意,它被标记为“正在开发中”(标记为v0.1a)。我自己没有尝试过,但从自述文件中显示的示例看起来很不错。

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 
} 
+9

没有冒犯,但那对于我来说似乎没用。我是在写一首诗还是一个节目? if(the(“string”).Iwant.to.validate.is.shorter.than(123)=== false){console.log('The string is too long'); }' – 2014-09-20 08:28:00

+3

这个超长且无用的方法调用是因为Chai被设计为编写单元测试。 – TinyTimZamboni 2015-05-27 18:57:43

+0

@SavasVedova,你正在写这两个。 – AJB 2016-09-08 05:48:52

14

我想在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 
0

我建议valida有缺少文档然而这是很容易理解看examples

Valida特点是:

  • 消毒
  • 同步和异步验证
  • 扩展
0

我写整理于JavaScript验证库(包括节点和浏览器),我将在接下来的几天内编写文档,但代码已准备就绪:https://github.com/wilkerlucio/composed-validations

请让我知道,如果你有我评论我的回答任何问题就可以了/建议:)