日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Json Schema介绍 和 .net 下的实践 - 基于Lateapexearlyspeed.Json.Schema - 基础1 - type关键字和string类型

發(fā)布時間:2024/1/16 windows 36 coder
生活随笔 收集整理的這篇文章主要介紹了 Json Schema介绍 和 .net 下的实践 - 基于Lateapexearlyspeed.Json.Schema - 基础1 - type关键字和string类型 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本系列旨在介紹Json Schema的常見用法,以及.net實現(xiàn)庫Lateapexearlyspeed.Json.Schema的使用


這篇文章將介紹Json Schema中的type關鍵字,和string類型的常見驗證功能。用例基于.net的LateApexEarlySpeed.Json.Schema nuget package。這是新創(chuàng)建的一個 Json Schema在.net下的高性能實現(xiàn)庫。


最簡單的Json Schema

就像其他各種Schema一樣,Json Schema的一個基本且核心的目的是對Json數(shù)據(jù)進行描述,以便進行驗證。Json Schema其實是一個由各種keywords組合而成的“容器”,每個keyword有不同的作用范圍和驗證功能。一個最簡單的Json Schema是空Json object,它代表所有的Json 數(shù)據(jù)都是有效的 (因為它沒有帶著任何keyword):

{}

讓我們用 .net下的Lateapexearlyspeed.Json.Schema library試一下:

var jsonValidator = new JsonValidator("{}");
ValidationResult validationResult = jsonValidator.Validate("123");

Assert.True(validationResult.IsValid);

除了空Json object, 還可以用true和false分別表示“任何數(shù)據(jù)都符合”和“任何數(shù)據(jù)都不符合”:

ValidationResult result = new JsonValidator("true").Validate("123");
Assert.True(result.IsValid);
ValidationResult result = new JsonValidator("false").Validate("123");
Assert.False(result.IsValid);

type 關鍵字

一般來說,大家用的最多的關鍵字(keyword)應該是type, 它用來描述數(shù)據(jù)應該是哪種類型的。比如下面的例子,只允許json數(shù)據(jù)是string而不能是其他類型:

string schema = """
{ "type": "string" }
""";
var jsonValidator = new JsonValidator(schema);
Assert.True(jsonValidator.Validate("\"abc\"").IsValid);
Assert.False(jsonValidator.Validate("123").IsValid);

type關鍵字支持如下內容:string,number,integer,object,array,boolean,null。

String

String type用于表示數(shù)據(jù)是json string type。

"This is string json token."
string schema = """
{ "type": "string" }
""";
var jsonValidator = new JsonValidator(schema);
Assert.True(jsonValidator.Validate("\"abc\"").IsValid);
Assert.False(jsonValidator.Validate("123").IsValid);

長度

對于String json token來說,可以用minLength和maxLength關鍵字來表示string長度:

string schema = """
{ 
  "type": "string",
  "minLength": 3,
  "maxLength": 5
}
""";
var jsonValidator = new JsonValidator(schema);
Assert.True(jsonValidator.Validate("\"abc\"").IsValid);

ValidationResult result = jsonValidator.Validate("\"ab\"");
Assert.False(result.IsValid);
Assert.Equal("minLength", result.Keyword);
Assert.Equal(ResultCode.StringLengthOutOfRange, result.ResultCode);
Assert.Equal("String instance's length is 2 which is less than '3'", result.ErrorMessage);

正則表達式

正則表達式的關鍵字是pattern,它用來驗證string數(shù)據(jù)是否匹配要求的pattern.

string schema = """
{ 
  "type": "string",
  "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"
}
""";
var jsonValidator = new JsonValidator(schema);
Assert.True(jsonValidator.Validate("\"(888)555-1212\"").IsValid);

ValidationResult result = jsonValidator.Validate("\"(800)FLOWERS\"");
Assert.False(result.IsValid);
Assert.Equal("pattern", result.Keyword);
Assert.Equal(ResultCode.RegexNotMatch, result.ResultCode);
Assert.Equal("Regex: '^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$' cannot find match in instance: '(800)FLOWERS'", result.ErrorMessage);

字符串格式

有時人們需要表示數(shù)據(jù)是一些常用的格式,比如是郵箱地址,uri, ip地址,日期時間,GUID 等。雖然可以用正則表達式pattern來手動解決,但json schema還是規(guī)定了format關鍵字來描述一些常用格式,以方便使用。LateApexEarlySpeed.Json.Schema默認支持如下format:

  • uri
  • uri-reference
  • date
  • time
  • date-time
  • email
  • uuid
  • hostname
  • ipv4
  • ipv6
  • json-pointer
  • regex

它們各自的具體含義可參考官方說明。
這里僅用email format來舉例子吧:

string schema = """
{ 
  "type": "string",
  "format": "email"
}
""";
var jsonValidator = new JsonValidator(schema);
Assert.True(jsonValidator.Validate("\"hello@world.com\"", new JsonSchemaOptions{ValidateFormat = true}).IsValid);

ValidationResult result = jsonValidator.Validate("\"@world.com\"", new JsonSchemaOptions { ValidateFormat = true });
Assert.False(result.IsValid);
Assert.Equal("format", result.Keyword);
Assert.Equal(ResultCode.InvalidFormat, result.ResultCode);
Assert.Equal("Invalid string value for format:'email'", result.ErrorMessage);

更完整的字符串相關關鍵字請參考官方json schema specification。


之后的文章會繼續(xù)介紹Json Schema的其他功能和LateApexEarlySpeed.Json.Schema的使用。


LateApexEarlySpeed.Json.Schema是新的Json Schema的.net library, nuget package下載:https://www.nuget.org/packages/Lateapexearlyspeed.Json.Schema

github doc repo: https://github.com/lateapexearlyspeed/Lateapexearlyspeed.JsonSchema.Doc, 使用中遇到的問題,歡迎發(fā)到repo issue這里。

總結

以上是生活随笔為你收集整理的Json Schema介绍 和 .net 下的实践 - 基于Lateapexearlyspeed.Json.Schema - 基础1 - type关键字和string类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。