slime-validatorJavaScript 驗證庫
slime-validator 是一個基于 Plugin 系統(tǒng)的JavaScript驗證庫,使數(shù)據(jù)驗證變得容易。
slime-validator 用更少的代碼進行數(shù)據(jù)驗證,比其他工具節(jié)省 20% 以上的代碼,插件系統(tǒng)讓您可以非常簡單地創(chuàng)建自己的驗證規(guī)則,也可以創(chuàng)建復雜的驗證規(guī)則,在瀏覽器和 Nodejs 上均可使用。
安裝
使用 npm
$ npm install slime-validator -save
瀏覽器
<script src="${YOUR_PATH}/slime-validator.js"></script>
基本用法
對于es模塊
import Validator from 'slime-validator' // One const V1 = new Validator({ Required:true }) console.log(V1.validate('Hello world')) // Output: null console.log(V1.validate()) // Output: Input is required // Two console.log(Validator.validate({ Required:true }, 'Hello world')) // Output: null console.log(Validator.validate({ Required:true }, null)) // Output: Input is required
對于 CDN
<html> <script src="https://unpkg.com/[email protected]/dist/slime-validator.umd.js"></script> <script> let ret = null const v = new window.SlimeValidator({ field: [{ MaxLength: 10 }] }) ret = v.validate({field: '11222222221'}) console.log(JSON.stringify(ret)) </script> <body> </body> </html>
自定義驗證規(guī)則
Validator.usePlugin({ tagName: 'IsNotRequired', message(field, value, opts) { return `${field} Check failed` }, validate(field, value, opts) { return false } }, true) // true means to replace the exist rule with the same name. const V7 = new Validator({ field: { IsNotRequired: true } }) console.log(V7.validate({ field: "Something" })) // Output:{ field: 'field Check failed' }
欲了解更多信息,可以閱讀文檔
評論
圖片
表情
