[![GoDoc](https://godoc.org/github.com/xeipuuv/gojsonschema?status.svg)](https://godoc.org/github.com/xeipuuv/gojsonschema)
[![Build Status](https://travis-ci.org/xeipuuv/gojsonschema.svg)](https://travis-ci.org/xeipuuv/gojsonschema)
[![Go Report Card](https://goreportcard.com/badge/github.com/xeipuuv/gojsonschema)](https://goreportcard.com/report/github.com/xeipuuv/gojsonschema)
# gojsonschema
## Description
An implementation of JSON Schema for the Go programming language. Supports draft-04, draft-06 and draft-07.
References :
* http://json-schema.org
* http://json-schema.org/latest/json-schema-core.html
* http://json-schema.org/latest/json-schema-validation.html
## Installation
```
go get github.com/xeipuuv/gojsonschema
```
Dependencies :
* [github.com/xeipuuv/gojsonpointer](https://github.com/xeipuuv/gojsonpointer)
* [github.com/xeipuuv/gojsonreference](https://github.com/xeipuuv/gojsonreference)
* [github.com/stretchr/testify/assert](https://github.com/stretchr/testify#assert-package)
## Usage
### Example
```go
package main
import (
"fmt"
"github.com/xeipuuv/gojsonschema"
)
func main() {
schemaLoader := gojsonschema.NewReferenceLoader("file:///home/me/schema.json")
documentLoader := gojsonschema.NewReferenceLoader("file:///home/me/document.json")
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
panic(err.Error())
}
if result.Valid() {
fmt.Printf("The document is valid\n")
} else {
fmt.Printf("The document is not valid. see errors :\n")
for _, desc := range result.Errors() {
fmt.Printf("- %s\n", desc)
}
}
}
```
#### Loaders
There are various ways to load your JSON data.
In order to load your schemas and documents,
first declare an appropriate loader :
* Web / HTTP, using a reference :
```go
loader := gojsonschema.NewReferenceLoader("http://www.some_host.com/schema.json")
```
* Local file, using a reference :
```go
loader := gojsonschema.NewReferenceLoader("file:///home/me/schema.json")
```
References use the URI scheme, the prefix (file://) and a full path to the file are required.
* JSON strings :
```go
loader := gojsonschema.NewStringLoader(`{"type": "string"}`)
```
* Custom Go types :
```go
m := map[string]interface{}{"type": "string"}
loader := gojsonschema.NewGoLoader(m)
```
And
```go
type Root struct {
Users []User `json:"users"`
}
type User struct {
Name string `json:"name"`
}
...
data := Root{}
data.Users = append(data.Users, User{"John"})
data.Users = append(data.Users, User{"Sophia"})
data.Users = append(data.Users, User{"Bill"})
loader := gojsonschema.NewGoLoader(data)
```
#### Validation
Once the loaders are set, validation is easy :
```go
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
```
Alternatively, you might want to load a schema only once and process to multiple validations :
```go
schema, err := gojsonschema.NewSchema(schemaLoader)
...
result1, err := schema.Validate(documentLoader1)
...
result2, err := schema.Validate(documentLoader2)
...
// etc ...
```
To check the result :
```go
if result.Valid() {
fmt.Printf("The document is valid\n")
} else {
fmt.Printf("The document is not valid. see errors :\n")
for _, err := range result.Errors() {
// Err implements the ResultError interface
fmt.Printf("- %s\n", err)
}
}
```
## Loading local schemas
By default `file` and `http(s)` references to external schemas are loaded automatically via the file system or via http(s). An external schema can also be loaded using a `SchemaLoader`.
```go
sl := gojsonschema.NewSchemaLoader()
loader1 := gojsonschema.NewStringLoader(`{ "type" : "string" }`)
err := sl.AddSchema("http://some_host.com/string.json", loader1)
```
Alternatively if your schema already has an `$id` you can use the `AddSchemas` function
```go
loader2 := gojsonschema.NewStringLoader(`{
"$id" : "http://some_host.com/maxlength.json",
"maxLength" : 5
}`)
err = sl.AddSchemas(loader2)
```
The main schema should be passed to the `Compile` function. This main schema can then directly reference the added schemas without needing to download them.
```go
loader3 := gojsonschema.NewStringLoader(`{
"$id" : "http://some_host.com/main.json",
"allOf" : [
{ "$ref" : "http://some_host.com/string.json" },
{ "$ref" : "http://some_host.com/maxlength.json" }
]
}`)
schema, err := sl.Compile(loader3)
documentLoader := gojsonschema.NewStringLoader(`"hello world"`)
result, err := schema.Validate(documentLoader)
```
It's also possible to pass a `ReferenceLoader` to the `Compile` function that references a loaded schema.
```go
err = sl.AddSchemas(loader3)
schema, err := sl.Compile(gojsonschema.NewReferenceLoader("http://some_host.com/main.json"))
```
Schemas added by `AddSchema` and `AddSchemas` are only validated when the entire schema is compiled, unless meta-schema validation is used.
## Using a specific draft
By default `gojsonschema` will try to detect the draft of a schema by using the `$schema` keyword and parse it in a strict draft-04, draft-06 or draft-07 mode. If `$schema` is missing, or the draft version is not explicitely set, a hybrid mode is used which merges together functionality of all drafts into one mode.
Autodectection can be turned off with the `AutoDetect` property. Specific draft versions can be specified with the `Draft` property.
```go
sl := gojsonschema.NewSchemaLoader()
sl.Draft = gojsonschema.Draft7
sl.AutoDetect = false
```
If autodetection is on (default), a draft-07 schema can savely reference draft-04 schemas and vice-versa, as long as `$schema` is specified in all schemas.
## Meta-schema validation
Schemas that are added using the `AddSchema`, `AddSchemas` and `Compile` can be validated against their meta-schema by setting the `Validate` property.
The following example will produce an error as `multipleOf` must be a number. If `Validate` is off (default), this error is only returned at the `Compile` step.
```go
sl := gojsonschema.NewSchemaLoader()
sl.Validate = true
err := sl.AddSchemas(gojsonschema.NewStringLoader(`{
$id" : "http://some_host.com/invalid.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"multipleOf" : true
}`))
```
```
```
Errors returned by meta-schema validation are more readable and contain more information, which helps significantly if you are developing a schema.
Meta-schema validation also works with a custom `$schema`. In case `$schema` is missing, or `AutoDetect` is set to `false`, the meta-schema of the used draft is used.
## Working with Errors
The library handles string error codes which you can customize by creating your own gojsonschema.locale and setting it
```go
gojsonschema.Locale = YourCustomLocale{}
```
However, each error contains additional contextual information.
Newer versions of `gojsonschema` may have new additional errors, so code that uses a custom locale will need to be updated when this happens.
**err.Type()**: *string* Returns the "type" of error that occurred. Note you can also type check. See below
Note: An error of RequiredType has an err.Type() return value of "required"
"required": RequiredError
"invalid_type": InvalidTypeError
"number_any_of": NumberAnyOfError
"number_one_of": NumberOneOfError
"number_all_of": NumberAllOfError
"number_not": NumberNotError
"missing_dependency": MissingDependencyError
"internal": InternalError
"const": ConstEror
"enum": EnumError
"array_no_additional_items": ArrayNoAdditionalItemsError
"array_min_items": ArrayMinItemsError
"array_max_items": ArrayMaxItemsError
"unique": ItemsMustBeUniqueError
"contains" : ArrayContainsError
"array_min_properties": ArrayMinPropertiesError
"array_max_properties": ArrayMaxPropertiesError
"additional_property_not_allowed": AdditionalPropertyNotAllowedError
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 毕业设计 基于区块链的智能投顾系统源码+详细文档+全部资料(高分项目).zip毕业设计 基于区块链的智能投顾系统源码+详细文档+全部资料(高分项目).zip毕业设计 基于区块链的智能投顾系统源码+详细文档+全部资料(高分项目).zip毕业设计 基于区块链的智能投顾系统源码+详细文档+全部资料(高分项目).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为毕设项目、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 3、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计 基于区块链的智能投顾系统源码+详细文档+全部资料(高分项目).zip (790个子文件)
AUTHORS 173B
AUTHORS 173B
AUTHORS 173B
AUTHORS 173B
AUTHORS 12B
configtx.yaml.bak 15KB
init.sh.bak 4KB
gccgo_c.c 1KB
configtxgen 20.35MB
configtxlator 16.3MB
CONTRIBUTORS 170B
CONTRIBUTORS 170B
CONTRIBUTORS 170B
CONTRIBUTORS 170B
cryptogen 12.62MB
discover 16.73MB
Dockerfile 1KB
.editorconfig 539B
.editorconfig 539B
.editorconfig 539B
.env 122B
env 100B
example01 13.43MB
fabric-ca-client 21.89MB
fabric-ca-server 28.89MB
.gitignore 337B
.gitignore 259B
.gitignore 254B
.gitignore 254B
.gitignore 157B
.gitignore 39B
.gitignore 32B
.gitignore 25B
.gitignore 25B
.gitignore 16B
.gitignore 12B
.gitignore 12B
.gitignore 11B
.gitignore 10B
tables11.0.0.go 376KB
tables10.0.0.go 374KB
tables9.0.0.go 372KB
tables11.0.0.go 271KB
tables10.0.0.go 267KB
tables9.0.0.go 263KB
zerrors_linux_ppc64le.go 134KB
zerrors_linux_ppc64.go 134KB
zerrors_linux_sparc64.go 134KB
zerrors_linux_s390x.go 134KB
zerrors_linux_mips64le.go 132KB
zerrors_linux_mipsle.go 132KB
zerrors_linux_mips64.go 132KB
zerrors_linux_mips.go 132KB
zerrors_linux_arm.go 132KB
zerrors_linux_amd64.go 131KB
zerrors_linux_386.go 131KB
zerrors_linux_arm64.go 131KB
zerrors_linux_riscv64.go 131KB
tables11.0.0.go 117KB
tables10.0.0.go 111KB
tables9.0.0.go 109KB
server.go 91KB
scannerc.go 76KB
tables11.0.0.go 75KB
tables10.0.0.go 74KB
zerrors_openbsd_arm64.go 73KB
table_marshal.go 73KB
zerrors_darwin_amd64.go 73KB
zerrors_darwin_arm64.go 73KB
zerrors_darwin_386.go 73KB
zerrors_darwin_arm.go 73KB
zerrors_netbsd_386.go 72KB
zerrors_openbsd_amd64.go 72KB
transport.go 72KB
zerrors_netbsd_arm64.go 72KB
zerrors_netbsd_amd64.go 72KB
zerrors_netbsd_arm.go 72KB
tables9.0.0.go 71KB
zerrors_freebsd_arm.go 70KB
zerrors_freebsd_arm64.go 70KB
zerrors_freebsd_amd64.go 70KB
zerrors_freebsd_386.go 70KB
zerrors_openbsd_arm.go 68KB
zerrors_openbsd_386.go 68KB
zsyscall_darwin_amd64.go 67KB
zerrors_dragonfly_amd64.go 67KB
zsyscall_darwin_386.go 67KB
zsyscall_darwin_arm.go 66KB
zsyscall_darwin_arm64.go 66KB
ztypes_linux_s390x.go 63KB
ztypes_linux_amd64.go 63KB
ztypes_linux_riscv64.go 63KB
ztypes_linux_ppc64le.go 63KB
ztypes_linux_ppc64.go 63KB
ztypes_linux_386.go 63KB
ztypes_linux_mipsle.go 63KB
ztypes_linux_mips.go 63KB
ztypes_linux_sparc64.go 63KB
ztypes_linux_mips64le.go 63KB
ztypes_linux_mips64.go 63KB
共 790 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
不走小道
- 粉丝: 3365
- 资源: 5054
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- py-apple-controller-四足机器人
- py-apple-bldc-quadruped-robot-四足机器人
- 四足机器人-四足机器人
- asm-西电微机原理实验
- 四足机器人行走机制设计与应用解析
- 探索POINTS 1.5视觉思考模型:开启高效思考之门
- 支持 DELPHI 12.2的RXLIB 控件
- game_patch_1.29.13.13020.pak
- 4S店车辆管理系统.zip
- J2EE在在线项目管理与任务分配中的应用_411v2rh8_226-wx.zip
- “课件通”中小学教学课件共享平台.zip
- Java Web的租房管理系统(编号:22787207).zip
- Java大学生创新能力培养平台的设计与实现(编号:49116136).zip
- JavaWeb图书管理系统(编号:29027118)(1).zip
- springboot4S店车辆管理系统 LW PPT.zip
- spingboot茶文化推广系统(编号:3018432).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功