在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:santhosh-tekuri/jsonschema开源软件地址:https://github.com/santhosh-tekuri/jsonschema开源编程语言:Go 100.0%开源软件介绍:jsonschema v5.0.0Package jsonschema provides json-schema compilation and validation. Features:
see examples in godoc The schema is compiled against the version specified in You can force to use specific version, when compiler := jsonschema.NewCompiler()
compiler.Draft = jsonschema.Draft4 This package supports loading json-schema from filePath and fileURL. To load json-schema from HTTPURL, add following import: import _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" Rich ErrorsThe ValidationError returned by Validate method contains detailed context to understand why and where the error is. schema.json: {
"$ref": "t.json#/definitions/employee"
} t.json: {
"definitions": {
"employee": {
"type": "string"
}
}
} doc.json: 1 assuming fmt.Printf("%#v\n", err) // using %#v prints errors hierarchy Prints:
Here To output b, _ := json.MarshalIndent(err.FlagOutput(), "", " ")
fmt.Println(string(b)) Prints: {
"valid": false
} To output b, _ := json.MarshalIndent(err.BasicOutput(), "", " ")
fmt.Println(string(b)) Prints: {
"valid": false,
"errors": [
{
"keywordLocation": "",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/schema.json#",
"instanceLocation": "",
"error": "doesn't validate with file:///Users/santhosh/jsonschema/schema.json#"
},
{
"keywordLocation": "/$ref",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/schema.json#/$ref",
"instanceLocation": "",
"error": "doesn't validate with 'file:///Users/santhosh/jsonschema/t.json#/definitions/employee'"
},
{
"keywordLocation": "/$ref/type",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/t.json#/definitions/employee/type",
"instanceLocation": "",
"error": "expected string, but got number"
}
]
} To output b, _ := json.MarshalIndent(err.DetailedOutput(), "", " ")
fmt.Println(string(b)) Prints: {
"valid": false,
"keywordLocation": "",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/schema.json#",
"instanceLocation": "",
"errors": [
{
"valid": false,
"keywordLocation": "/$ref",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/schema.json#/$ref",
"instanceLocation": "",
"errors": [
{
"valid": false,
"keywordLocation": "/$ref/type",
"absoluteKeywordLocation": "file:///Users/santhosh/jsonschema/t.json#/definitions/employee/type",
"instanceLocation": "",
"error": "expected string, but got number"
}
]
}
]
} CLIjv [-draft INT] [-output FORMAT] <json-schema> [<json-doc>]...
-draft int
draft used when '$schema' attribute is missing. valid values 4, 5, 7, 2019, 2020 (default 2020)
-output string
output format. valid values flag, basic, detailed if no exit-code is 1, if there are any validation errors Validating YAML Documentsince yaml supports non-string keys, such yaml documents are rendered as invalid json documents. https://play.golang.org/p/Hhax3MrtD8r the above example shows how to validate yaml document with jsonschema. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论