<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          PSVJS 數(shù)據(jù)格式驗(yàn)證工具

          聯(lián)合創(chuàng)作 · 2023-09-22 02:55

          Porco Schema Validate (psv)

          psv 是一款輕量級(jí) JS 數(shù)據(jù)格式驗(yàn)證工具,相比于其他功能齊備的驗(yàn)證工具,psv 的優(yōu)勢(shì)在于體積非常小,最開始的核心代碼只有 130 行。因此 psv 非常適合做小型項(xiàng)目、原型試錯(cuò)、個(gè)人 demo 以及教學(xué)。

          下載、安裝

          npm install psv --save

          使用

          首先你需要定義出自己的 schema,比如我:

          var schema = {
              key1: {
                  type: String,
                  required: true
              },
              key2: {
                  type: String,
                  required: true
              },
          };

          這個(gè) schema 的意思是,兩個(gè)字段(key1,key2),都是必填,string 類型。那么我傳入待驗(yàn)證的 data 結(jié)構(gòu)是:

          var data = {
              key1: 'psv',
              key2: 'psv',
          }

          接著我們導(dǎo)入并創(chuàng)建 Psv 對(duì)象進(jìn)行驗(yàn)證

          import Psv from 'psv';
          function testPsv(schema, data) {
          	const psv = new Psv(schema, data);
          	const validate = psv.validate();
          	if (!validate) {
          		psv.printErrors();
          	}
          }

          上面的代碼首先創(chuàng)建 Psv 對(duì)象,并通過(guò)構(gòu)造函數(shù)傳入 schema 和 data。接著調(diào)用 validate 函數(shù),該函數(shù)返回值為 true or false, 如果為 true 代表 data 符合 schema 定義,為 false 的話,可以通過(guò) psv.printErrors() 或者 psv.getErrors() 來(lái)獲取錯(cuò)誤信息。

          api

          目前支持五種數(shù)據(jù)類型的定義:String, Number, Array, Boolean, Object。

          1.String

          str: {
              type: String,
              max: 5,
              min: 3,
              pattern: '^[0-9]*$',
              required: true
          },

          max : 最大長(zhǎng)度 (數(shù)字)

          min : 最小長(zhǎng)度 (數(shù)字)

          pattern : 自定義正則表達(dá)式 (正則表達(dá)式)

          required : 是否必須 (布爾)

          2.Number

          num: {
              type: Number,
              max: 5,
              min: 3,
              required: true
          },

          max : 最大值 (數(shù)字)

          min : 最小值 (數(shù)字)

          required : 是否必須 (布爾)

          3.Array

          array: {
              type: [Number],
              max: 5,
              min: 3,
              required: true
          },

          max : 最大長(zhǎng)度 (數(shù)字)

          min : 最小長(zhǎng)度 (數(shù)字)

          required : 是否必須 (布爾)

          數(shù)組支持對(duì)象元素檢測(cè)(可嵌套,同樣,根據(jù)我們團(tuán)隊(duì)的實(shí)際應(yīng)用場(chǎng)景,我們建議采用扁平化的數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))

          const schema2 = {
              str: {
                  type: String,
                  required: true
              },
              num: {
                  type: Number,
                  required: true
              }
          };
          const schema = {
              key: {
                  type: [schema2],
                  required: true
              }
          };

          如果你不希望對(duì)數(shù)組檢測(cè),或者說(shuō),數(shù)組元素類型不確定,可以采用 Array 定義,我們將不會(huì)對(duì)其進(jìn)行類型檢測(cè)

          const schema = {
              array: {
                  type: Array,
                  max: 5,
                  min: 3,
                  required: true
              }
          };
          const data = {
              array: [1, '1', true, {}]
          };

          4.Boolean

          boo: {
              type: Boolean,
              required: true
          },

          required : 是否必須 (布爾)

          5.Object

          object: {
              type: Object,
              required: true
          },

          required : 是否必須 (布爾)

          注意:當(dāng) type = Object 時(shí),說(shuō)明該字段可以是任何 js 基本類型或?qū)ο?,甚至可以?一個(gè) 函數(shù)(慎用)。

          同樣,psv 支持嵌套定義

          const schema2 = {
              str2: {
                  type: String,
                  required: true
              }
          }
          const schema = {
              str1: {
                  type: schema2,
                  required: true
              },
          };

          升級(jí):

          2.1.8

          開始加入枚舉類型,目前僅限 string, number 兩大類型支持。

          string :

          const schema = {
              enum: {
                  type: String,
                  enum: ['1', '2', '3'],
                  required: true
              }
          };
          const data = {
              enum: '1'
          };

          number:

          const schema = {
              enum: {
                  type: Number,
                  enum: [1, 2, 3],
                  required: true
              }
          };
          const data = {
              enum: 3
          };
          瀏覽 23
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美操逼手机视频 | 亚洲无码人妻 | 在线色网| 亚洲精品电影网 | 乱伦月 大像传媒 |