找到你要的答案

Q:How to conciliate REST and JSONschema?

Q:如何调和的休息和jsonschema?

I'm starting a new project that consists in an Extjs 6 application with a pyramid/python backend.

Due to this architecture, the backend will only provide an RPC and won't serve any page directly. My implementation of such a thing is usually based on REST and will fit nicely this CRUD application.

Regarding data validation i would like to move from Colander/Peppercorn that i always found awkward to the simpler and more streamlined jsonschema.

The idea here would be to move all the parameters - minus the id contained in the url when is the case - of the various requests into a json body that could be easily handled by jsonschema.

The main problem here is that GET requests shouldn't have a body and i definitely want to put parameters in there (filters, pagination, etc).

There's probably some approach to REST or REST-like and JSONschema but i'm not able to find anything on the web.

Edit: someone mentioned the question about body in GET HTTP request. While putting a body in a GET HTTP request is somehow possible, it's in violation of part of HTTP 1.1 specification and therefore this is NOT the solution to this problem.

我开始一个新项目,包括在应用ExtJS 6有金字塔/ Python后端。

由于这种架构,后台只会提供一个RPC并不会提供任何页面直接。我对这种事的实现通常是基于休息,将很好地适应这个概念的应用。

关于我要离开漏勺/胡椒,我总觉得尴尬的更简单和更精简的jsonschema数据验证。

这里的想法是将所有参数减去ID包含在URL的案例时的各种请求转换为JSON的身体能被jsonschema容易处理。

这里的主要问题是,GET请求不应该有一个身体,我一定要把参数有(过滤器,分页,等)。

可能有一些方法来休息或休息喜欢JSONschema但我不能够在网络上找到什么。

编辑:有人提到的问题,身体在得到HTTP请求。而把尸体在获取HTTP请求是不可能的,它在HTTP 1.1规范的一部分,因此这不是违反了这个问题的解决方案。

answer1: 回答1:

If I understand you correctly, you want to use JSON Schema for input validation, but you are struggling to figure out how to validate query parameters with JSON Schema in a RESTful way.

Unfortunately, there isn't a definitive answer. JSON Schema just wasn't designed for that. Here are the options I have considered in my own work with REST and JSON Schema.

  1. Convert query parameters to JSON then validate against the schema
  2. Stuff your JSON into a query param and validate the value of that param. (i.e. /foo/1?params={"page": 2, "perPage": 10})
  3. Use POST instead of GET and stick your fingers in your ears when people tell you you are doing REST wrong. What do they know anyway.

I prefer option 1 because it is idiomatic HTTP.

Option 2 is probably the easiest to work with on the back-end, but it's dirty.

Option 3 is mostly a joke, but in all seriousness, there is nothing in REST or HTTP that says a POST can only be used for creation. In fact, it is the most flexible and versatile of the HTTP methods. Think of it like a factory that does something. That something could generate and store a new resource or just return it. If you are finding that you need to send a large number of query parameters, it's probably not really a simple GET. My rule of thumb is that if the result is inherently not cacheable, it's possible that a POST is more appropriate (or at least not inappropriate).

如果我理解正确的话,你想使用JSON模式输入验证,但你正努力找出如何用JSON架构在一个RESTful的方式验证查询参数。

不幸的是,没有一个明确的答案。JSON模式不是专为。这里的选项是我考虑了我自己的工作与休息、JSON模式。

  1. Convert query parameters to JSON then validate against the schema
  2. Stuff your JSON into a query param and validate the value of that param. (i.e. /foo/1?params={"page": 2, "perPage": 10})
  3. Use POST instead of GET and stick your fingers in your ears when people tell you you are doing REST wrong. What do they know anyway.

我宁愿选择1是因为它是地道的HTTP。

选项2可能是最容易与后端工作,但它的脏。

选项3是一个笑话,但在所有的严重性,在休息或HTTP什么说后只能用于创作。事实上,它是最灵活和通用的HTTP方法。想它像一个工厂做某事。可以生成和存储新资源或返回它。如果你发现你需要发送大量的查询参数,这可能不是一个简单的获取。我的经验法则是,如果结果本质上是不可缓存的,它可能是一个更合适的岗位是(或至少不合适)。

answer2: 回答2:

I don't see why you need to do anything specific. In Pylons request.GET returns a dictionary (actually a MultiDict, but basically the same thing) of the parameters in the querystring. You can easily convert this to JSON or pass it directly to the schema validation.

我不明白你为什么要做任何具体的事情。在塔request.get返回一个字典(实际上是一个multidict,但基本上相同)在QueryString参数。你可以很容易地转换为JSON或者通过它直接架构验证。

answer3: 回答3:

The cleanest would be to put the JSON string in a single query parameter. If you have a JSON Schema to validate it, the client could use the same JSON Schema, and would therefor already have the data in JSON form.

http://example.net/some/model/1?query={"foo":1,"bar":["baz","qux"]}

If you for some reason dislike having JSON in the query, you could use a standard for converting query parameters. There is no universally agreed-upon specification for param strings, but there are a few practices, depending on language and framework/library.

Example: http://example.net/some/model/1?foo=1,bar[]=baz,bar[]=qux

That might look cleaner to a new user, but will be more difficult if the client uses a lib/language that would querify JSON differently.

最干净的将JSON字符串在一个单一的查询参数。如果你有一个JSON Schema验证,客户端可以使用相同的JSON的图式,并会为此已经在JSON格式的数据。

http://example.net/some/model/1?查询= {“foo”:1,“酒吧”:[“巴兹”、“qux”] }

如果你因为某些原因不喜欢查询具有JSON,你可以使用查询参数的标准转换。有没有一种公认的参数字符串的规范,但也有一些做法,取决于语言和框架/库。

例如:http://example.net/some/model/1?foo = 1条[ ] = [ ] = qux巴兹,酒吧

看起来清洁的新用户,但如果客户端使用一个lib /语言,querify JSON不同更难。

python  rest  extjs  pyramid  jsonschema