@jdplus/descaler

1.2.0 • Public • Published

使用说明

=============================== Coverage summary ===============================
Statements   : 81.03% ( 94/116 )
Branches     : 73.25% ( 115/157 )
Functions    : 68.97% ( 20/29 )
Lines        : 100% ( 1/1 )
================================================================================

版本 1.2.0

增加 break 中断功能; action.path 增加相对路径_parent 功能;

引入

npm i @jdplus/descaler

项目中 init 函数使用方式

import { init, descaler } from "@jdplus/descaler";
import { configJSON } from "@/service/descaler.config";
init({
    onError: (e) => {
        //工具异常回调
        // {
        //     msg:"",
        //     err: {e}
        // }
        console.log(e);
    },
    onJSONErr: (e) => {
        //JSON校验异常回调 e:
        // {
        //     msg: "接口兜底捕获接口异常" + apiName,
        //     descalerErr: {
        //          rule,   // 触发的配置规则
        //          apiName,    // 触发的apiName
        //          checkValue,   // 触发校验的值
        //          type,       // 规则中action.type
        //          value,      // 规则action.value
        //          path,       // 规则action.path
        //          json,       // 接口返回值json
        //          apiCatchError   //接口catch捕获异常的error, 如果不是catch捕获,此项为undefined
        //     }
        // }
        console.log(e);
    },
    configJSON: configJSON, // 传入配置文件
    axiosInstance: teaPlus // 传入axios实例, 用于添加响应拦截器
});

descaler 函数单独使用

import { init, descaler } from "@jdplus/descaler";

// 监听descaler异常
init({
    onError: (e) => {
        //工具异常回调
        // {
        //     msg:"",
        //     err: {e}
        // }
        console.log(e);
    },
    onJSONErr: (e) => {
        //JSON校验异常回调 e:
        // {
        //     msg: "接口兜底捕获接口异常" + apiName,
        //     descalerErr: {
        //          rule,   // 触发的配置规则
        //          apiName,    // 触发的apiName
        //          checkValue,   // 触发校验的值
        //          type,       // 规则中action.type
        //          value,      // 规则action.value
        //          path,       // 规则action.path
        //          json,       // 接口返回值json
        //          apiCatchError   //接口catch捕获异常的error, 如果不是catch捕获,此项为undefined
        //     }
        // }
        console.log(e);
    }
});

/**
 * descaler 检测json并
 *
 * @param {object} config 配置参数
 * @param {object} json 需要校验的数据
 * @param {string} apiName 传入后用于监听异常时分辨监听到的异常数据
 * @return {Object} 执行配置参数后的数据
 */

var formatData = descaler(config, json, apiName);

// 配置参数说明
// {
//     rule: {
//         path: "rs.curTime",
//         type: "type",
//         value: "number"
//     },
//     action: {
//         path: "_self",
//         type: "default",
//         value: null
//     }
// }

init configJSON 配置文件说明

//配置文件示例
const configJSON = [
    {
        apiName: "rights/getActivitySwitch",
        check: [
            {
                rule: {
                    path: "rs.curTime",
                    type: "type",
                    value: "number"
                },
                action: {
                    path: "_self",
                    type: "default",
                    value: null
                }
            },
            {
                rule: {
                    path: "rs.doc_super_card_activity_time",
                    type: "JSONParse",
                    value: "JSONString"
                },
                action: {
                    path: "_self",
                    type: "jumpHash",
                    value: "other"
                }
            }
        ]
    },
    {
        apiName: "activityUser/isInWhiteList",
        check: [
            {
                rule: {
                    path: "rs.isInWhiteList",
                    type: "type",
                    value: "boolean"
                },
                action: {
                    path: "_self",
                    type: "default",
                    value: false
                }
            }
        ]
    }
];

rule.path action.path 路径

用于描述校验数据的路径, 正常情况直接传入路径的字符串就可以 举例 "a.b[0].c" 就可以从下面 data 中取到 1 不存在的值会返回 undefined

如果想校验所有的 c, 可以写为 "a.b[***].c"

如果想校验 data1 中的 c, 可以写为 "a.***.c"

path = "a.b"
var data = {
    a:{
        b:[
            {c:1},
            {c:2},
            {c:3},
            {c:4},
        ]
    }
}
var data1 = {
    a:{
        b0:{c:1}
        b1:{c:1}
        b2:{c:1}
        b3:{c:1}
        b4:{c:1}
    }
}

action.path 与 rule.path 类似, 多一个"_self"相对路径, 如果输入"_self", 表示规则处理的值与校验值路径相同, 如果输入"_self._parent._parent"表示寻找当前路径的父级的父级;

rule.type

  • equal 校验值与 rule.value 不相等时触发 action

  • unequal 校验值(基本类型)与 rule.value 相等时触发 action rule.value 支持基本类型值 和数组, 如果 rule.value 为数组, 校验值不存在于数组中时触发 action

  • type 校验值的 type 类型与 rule.value 不一致时触发 action type 支持 string, number, boolean, undefined, null, array, regexp, object, JSONString, emptyObj, notEmptyObject

  • JSONParse 校验值如果为字符串类型 JSON, 就对校验值进行 JSON.parse; 如果不是字符串类型 JSON,则触发 action

  • required 校验值为 null 或者 undefined 时触发 action

  • max 如果校验值大于 rule.value, 触发 action

  • range 如果校验值不为数字类型, 或超过 rule.value[0] rule.value[1]范围, 触发校验规则

  • minlength 如果校验值的 length 小于 rule.value 则触发校验规则

action.type

  • jump
    执行 window.location.href = action.value
  • jumpHash
    执行 window.location.hash = action.value
  • default
    action.path 补充默认值为 action.value
  • unset
    删除 action.path 路径上的值
  • defaultTimeStamp
    action.path 补充当前时间戳为默认值

isBreak: true

当 check 数组有多个规则时, 如果希望在触发了某个 action 后中断后续的校验, 可以在对应的 action 对象中增加 isBreak: true

// e.g.
    {
        apiName: "rights/getActivitySwitch",
        check: [
            {
                rule: {
                    path: "rs.curTime",
                    type: "type",
                    value: "number"
                },
                action: {
                    path: "_self",
                    type: "default",
                    value: null,
                    isBreak: true  //在这个action中增加了isBreak参数, 如果触发了action, 则这个check数组中后续的校验不会执行;
                }
            },
            {
                rule: {
                    path: "rs.doc_super_card_activity_time",
                    type: "JSONParse",
                    value: "JSONString"
                },
                action: {
                    path: "_self",
                    type: "jumpHash",
                    value: "other"
                }
            }
        ]
    }

Readme

Keywords

none

Package Sidebar

Install

npm i @jdplus/descaler

Weekly Downloads

6

Version

1.2.0

License

ISC

Unpacked Size

151 kB

Total Files

15

Last publish

Collaborators

  • wangzheng136
  • plus-components
  • shitou
  • iwander
  • cathy_huang