首页 > 其他分享 >利用NewtonSoft.Json的JsonScheme校验数据格式

利用NewtonSoft.Json的JsonScheme校验数据格式

时间:2023-05-09 23:34:31浏览次数:48  
标签:NewtonSoft string true required JsonScheme maxLength 数据格式 null type

推送的报文信息,需要校验格式是否正确,必传的是否传了,字符串长度是否超长。之前是把报文反序列化为类后,一个字段一个字段进行校验。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

相关文章

  • 查找某物质的热力学数据并转换为chemkin数据格式
    在使用CHEMKIN时,其库中的热力学数据有时无法满足需求,需要自行查询物质的热力学数据并编写热力学文件,本文将以B2O3为例子描述查找到转换热力学数据的整个过程。查找热力学数据进入NIST的热力学数据库网站http://webbook.nist.gov/chemistry/form-ser.html输入化学式,使用Calo......
  • opendrive数据格式解析思维导图 , opendrive高精地图是自动驾驶领域使用最为广泛的开源
    opendrive数据格式解析思维导图,opendrive高精地图是自动驾驶领域使用最为广泛的开源高精地图标准级地图格式。本思维导图将详细剖开高精路网地图内部的数据格式,涵盖:道路、车道、车道段、交叉口等相关名词及其属性、作用、链接关系等参数的解析。内容比较全面,希望对高精地图进行......
  • python学习笔记8(json数据格式、pycharts简单折线图)
    1.jsonjson是一种轻量级的数据交互格式,可以以json指定的格式去组织和封装数据;json本质上是一个带有特定格式的字符串;json负责不同编程语言中的数据传递和交互;1.1python数据与json数据相互转化引入json模块importjson1.1.1python数据转json数据importjson#python列表......
  • HTTP---响应数据格式
       ......
  • SpringMvc 数据格式化 & 数据校验
    数据格式化概述对属性对象的输入/输出进行格式化,从其本质上讲依然属于“类型转换”的范畴。Spring在格式化模块中定义了一个实现ConversionService接口的FormattingConversionService实现类,该实现类扩展了GenericConversionService,因此它既具有类型转换的功能,又具有格式......
  • HTTP请求数据格式及响应数据格式
    一、什么是HTTP概念:HyperTextTransferProtocol,超文本传输协议,规定了浏览器和服务器之间数据传输的规则  HTTP协议特点:1、基于TCP协议:面向连接,安全2、基于请求-响应模型的:一次请求对应一次响应3、HTTP协议是无状态的协议:对应事务处理没有记忆能力。每次请求-响应都是独......
  • 使用Newtonsoft.Json在Net6中设置时间格式(含T/不含T)
    实例一:JsonSerializerSettingsjsonSettings=newJsonSerializerSettings{DateFormatString="yyyy-MM-ddHH:mm:ss.fff",};Modelmodel=newModel{CreatedAt=DateTime.UtcNow};......
  • JWT控制返回数据格式
    1.只使用JWT的局部校验,不加权限控制的不会限制用户登录  2.JWT的局部校验配合权限控制提供用户登录访问限制  2_1.JWT的局部校验配合权限控制提供用户登录访问限制  3.重写jwt_response_payload_handler方法,创建utils文件,自定义返回格式  4.重写jwt_respo......
  • 微信小程序使用 wxs 对模板数据格式化展示
    在小程序页面展示时,对时间、金额进行格式化处理。但是每次在js文件中处理,并setData感觉无比麻烦。是否可以直接在wxml模板文件中进行处理。正好发现了微信小程序wxs,完全满足需求。微信小程序wxs使用场景WXS(WeiXinScript)是微信创造的一套脚本语言,虽然看起来很JS异常......
  • influxDB数据格式
    数据的行协议格式参考说明<measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]]<field_key>=<field_value>[,<field_key>=<field_value>][<timestamp>]对......