推送的报文信息,需要校验格式是否正确,必传的是否传了,字符串长度是否超长。之前是把报文反序列化为类后,一个字段一个字段进行校验。JsonSchema可以一次性校验
校验对象的格式,如下:
{ "type": "object", "properties": { "soId": { "type": "string", "required": true, "minLength": 1, "maxLength": 100 }, "type": { "type": "string", "required": true, "minLength": 1, "maxLength": 280 }, "shopStatus": { "type": [ "string", "null" ] }, "outerAsId": { "type": "string", "required": true, "minLength": 1, "maxLength": 500 }, "goodStatus": { "type": "string", "required": true, "minLength": 1, "maxLength": 20 }, "totalAmount": { "type": "number", "required": true }, "receiverAddress": { "type": [ "string", "null" ], "maxLength": 200 }, "receiverName": { "type": [ "string", "null" ], "maxLength": 50 } "items": { "type": "array", "required": true, "minItems": 1, "items": { "properties": { "skuId": { "type": "string", "required": true, "minLength": 1, "maxLength": 64 }, "pic": { "type": [ "string", "null" ], "maxLength": 300 }, "amount": { "type": "number", "required": true }, "qty": { "type": "integer", "required": true }, "name": { "type": [ "null", "string" ], "maxLength": 100 }, "type": { "type": "string", "required": true, "minLength": 1, "maxLength": 10 }, "refundStatus": { "type": [ "null", "string" ] } } } } } }
校验数组的格式,如下:
{ "type": "array", "minItems": 1, "maxItems": 100, "items": { "properties": { "shopCode": { "type": "string", "required": true }, "shopType": { "type": "string", "required": true }, "soId": { "type": "string", "required": true, "minLength": 1, "maxLength": 850 }, "orderDate": { "type": "string", "required": true, "format": "date-time" }, "shopStatus": { "type": "string", "required": true, "minLength": 1, "maxLength": 120 }, "receiverTown": { "type": [ "string", "null" ], "maxLength": 250 }, "receiverAddress": { "type": "string", "required": false, "minLength": 1, "maxLength": 200 }, "questionDesc": { "type": [ "string", "null" ], "maxLength": 550 }, "sellerFlag": { "type": [ "number", "null" ], "enum": [ 1, 2, 3, 4, 5, null ] }, "items": { "type": "array", "required": true, "minItems": 1, "items": { "properties": { "skuId": { "type": "string", "required": true, "minLength": 1, "maxLength": 64 },"iId": { "type": [ "string", "null" ], "maxLength": 64 },"propertiesValue": { "type": [ "string", "null" ], "maxLength": 100 }, "amount": { "type": "number", "required": true }, "basePrice": { "type": [ "number", "null" ], "required": false }, "qty": { "type": "integer", "required": true, "minimum": 1 }, "isGift": { "type": "boolean", "required": true },"refundStatus": { "type": [ "null", "string" ], "maxLength": 40 }, "outerOiId": { "type": "string", "required": true, "minLength": 1, "maxLength": 50 } } } }, "pay": { "type": [ "object", "null" ], "properties": { "outerPayId": { "type": "string", "required": true, "minLength": 1, "maxLength": 50 }, "payDate": { "type": "string", "format": "date-time", "required": true }, "payment": { "type": "string", "required": true, "minLength": 1, "maxLength": 20 }, "buyerAccount": { "type": [ "string", "null" ], "required": true, "minLength": 1, "maxLength": 1200 } } } } } }
使用方法
1.获取scheme的对象
string OrderJsonSchema = "{}"; //上面的json字符串
private static JsonSchema _orderSchema = Newtonsoft.Json.Schema.JsonSchema.Parse(OrderJsonSchema)
2.把要检查的对象转换为JToken
var orderDataJToken = JToken.FromObject(order);
3.用scheme校验数据
IList<string> errorList = new List<string>(); if (!orderDataJToken.IsValid(schema, out errorList)) { var failContent = string.Format("参数不符合规范,请检查传入参数!报文异常提示:[{0}]", string.Join(",", errorList)); }
标签:NewtonSoft,string,true,required,JsonScheme,maxLength,数据格式,null,type From: https://www.cnblogs.com/kingsmart/p/17304133.html