Json Schema介绍 和 .net 下的实践 - 基于Lateapexearlyspeed.Json.Schema - 基础1 - type关键字和string类型
本系列旨在介紹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
- 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类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 聊聊Llama2-Chinese中文大模
- 下一篇: 揭秘Spring事务失效场景分析与解决方