接口测试 & Json Schema,2024年最新史上最通俗计算机网络分层详解

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注软件测试)
img

正文

multipleOf关键字可以校验数据是否为给定条件数据的整数倍。

{
“type”: “number”,
“multipleOf”: 10
}

如上定义,数据20、230符合约束,232则不符合。

2、范围匹配

Json Schema提供了4个关键字来支持对数值取值范围的校验。

关键字作用
minimum取值 ≥
exclusiveMinimum取值 >
maximum取值 ≤
exclusiveMaximum取值 <

按照以下规则,数据0、20、99符合约定,-1、100不符合。

{
“type”: “number”,
“minimum”: 0,
“exclusiveMaximum”: 100
}

C、object

object是Json的一种基本结构,约束数据必须为对象类型。

{
“type”: “object”
}

1、properties关键字

properties是描述对象属性约束的关键字,它的取值也必须是个对象,而且也必须符合Json Schema模式。即相当于把对应的校验对象单独拿出,也可使用properties的取值约定进行校验。

{
“properties”: {
“price”: {
“type”: “number”
}
}
}

2、propertyNames关键字

propertyNames关键字用于约束属性名称(键名),并且可支持正则表达式。

{
“type”: “object”,
“propertyNames”: {
“pattern”: “1*$”
}
}

3、additionalProperties关键字

additionalProperties关键字和properties关键字配合使用,取值可以是boolean型或者object 。作用是对限制properties定义的属性进行限制。

当additionalProperties取值为boolean型false时,表示除properties中已定义的属性,不允许出现额外的属性。

而当它为object时, 同时需要符合模式描述,用于限制除properties中定义的属性外,额外的属性必须符合该关键字约束。

{
“type”: “object”,
“properties”: {
“number”: {
“type”: “number”
},
“vegetables”: {
“type”: “string”
},
“fruits”: {
“type”: “string”,
“enum”: [
“pear”,
“watermelon”,
“lemon”
]
}
},
“additionalProperties”: {
“type”: “string”
}
}

4、required关键字

required关键字也是配合properties关键字使用,用于约定properties定义中必须包含的属性,取值为一个属性名称的数组列表。

{
“type”: “object”,
“properties”: {
“name”: {
“type”: “string”
},
“email”: {
“type”: “string”
},
“address”: {
“type”: “string”
},
“telephone”: {
“type”: “string”
}
},
“required”: [
“name”,
“email”
]
}

以上约定,表明对象数据中必须包含name和email两个属性,其他的则不作要求。

5、限定对象长度的关键字

minProperties和maxProperties用来约束object对象长度的关键字,取值需为正整数,且maxProperties应大于minProperties。

{
“type”: “object”,
“minProperties”: 2,
“maxProperties”: 4
}

以上约定限制对应的object属性至少有2个,不超过4个。

6、对象数据依赖关键字

dependencies关键字来定义对象属性间的依赖关系。

{
“type”: “object”,
“properties”: {
“name”: {
“type”: “string”
},
“credit_card”: {
“type”: “number”
},
“billing_address”: {
“type”: “string”
}
},
“required”: [
“name”
],
“dependencies”: {
“credit_card”: [
“billing_address”
]
}
}

以上约束表示,当存在credit_card属性时,也必须存在billing_address属性。

D、array

array是Json的另一种基本数据结构。

1、items关键字

1).约束array类型的主要关键字是items,用于约束数组中每项的取值约束。

{
“type”: “array”,
“items”: {
“type”: “number”
}
}

以上约束了数组每项取值必须为整型。

2).items关键字也可以按数组项顺序逐项对数组进行约定。

{
“type”: “array”,
“items”: [
{
“type”: “number”
},
{
“type”: “string”,
“enum”: [
“different”,
“same”
]
},
{
“type”: “object”
}
]
}

items关键字会对数组中的每一项都进行严格约束,以下Json满足约束条件。

[
3,
“different”,
{
“types”: “of values”
}
]

2、contains关键字(draft 06)

{
“type”: “array”,
“contains”: {
“type”: “number”
}
}

以上约束,会约束数组中只需要包含至少一个符合条件的项即可。

3、additionalItems关键字

additionalItems关键字约束了数组是否允许额外的数组项。

{
“type”: “array”,
“items”: [
{
“type”: “number”
},
{
“type”: “string”
},
{
“type”: “string”,
“enum”: [
“Street”,
“Avenue”,
“Boulevard”
]
},
{
“type”: “string”,
“enum”: [
“NW”,
“NE”,
“SW”,
“SE”
]
}
],
“additionalItems”: false
}

以上约束,约束数组不允许出现第5个数据项。但符合条件的3项数组是符合定义的。

4、数组长度校验

{
“type”: “array”,
“minItems”: 2,
“maxItems”: 3
}

以上约束,约束通过minItems、maxItems可以定义数组的长度,下例定义数组长度至少为2, 不超过3。

5、唯一性校验

uniqueItems是约束数组唯一性的关键字,取值为boolean型,为true时可以约束数组对象中的每一项唯一。

{
“type”: “array”,
“uniqueItems”: true
}

E、boolean

布尔型约束通过type关键字约束,取值为true或false。

{
“type”: “boolean”
}

F、null

null型约束通过type关键字约束,取值只可为null。

{
“type”: “null”
}

四、Json Schema通用关键字

A、描述性关键字

描述性关键字在Json Schema中并不会产生实际的约束,但是对于阅读和理解Json Schema中相关约束有非常大的帮助。可以理解为Json Schema对于Json数据的说明文档。

描述性关键字主要包括:

  • title:描述对象的标题
  • Description:对数据进行说明描述
  • default:所描述对象的默认值
  • example:从draft 06支持的关键字,提供当前约束的示例

{
“title”: “Match anything”,
“description”: “This is a schema that matches anything.”,
“default”: “Default value”,
“examples”: [
“Anything”,
4035
]
}

B、枚举关键字

枚举关键字enum是个应用比较广泛的Json Schema关键字,一般用于约束数据在枚举范围内进行取值。

{
“type”: “string”,
“enum”: [
“red”,
“amber”,
“green”
]
}

C、const常量关键字

const常量关键字,用于约束数据为固定取值。

{
“const”: “United States of America”
}

D、聚合关键字

聚合关键字是Json Schema中对多个约束进行聚合处理的关键字。

1、allOf

待校验的数据对象满足allOf关键字中给出的所有约束时,才算符合要求。

{
“allOf”: [
{
“type”: “string”
},
{
“maxLength”: 5
}
]
}

2、anyOf

待校验的数据对象满足anyOf关键字中给出的任一约束时,才算符合要求。

{
“anyOf”: [
{
“type”: “string”
},
{
“type”: “number”
}
]
}

3、oneOf

oneOf关键字约束待校验的数据正好符合约束条件中的一项。

{
“oneOf”: [
{
“type”: “number”,
“multipleOf”: 5
},
{
“type”: “number”,
“multipleOf”: 3
}
]
}

4、not

not关键字约束待校验的数据不是给出的约束条件。

{
“not”: {
“type”: “string”
}
}

E、条件关键字

从draft 07开始可以支持条件关键字if、then、else可以给出一些约束的互相依赖关系。

{
“type”: “object”,
“properties”: {
“street_address”: {
“type”: “string”
},
“country”: {
“enum”: [
“United States of America”,
“Canada”
]
}
},
“if”: {
“properties”: {
“country”: {
“const”: “United States of America”
}
}
},
“then”: {
“properties”: {
“postal_code”: {
“pattern”: “[0-9]{5}(-[0-9]{4})?”
}
}
},
“else”: {
“properties”: {
“postal_code”: {
“pattern”: “[A-Z][0-9][A-Z] [0-9][A-Z][0-9]”
}
}
}
}

以上约束,约束当country是美国时,对应的区号约束条件。

F、结构性关键字

当Json数据量较大,且存在很多雷同的约束时,可以利用结构性关键字来组织多个Json Schema模式文件来组织约束。

definitions关键字可以定义可被引用的约束条件。

{
“definitions”: {
“address”: {
“type”: “object”,
“properties”: {
“street_address”: {
“type”: “string”
},
“city”: {
“type”: “string”
},
“state”: {
“type”: “string”
}
},
“required”: [
“street_address”,
“city”,
“state”
]
}
}
}

通过**$ref**关键字进行引用即可重用一些共用的约束。

{
“$ref”: “#/definitions/address”
}

$ref关键字也可以从其他模式文件中加载引用。

{
“$ref”: “definitions.json#/address”
}

$id关键字可以为一组约束指定一个唯一的id,便于结构化的引用和定义引用跟路径。

{
“$id”: “http://foo.bar/schemas/address.json”
}

五、实例

以下内容来自:http://json-schema.org/learn/getting-started-step-by-step.html

{
// schema版本(OP)
KaTeX parse error: Expected 'EOF', got '#' at position 49: …draft-07/schema#̲", // schema唯一标…id”: “http://example.com/product.schema.json”,
// schema标题(OP)
“title”: “Product”,
// schema描述(OP)
“description”: “A product from Acme’s catalog”,
// 约束检查对象的类型
“type”: “object”,
// 受约束字段描述
“properties”: {
// 约束productId类型为整型
“productId”: {
“description”: “The unique identifier for a product”,
“type”: “integer”
},
// 约束productName类型为string
“productName”: {
“description”: “Name of the product”,
“type”: “string”
},
// 约束price > 0(不包含0)
“price”: {
“description”: “The price of the product”,
“type”: “number”,
“exclusiveMinimum”: 0
},
// 约束tags为数组类型
“tags”: {
“description”: “Tags for the product”,
“type”: “array”,
// 数组内的项类型为string
“items”: {
“type”: “string”
},
// 数组至少包含1项
“minItems”: 1,
// 数组中每项不能重发
“uniqueItems”: true
},
“dimensions”: {
“type”: “object”,
// 嵌套对象约束检查规则
“properties”: {
“length”: {
“type”: “number”
},
“width”: {
“type”: “number”
},
“height”: {
“type”: “number”
}
},
// 必须包含length、width、height
“required”: [ “length”, “width”, “height” ]
}
},
// 同上

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
veMinimum": 0
},
// 约束tags为数组类型
“tags”: {
“description”: “Tags for the product”,
“type”: “array”,
// 数组内的项类型为string
“items”: {
“type”: “string”
},
// 数组至少包含1项
“minItems”: 1,
// 数组中每项不能重发
“uniqueItems”: true
},
“dimensions”: {
“type”: “object”,
// 嵌套对象约束检查规则
“properties”: {
“length”: {
“type”: “number”
},
“width”: {
“type”: “number”
},
“height”: {
“type”: “number”
}
},
// 必须包含length、width、height
“required”: [ “length”, “width”, “height” ]
}
},
// 同上

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-lbb2502p-1713555310232)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!


  1. A-Za-z_ ↩︎

  • 28
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值