【问题标题】:Validation library for Node.js [closed]Node.js 的验证库 [关闭]
【发布时间】:2011-05-04 13:48:15
【问题描述】:

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

  • 如果是字符串、日期、数字等类型
  • 最大和最小长度
  • 电子邮件、电话
  • 等等……

【问题讨论】:

  • 因为问题已经结束,所以我正在评论我的答案,来自 hapi 的 joi.js 是一个非常广泛的 JavaScript 模型框架。它拥有您想要的一切以及更多。优秀的文档和优秀的用户在作品中使用它
  • 我鼓励您检查contextable.js 框架,它提供了基于模式的验证和错误处理。这是 Node.js 的某种 ActiveRecord。

标签: javascript validation node.js


【解决方案1】:

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

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

日期当前需要作为“对象”类型传递。这绝对是我忘记的东西,并将放在待办事项列表中。 ;)

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

【讨论】:

    【解决方案2】:

    我认为这是schema 模块打算做的事情。请注意,它被标记为“开发中”(标记为 v0.1a)。我自己没有尝试过,但从 README 中显示的示例看起来很不错。

    【讨论】:

      【解决方案3】:

      我最近通过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 专注于字符串验证。因此检查变量的类型,例如“它是 Date 类型吗?”是not what this library is meant for
      • 很遗憾。我真的很喜欢他们关于验证应该如何工作的想法,我认为这是一个方便的方法,但是我希望有一种方法来进行更严格的验证。
      • 我刚刚发布了这个新的验证框架:github.com/wilkerlucio/composed-validations
      • 我找不到如何将验证器用于可选参数?节点验证器是否提供此功能。
      • 我检查了所有这些,但根本没有发现它们有表现力,我自己写了npmjs.org/package/indicative
      【解决方案4】:

      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
      }
      

      【讨论】:

      • 无意冒犯,但 to.have, to.be 对我来说似乎没用。我是在写诗还是在写程序? if (the("string").I.want.to.validate.is.shorter.than(123) === false) { console.log('The string is too long'); }
      • 存在超长且无用的方法调用,因为 Chai 是为编写单元测试而设计的。
      • @SavasVedova,你在写这两个。
      【解决方案5】:

      我想要 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
      

      【讨论】:

        【解决方案6】:

        我推荐valida 缺少文档,但是查看examples 很容易理解。

        验证功能是:

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

        【讨论】:

          【解决方案7】:

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

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

          【讨论】:

            猜你喜欢
            • 2011-04-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-03-30
            • 2010-09-06
            • 1970-01-01
            相关资源
            最近更新 更多