简体   繁体   English

REST设计-验证非数字输入到数字字段?

[英]REST design - Validating non-numeric input, to a numeric field?

Is there a general practice to validating REST requests, where the data is not the correct datatype? 在数据不是正确数据类型的情况下,是否存在验证REST请求的一般做法? (Eg: submitting "bad value" to a int ) (例如:将“差值”提交给int)

For example, if we have a "Shop item" entity, with mixed datatypes: 例如,如果我们有一个“商店商品”实体,具有混合数据类型:

id: int
title: string
price: decimal
expiry: datetime

And we test the 'add' operation on the REST endpoint with a response with bad data: 然后,我们使用错误数据的响应测试REST端点上的“添加”操作:

{
    "title": "New item"
    "price": "bad value"
    "expiry": "Not a date"
}

Question: Is there a general practice to replying to requests with bad data? 问题:是否有一般的做法来答复包含不良数据的请求?

Specifically - Do I need the error code/description to tell them that the field data is invalid? 具体来说-我需要错误代码/描述来告诉他们字段数据无效吗? Would it be appropriate to just fall back to a 'mandatory field required' message? 退回“必填字段”消息是否合适? (Either scenario is going back as a HTTP 400) (这两种情况都可以追溯到HTTP 400)

Also, WebAPI / JSON.NET: Behind the scenes, I happen to be using Microsoft's WebAPI/JSON.Net. 另外,WebAPI / JSON.NET:在幕后,我碰巧正在使用Microsoft的WebAPI / JSON.Net。 With the automatic deserialisation to an entity, correct data will map to their property/types just fine. 通过自动反序列化到实体,正确的数据将映射到其属性/类型。 I've also made all properties on the input models to be nullable, so we can validate for missing inputs. 我还使输入模型上的所有属性都可以为空,因此我们可以验证缺少的输入。 So bad data will be treated as a 'missing required field' validation. 因此,不良数据将被视为“缺少必填字段”验证。

Dropping back to making all properties a string, so we can further validate input seems a step backwards... 回到使所有属性成为字符串,以便我们进一步验证输入似乎倒退了一步……

I don't know what the best practice is (REST leaves lots of room for interpretation and people chose to do it in different ways) but, personally, I would return a 400 Bad Request . 我不知道最佳实践是什么(REST留下了很多解释的空间,人们选择了不同的方式进行解释),但是我个人会返回400 Bad Request If the request was invalid (eg invalid string value for a decimal or datetime field) or cannot be otherwise served because of some validation error then from my point of view it's a bad request. 如果请求无效(例如,十进制或日期时间字段的字符串值无效)或由于某些验证错误而无法以其他方式提供服务,那么从我的角度来看,这是一个错误的请求。

Inside the response body I would then have a representation of an error that contains: 在响应体内,我将具有以下错误的表示形式:

  • the error code , something like 123 ; 错误code ,如123 ;
  • the error message something like Invalid value for field , Validation failed etc (maybe internationalized depending on the Accept-Language client header). 错误message ,例如Invalid value for field ,“ Validation failed等(可能会国际化,具体取决于“ Accept-Language客户端标头)。 This might be an array of field / message objects if you want to specifically tell where the issues are. 如果您要明确指出问题出在哪里,则可能是field / message对象的数组。

Also, I would not make all the entity properties a string (it defeats the purpose of having an entity to bind to in the first place) and would not make them all nullable to validate for missing values (I would do the opposite and use [Required] ). 另外,我不会将所有实体属性都设置为字符串(这违背了让实体首先绑定到的目的),也不会使它们全部为空以验证缺失值(我会做相反的事情并使用[Required] )。

I'm not very familiar with Microsoft's WebAPI but you should be able to intervene in the validation process. 我对Microsoft的WebAPI不太熟悉,但是您应该可以介入验证过程。 For application validations you rely on ModelState.IsValid to return a HttpStatusCode.BadRequest while for plain invalid data you customize the parameter binding maybe with some HTTP message handler of some sort. 对于应用程序验证,您依赖ModelState.IsValid返回HttpStatusCode.BadRequest而对于普通无效数据,则可以使用某种HTTP消息处理程序自定义参数绑定

Take a look at http://soabits.blogspot.dk/2013/05/error-handling-considerations-and-best.html which goes into details about good error handling in APIs. 看看http://soabits.blogspot.dk/2013/05/error-handling-considerations-and-best.html ,其中详细介绍了API中良好的错误处理。 You can also search StackOverflow for "error handling [rest]" which gives a good set of results. 您也可以在StackOverflow上搜索“错误处理[休息]”,这将提供很好的结果。

This is how we handle it in our API: 这就是我们在API中处理它的方式:

{
  "code": 422,
  "errors": [
    {
      "field": "title",
      "message": "can't be blank"
    },
    {
      "field": "price",
      "message": "must be greater than 0"
    },
    {
      "field": "expiry",
      "message": "wrong date format"
    }
  ],
  "message": "Validation failed"
}

The format of the error messages stays the same for any type of errors. 对于任何类型的错误,错误消息的格式均保持不变。 What can change is the error Array (it might be empty) and the root message. 可以更改的是错误Array(它可能为空)和根消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM