entry-check

0.0.16 • Public • Published

entry-check

小程序页面入参检查

NPM

standard-readme compliant

Install

npm i entry-check

Usage

ES6

import EC from 'entry-check';

const [err] = await EC(config, opts);
if (err) {
  // 处理检查报错
  return;
}

Extra Sections

支持检查的数据类型

[
  "null",
  "undefined",
  "string",
  "number",
  "boolean",
  "array",
  "object",
]

小程序使用场景

  • 页面入参 onLoad
  • 页面间通信接口 wx.navigateTo({ events });
  • 小程序入参 onLaunch / onShow

API

function entryCheck(
  [
    [
      key,        // 参数名
      type,       // 参数类型
      required,   // 是否必须
    ],
    // ...
  ],
  {},             // 页面入参对象
) {
  // return Promise<[Error || null, result[] || null]>;
}

Example

  // 入参数据
  const entryData = {
    id: 12,
    from: 'share',
  };
  // 入参检查配置
  const checkConfig = [
    ['id', 'number', true],
    ['from', 'number'],
  ];
  const [err, logResult] = await entryCheck(checkConfig, entryData);
  // 异常处理
  if (err) {
    const defaultStyle = chalk.inverse.bold;
    console.log(defaultStyle.redBright('终止信息:'), err.message);
    console.log(defaultStyle.blueBright('上报错误结果:'), JSON.stringify(logResult, null, 2));
    console.log(defaultStyle.yellowBright('检查配置:'), JSON.stringify(checkConfig, null, 2));
    return;
  }
  console.log('入参检查通过');

  // 终止信息: from 类型不匹配
  // 上报错误结果: {
  //   "from": [
  //     false,
  //     "share",
  //     "from 类型不匹配"
  //   ]
  // }
  // 检查配置: [
  //   [
  //     "id",
  //     "number",
  //     true
  //   ],
  //   [
  //     "from",
  //     "number"
  //   ]
  // ]

License

ISC

Package Sidebar

Install

npm i entry-check

Weekly Downloads

4

Version

0.0.16

License

ISC

Unpacked Size

19.9 kB

Total Files

9

Last publish

Collaborators

  • willow_ramirez