回答
我最近發現由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('<a>').entityDecode(); //'<a>'
是的,但節點驗證器專注於字符串驗證。因此,檢查一個變量的類型,如「是否是Date類型?」是[不是這個圖書館是什麼意思](https://github.com/chriso/node-validator/issues/52)。 – sebpiq 2013-01-22 15:29:01
這是一個恥辱。我真的很喜歡他們的驗證應該如何工作的想法,我認爲這是一個方便的,但我想有一個方法來進行更嚴格的驗證。 – 2013-12-14 21:30:53
我剛剛發佈了這個新的驗證框架:https://github.com/wilkerlucio/composed-validations – 2014-04-21 06:12:08
不是一個變量的水平,但在一個函數的參數級別:
http://github.com/torvalamo/argtype.js
當前日期需要作爲類型「對象」傳遞。這絕對是我忘記的東西,並會列入待辦事項清單。 ;)
特定的最大和最小長度不支持,並可能不會實現(但誰知道)。電子郵件,電話和所有可以通過正則表達式檢查。請參閱github頁面上的示例,其中包含一個(簡單)正則表達式示例。
我認爲這就是schema模塊所要做的。請注意,它被標記爲「正在開發中」(標記爲v0.1a)。我自己沒有嘗試過,但從自述文件中顯示的示例看起來很不錯。
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」).Iwant.to.validate.is.shorter.than(123)=== false){console.log('The string is too long'); }' – 2014-09-20 08:28:00
這個超長且無用的方法調用是因爲Chai被設計爲編寫單元測試。 – TinyTimZamboni 2015-05-27 18:57:43
@SavasVedova,你正在寫這兩個。 – AJB 2016-09-08 05:48:52
我想在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
我寫整理於JavaScript驗證庫(包括節點和瀏覽器),我將在接下來的幾天內編寫文檔,但代碼已準備就緒:https://github.com/wilkerlucio/composed-validations
請讓我知道,如果你有我評論我的回答任何問題就可以了/建議:)
- 1. 的Node.js + mogoose驗證
- 2. Node.js - URL的輸入驗證
- 3. 驗證在node.js中的PHP證書
- 4. Node.js的用戶身份驗證?
- 5. Swagger + Node.js的異步驗證功能
- 6. 如何驗證node.js中的csv文件?
- 7. Node.js中間件身份驗證變量
- 8. node.js通過身份驗證狀態
- 9. LTI使用Node.js啓動身份驗證
- 10. Node.js HTTPS服務器驗證失敗
- 11. Node.js EJS快速驗證器爲每個
- 12. node.js移動後端身份驗證
- 13. 在node.js中驗證一個JWT
- 14. 無法驗證HTTP GET請求Node.js + Expressjs
- 15. Node.js雙因素身份驗證
- 16. Rails,Node.js跨服務器身份驗證
- 17. node.js,css驗證,w3c禁止我?
- 18. 驗證庫PHP/Javascript
- 19. 存儲庫驗證
- 20. NET Framework驗證庫
- 21. 驗證數據庫
- 22. 數據庫驗證
- 23. 良好的PHP驗證庫
- 24. 驗證庫PHP/MySQL的
- 25. Swing的驗證碼庫?
- 26. 壁紙中的許可證驗證庫
- 27. 的Node.js - 以HTML形式發送數據的Node.js和驗證插入的數據
- 28. 企業庫驗證塊 - 驗證可爲空的屬性?
- 29. 在ASP.net MVC流利的驗證 - 數據庫驗證
- 30. Symfony2的做表單驗證而不是數據庫驗證
因爲這個問題被關閉,從HAPI joi.js是相當廣泛的JavaScript的模型框架。它擁有你想要的一切以及更多。優秀的文檔和優秀的用戶在製作中使用它 – 2015-01-22 10:56:24
我鼓勵您檢查提供基於模式的驗證和錯誤處理的[contextable.js]框架(https://github.com/xpepermint/contextablejs)。這是Node.js的某種ActiveRecord。 – xpepermint 2016-11-21 14:26:31