javascript express.js - 最好的 POST json 模式验证器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19117233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:26:18  来源:igfitidea点击:

express.js - best POST json schema validator

javascriptjsonvalidationexpressschema

提问by Kosmetika

I'm searching for a module to validate POST json requests in my Express.jsapplication.

我正在寻找一个模块来验证我的Express.js应用程序中的POST json 请求。

What json schema module do you use in your node.js apps?

您在 node.js 应用程序中使用什么 json 模式模块?

I assume node-validator (https://github.com/chriso/node-validator) is not an option here because it works only with strings.

我认为 node-validator ( https://github.com/chriso/node-validator) 在这里不是一个选项,因为它只适用于字符串。

采纳答案by Trenskow

I made this - if you are still interested: https://npmjs.org/package/isvalid

我做了这个 - 如果你仍然感兴趣:https: //npmjs.org/package/isvalid

回答by Adrian Adkison

Hi I recently wrote express-jsonschema. The main differences with the other validators out there are:

嗨,我最近写了express-jsonschema。与其他验证器的主要区别在于:

  1. You use the standard json schemasfor validation. If you have written other server side languages you probably have used them before.
  2. It doesnt control how your application responds to invalid data. It does give you an opportunity to hook in and respond however you want. In my experience this is something that is nice to control.
  1. 您使用标准的json 模式进行验证。如果您编写过其他服务器端语言,您可能以前使用过它们。
  2. 它不控制您的应用程序如何响应无效数据。它确实让您有机会加入并响应您想要的任何方式。根据我的经验,这是很好控制的事情。

express-schema-validator, express-validate, and Paperworkare all great. They each have their own unique syntax for declaring schemas and also control how your app responds to invalid data (i.e. status code and data structure).

express-schema-validatorexpress-validatePaperwork都很棒。它们每个都有自己独特的语法来声明模式,并且还控制您的应用程序如何响应无效数据(即状态代码和数据结构)。

Good luck!

祝你好运!

回答by Laurent Perrin

I made Paperwork, which is a very simple solution for JSON validation. You can do things like:

我制作了Paperwork,这是一个非常简单的 JSON 验证解决方案。您可以执行以下操作:

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

It will validate:

它将验证:

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

Or will silently responds a 400 error with information about what's wrong. Check the doc to do more advanced usages.

或者将静默响应 400 错误并提供有关错误的信息。检查文档以执行更高级的用法。

回答by Alex Povar

There are a lot of options for those who choose JSON Schema validation rules. Good libs comparison here: https://github.com/ebdrup/json-schema-benchmark

对于选择 JSON Schema 验证规则的人来说,有很多选择。好的库比较在这里:https: //github.com/ebdrup/json-schema-benchmark