hojs-schema

0.0.3 • Public • Published

NPM version build status Test coverage David deps node version npm download npm license

hojs-schema

API参数检查

特性

  • 参数数据类型检查
  • 参数自动格式化
  • 参数默认值
  • 自定义数据类型
  • 自动生成文档(未完成)

安装

$ npm install hojs-schema --save

使用

'use strict';
 
const { Manager, Schema } = require('hojs-schema');
 
// 创建一个 Manager 实例
const manager = new Manager();
 
// 注册自定义类型
manager.registerType('password', {
  checker(data) {
    // password 类型长度为 6 至 100 字符串
    return typeof data === 'string' && data.length > 6 && data.length < 100;
  },
});
 
// 定义检查模式
manager.registerSchema('user', {
  params: {
    // email 参数
    email: {
      // email 是内置类型
      type: 'email',
    },
    // password 参数
    password: {
      type: 'password',
    },
    // born 参数
    born: {
      type: 'date',
    },
    // age 参数
    age: {
      type: 'integer',
      // 定义 integer 类型的属性
      meta: {
        min: 0,
        max: 100,
      },
    },
    // signature 参数
    signature: {
      type: 'trim_string',
    },
  },
});
 
// 检查并格式化数据,如果出错会抛出相应的异常
try {
  console.log(manager.getSchema('user').check({
    email: 'me@ucdok.com',
    password: '123456789',
    born: '2016-10-30',
    age: 18,
    signature: ' 哈哈哈哈 ',
  }));
  /*
输出:
{ email: 'me@ucdok.com',
  password: '123456789',
  born: 2016-10-30T00:00:00.000Z,
  age: 18,
  signature: '哈哈哈哈' }
  */
} catch (err) {
  return const.error(err);
}

在 Express 中使用

'use strict';
 
const { Manager, Schema } = require('hojs-schema');
const express = require('express');
 
const manager = new Manager();
const app = express();
 
// 注册的模式名称格式为: METHOD path
manager.registerSchema('GET /test', {
  params: {
    message: { type: 'string' },
  },
});
 
// 使用中间件
app.use(manager.getMiddleware({
  type: 'express',
  onInput(req) {
    // 根据不同的请求方法合并输入数据
    return manager.utils.merge(req.query || {}, req.body || {}, req.files || {}, req.params || {});
  },
  onSuccess(data, req, res, next) {
    // 验证成功,存储验证并格式化后的数据
    req.data = data;
    next();
  },
  onFail(err, req, res, next) {
    // 验证出错时返回数据
    res.json({
      status: 'error',
      message: err.message,
      parameter: err.parameter,
    });
  },
}));
 
// 使用
app.get('/test', function (req, res, next) {
  res.json({
    status: 'success',
    message: `Hello, ${ req..message }`,
  });
});

License

MIT License

Copyright (c) 2016 Zongmin Lei <leizongmin@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Package Sidebar

Install

npm i hojs-schema

Weekly Downloads

4

Version

0.0.3

License

MIT

Last publish

Collaborators

  • leizongmin