npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

open-rest-helper-assert

v1.1.0

Published

Controller helper assert for open-rest or restify, Used to get instance from Model

Downloads

2

Readme

open-rest-helper-assert

open-rest 的 helper 插件,用来对某些值做断言

Build status codecov

Node version

Usage

npm instsall open-rest-helper-assert --save
const rest = require('open-rest');
const assert = require('open-rest-helper-assert')(rest);

// assert Equivalent to rest.helper.assert

assert.equal

判断某两个值是否相等,相等则通过,不等则输出错误信息

// keyPath 从req上获取某个值的路径,例如: 'params.id', 'hooks.user.name', 分别代表读取 req.params.id, req.hooks.user.name
// obj 需要比较的值,有两种情况
//    1. {path: 'params.id'} 代表值从 req.params.id 获取
//    2. {fixed: 20} 代表固定的值
// error 如果不想等报的错误,Error类型

assert.equal(keyPath, obj, error);

// return
// function(req, res, next) { ... };

//or 链式调用
assert
  .equal
  .keyPath('hooks.user.role')
  .obj({fixed: 'admin'})
  .error(new restify.ForbiddenError('您没有权限执行该操作'))
  .exec();

assert.notEqual

判断某两个值是否不等,不等则通过,相等则输出错误信息

var assert = require('open-rest-helper-assert');

// keyPath 从req上获取某个值的路径,例如: 'params.id', 'hooks.user.name', 分别代表读取 req.params.id, req.hooks.user.name
// obj 需要比较的值,有两种情况
//    1. {path: 'params.id'} 代表值从 req.params.id 获取
//    2. {fixed: 20} 代表固定的值
// error 如果不想等报的错误,Error类型

assert.notEqual(keyPath, obj, error);

// return
// function(req, res, next) { ... };

//or 链式调用
assert
  .notEqual
  .keyPath('hooks.user.role')
  .obj({fixed: 'admin'})
  .error(new restify.ForbiddenError('您没有权限执行该操作'))
  .exec();

assert.has

判断某个值是否包含在另外一个Object, Array, Set, Map 中,包含则通过,不包含则输出错误信息

var assert = require('open-rest-helper-assert');

// obj1 被包含的值
//    1. {path: 'params.id'} 代表值从 req.params.id 获取
//    2. {fixed: 20} 代表固定的值
// obj2 判断的对象
//    1. {path: 'params.id'} 代表值从 req.params.id 获取
//    2. {fixed: 20} 代表固定的值
// error 如果不想等报的错误,Error类型

assert.has(obj1, obj2, error);

// return
// function(req, res, next) { ... };

//or 链式调用
assert
  .has
  .obj1({path: 'user.role'})
  .obj2({fixed: new Set(['member', 'admin'])});
  .error(new restify.ForbiddenError('您没有权限执行该操作'))
  .exec();

assert.notHas

判断某个值是否包含在另外一个Object, Array, Set, Map 中,不包含则通过,包含则输出错误信息

var assert = require('open-rest-helper-assert');

// obj1 被包含的值
//    1. {path: 'params.id'} 代表值从 req.params.id 获取
//    2. {fixed: 20} 代表固定的值
// obj2 判断的对象
//    1. {path: 'params.id'} 代表值从 req.params.id 获取
//    2. {fixed: 20} 代表固定的值
// error 如果不想等报的错误,Error类型

assert.notHas(obj1, obj2, error);

// return
// function(req, res, next) { ... };

//or 链式调用
assert
  .notHas
  .obj1({path: 'user.role'})
  .obj2({fixed: new Set(['member', 'admin'])});
  .error(new restify.ForbiddenError('您没有权限执行该操作'))
  .exec();

assert.exists

判断某个值是否存在,不存在则报错

var assert = require('open-rest-helper-assert');

// keyPath 从req上获取某个值的路径,例如: 'params.id', 'hooks.user.name', 分别代表读取 req.params.id, req.hooks.user.name
// error 如果不想等报的错误,Error类型, 默认值: new restify.NotFoundError('Resource not found.')

assert.exists('hooks.users', error);

// return
// function(req, res, next) { ... };

//or 链式调用方式
assert
  .exists
  .keyPath('hooks.users')
  .error(new restify.ForbiddenError('您没有权限执行该操作'))
  .exec();