{"id":24493069,"url":"https://github.com/open-node/open-rest-helper-assert","last_synced_at":"2025-10-03T17:55:57.047Z","repository":{"id":57315130,"uuid":"66768660","full_name":"open-node/open-rest-helper-assert","owner":"open-node","description":null,"archived":false,"fork":false,"pushed_at":"2017-07-24T06:05:15.000Z","size":14,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-09T14:13:03.334Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/open-node.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-28T13:07:17.000Z","updated_at":"2017-01-23T11:31:13.000Z","dependencies_parsed_at":"2022-09-18T20:50:58.643Z","dependency_job_id":null,"html_url":"https://github.com/open-node/open-rest-helper-assert","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/open-node/open-rest-helper-assert","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-assert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-assert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-assert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-assert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/open-node","download_url":"https://codeload.github.com/open-node/open-rest-helper-assert/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-assert/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265262898,"owners_count":23736494,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-21T19:19:02.195Z","updated_at":"2025-10-03T17:55:52.011Z","avatar_url":"https://github.com/open-node.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# open-rest-helper-assert\n\nopen-rest 的 helper 插件，用来对某些值做断言\n\n[![Build status](https://api.travis-ci.org/open-node/open-rest-helper-assert.svg?branch=master)](https://travis-ci.org/open-node/open-rest-helper-assert)\n[![codecov](https://codecov.io/gh/open-node/open-rest-helper-assert/branch/master/graph/badge.svg)](https://codecov.io/gh/open-node/open-rest-helper-assert)\n\n# Node version\n\u003cpre\u003e \u003e= 6 \u003c/pre\u003e\n\n# Usage\n\n```bash\nnpm instsall open-rest-helper-assert --save\n```\n\n```js\nconst rest = require('open-rest');\nconst assert = require('open-rest-helper-assert')(rest);\n\n// assert Equivalent to rest.helper.assert\n```\n\n## assert.equal\n判断某两个值是否相等，相等则通过，不等则输出错误信息\n```js\n// keyPath 从req上获取某个值的路径，例如: 'params.id', 'hooks.user.name', 分别代表读取 req.params.id, req.hooks.user.name\n// obj 需要比较的值,有两种情况\n//    1. {path: 'params.id'} 代表值从 req.params.id 获取\n//    2. {fixed: 20} 代表固定的值\n// error 如果不想等报的错误，Error类型\n\nassert.equal(keyPath, obj, error);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nassert\n  .equal\n  .keyPath('hooks.user.role')\n  .obj({fixed: 'admin'})\n  .error(new restify.ForbiddenError('您没有权限执行该操作'))\n  .exec();\n```\n\n## assert.notEqual\n判断某两个值是否不等，不等则通过，相等则输出错误信息\n\n```js\nvar assert = require('open-rest-helper-assert');\n\n// keyPath 从req上获取某个值的路径，例如: 'params.id', 'hooks.user.name', 分别代表读取 req.params.id, req.hooks.user.name\n// obj 需要比较的值,有两种情况\n//    1. {path: 'params.id'} 代表值从 req.params.id 获取\n//    2. {fixed: 20} 代表固定的值\n// error 如果不想等报的错误，Error类型\n\nassert.notEqual(keyPath, obj, error);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nassert\n  .notEqual\n  .keyPath('hooks.user.role')\n  .obj({fixed: 'admin'})\n  .error(new restify.ForbiddenError('您没有权限执行该操作'))\n  .exec();\n```\n\n## assert.has\n判断某个值是否包含在另外一个Object, Array, Set, Map 中，包含则通过，不包含则输出错误信息\n\n```js\nvar assert = require('open-rest-helper-assert');\n\n// obj1 被包含的值\n//    1. {path: 'params.id'} 代表值从 req.params.id 获取\n//    2. {fixed: 20} 代表固定的值\n// obj2 判断的对象\n//    1. {path: 'params.id'} 代表值从 req.params.id 获取\n//    2. {fixed: 20} 代表固定的值\n// error 如果不想等报的错误，Error类型\n\nassert.has(obj1, obj2, error);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nassert\n  .has\n  .obj1({path: 'user.role'})\n  .obj2({fixed: new Set(['member', 'admin'])});\n  .error(new restify.ForbiddenError('您没有权限执行该操作'))\n  .exec();\n```\n\n## assert.notHas\n判断某个值是否包含在另外一个Object, Array, Set, Map 中，不包含则通过，包含则输出错误信息\n\n```js\nvar assert = require('open-rest-helper-assert');\n\n// obj1 被包含的值\n//    1. {path: 'params.id'} 代表值从 req.params.id 获取\n//    2. {fixed: 20} 代表固定的值\n// obj2 判断的对象\n//    1. {path: 'params.id'} 代表值从 req.params.id 获取\n//    2. {fixed: 20} 代表固定的值\n// error 如果不想等报的错误，Error类型\n\nassert.notHas(obj1, obj2, error);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nassert\n  .notHas\n  .obj1({path: 'user.role'})\n  .obj2({fixed: new Set(['member', 'admin'])});\n  .error(new restify.ForbiddenError('您没有权限执行该操作'))\n  .exec();\n```\n\n## assert.exists\n判断某个值是否存在，不存在则报错\n\n```js\nvar assert = require('open-rest-helper-assert');\n\n// keyPath 从req上获取某个值的路径，例如: 'params.id', 'hooks.user.name', 分别代表读取 req.params.id, req.hooks.user.name\n// error 如果不想等报的错误，Error类型, 默认值: new restify.NotFoundError('Resource not found.')\n\nassert.exists('hooks.users', error);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用方式\nassert\n  .exists\n  .keyPath('hooks.users')\n  .error(new restify.ForbiddenError('您没有权限执行该操作'))\n  .exec();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-node%2Fopen-rest-helper-assert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopen-node%2Fopen-rest-helper-assert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-node%2Fopen-rest-helper-assert/lists"}