javascript - 如何通过http进行jsonschemavalidation?

标签 javascript node.js jsonschema

我已经定义了一个restservice并尝试使用文书工作进行jsonschema验证:

var _ = require('underscore');
var validate = require('isvalid-express');
var paperwork=require('paperwork');

module.exports = function (app) {

    app.post('/myroute', paperwork({
        username: /[a-z0-9]+/,
        password: String,
        age: Number,
        interests: [String],
        jobs: [{
            company: String,
            role: String
        }]
    }, function (req, res) {
        // ...
    }));


};

这是我在 postman 中将其发布到我的路线的请求:

{
  username: 'brucewayne',
  password: 'iambatman',
  age: 36,
  interests: ['Climbing', 'CQC', 'Cosplay'],
  jobs: [{
    company: 'Wayne Inc.',
    role: 'CEO'
  }]
}

但是它会抛出错误:

TypeError: undefined is not a function
    at module.exports (c:\heroku\newher\node_codechallenge\node_modules\paperwork\paperwork.js:129:5)
    at module.exports (c:\heroku\newher\node_codechallenge\app\testroutes.js:12:26)
    at Object.<anonymous> (c:\heroku\newher\node_codechallenge\test.js:16:31)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain [as _onTimeout] (module.js:497:10)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

在 Node.js 中对 http 进行 json 模式验证的最佳方法是什么?

最佳答案

您可以考虑使用JSON-schema standard而不是文书工作使用的非标准架构定义。

有很多 JavaScript JSON Schema 验证器,这里是基准:https://github.com/ebdrup/json-schema-benchmark

关于javascript - 如何通过http进行jsonschemavalidation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931898/

相关文章:

javascript - 使用 date-fns 获取两个范围内的重叠月份

java - NoClassDefFoundError com/fasterxml/jackson/core/TreeNode -- json 模式验证

javascript - JSON 架构 + JSON 编辑器 : Table with togglable columns

json - JSON 模式中的变量属性

javascript - 通过内联键盘启动 Telegram Bot

javascript - HTML 元素到 jQuery 元素

javascript - 在遍历两个数组时查找共同值

node.js - ElasticSearch JS查询返回所有文档而不是过滤

node.js - 在 Node JS 中从不同的 'sessions' 读取/写入相同的文件内容是否有任何风险?

javascript - rails : Delete nested attribute while updating entry