Skip to content

Commit 036563a

Browse files
authored
Merge pull request #1189 from sudo-bmitch/pr-jsonschema-validator
Switch jsonschema validation libraries
2 parents dd33f72 + 4bbdd7f commit 036563a

File tree

8 files changed

+135
-275
lines changed

8 files changed

+135
-275
lines changed

go.mod

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,5 @@ go 1.18
55
require (
66
github.com/opencontainers/go-digest v1.0.0
77
github.com/russross/blackfriday v1.6.0
8-
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
9-
github.com/xeipuuv/gojsonschema v1.2.0
10-
)
11-
12-
require (
13-
github.com/davecgh/go-spew v1.1.1 // indirect
14-
github.com/stretchr/testify v1.7.0 // indirect
15-
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
16-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
8+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
179
)

go.sum

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
41
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
52
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
6-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
83
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
94
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
10-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
12-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
13-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14-
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
15-
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
16-
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
17-
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
18-
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
19-
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
20-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
23-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
6+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=

schema/defs-descriptor.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"mediaType": {
55
"id": "https://opencontainers.org/schema/image/descriptor/mediaType",
66
"type": "string",
7-
"pattern": "^[A-Za-z0-9][A-Za-z0-9!#$&-^_.+]{0,126}/[A-Za-z0-9][A-Za-z0-9!#$&-^_.+]{0,126}$"
7+
"pattern": "^[A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}/[A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}$"
88
},
99
"digest": {
1010
"description": "the cryptographic checksum digest of the object, in the pattern '<algorithm>:<encoded>'",

schema/error.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323

2424
// A SyntaxError is a description of a JSON syntax error
2525
// including line, column and offset in the JSON file.
26+
//
27+
// Deprecated: SyntaxError is no longer returned from Validator.
2628
type SyntaxError struct {
2729
msg string
2830
Line, Col int
@@ -34,6 +36,8 @@ func (e *SyntaxError) Error() string { return e.msg }
3436
// WrapSyntaxError checks whether the given error is a *json.SyntaxError
3537
// and converts it into a *schema.SyntaxError containing line/col information using the given reader.
3638
// If the given error is not a *json.SyntaxError it is returned unchanged.
39+
//
40+
// Deprecated: WrapSyntaxError is no longer returned by Validator.
3741
func WrapSyntaxError(r io.Reader, err error) error {
3842
var serr *json.SyntaxError
3943
if errors.As(err, &serr) {

schema/loader.go

Lines changed: 0 additions & 125 deletions
This file was deleted.

schema/schema.go

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,56 +23,62 @@ import (
2323

2424
// Media types for the OCI image formats
2525
const (
26-
ValidatorMediaTypeDescriptor Validator = v1.MediaTypeDescriptor
27-
ValidatorMediaTypeLayoutHeader Validator = v1.MediaTypeLayoutHeader
28-
ValidatorMediaTypeManifest Validator = v1.MediaTypeImageManifest
29-
ValidatorMediaTypeImageIndex Validator = v1.MediaTypeImageIndex
30-
ValidatorMediaTypeImageConfig Validator = v1.MediaTypeImageConfig
31-
ValidatorMediaTypeImageLayer unimplemented = v1.MediaTypeImageLayer
26+
ValidatorMediaTypeDescriptor Validator = v1.MediaTypeDescriptor
27+
ValidatorMediaTypeLayoutHeader Validator = v1.MediaTypeLayoutHeader
28+
ValidatorMediaTypeManifest Validator = v1.MediaTypeImageManifest
29+
ValidatorMediaTypeImageIndex Validator = v1.MediaTypeImageIndex
30+
ValidatorMediaTypeImageConfig Validator = v1.MediaTypeImageConfig
31+
ValidatorMediaTypeImageLayer Validator = v1.MediaTypeImageLayer
3232
)
3333

3434
var (
35-
// fs stores the embedded http.FileSystem
36-
// having the OCI JSON schema files in root "/".
35+
// specFS stores the embedded http.FileSystem having the OCI JSON schema files in root "/".
3736
//go:embed *.json
38-
fs embed.FS
37+
specFS embed.FS
3938

40-
// schemaNamespaces is a set of URI prefixes which are treated as containing the schema files of fs.
41-
// This is necessary because *.json schema files in this directory use "id" and "$ref" attributes which evaluate to such URIs, e.g.
42-
// ./image-manifest-schema.json URI contains
43-
// "id": "https://opencontainers.org/schema/image/manifest",
44-
// and
45-
// "$ref": "content-descriptor.json"
46-
// which evaluates as a link to https://opencontainers.org/schema/image/content-descriptor.json .
47-
//
48-
// To support such links without accessing the network (and trying to load content which is not hosted at these URIs),
49-
// fsLoaderFactory accepts any URI starting with one of the schemaNamespaces below,
50-
// and uses _escFS to load them from the root of its in-memory filesystem tree.
51-
//
52-
// (Note that this must contain subdirectories before its parent directories for fsLoaderFactory.refContents to work.)
53-
schemaNamespaces = []string{
54-
"https://opencontainers.org/schema/image/descriptor/",
55-
"https://opencontainers.org/schema/image/index/",
56-
"https://opencontainers.org/schema/image/manifest/",
57-
"https://opencontainers.org/schema/image/",
58-
"https://opencontainers.org/schema/descriptor/",
59-
"https://opencontainers.org/schema/",
39+
// specsOrig maps OCI schema media types to schema files.
40+
specs = map[Validator]string{
41+
ValidatorMediaTypeDescriptor: "content-descriptor.json",
42+
ValidatorMediaTypeLayoutHeader: "image-layout-schema.json",
43+
ValidatorMediaTypeManifest: "image-manifest-schema.json",
44+
ValidatorMediaTypeImageIndex: "image-index-schema.json",
45+
ValidatorMediaTypeImageConfig: "config-schema.json",
6046
}
6147

62-
// specs maps OCI schema media types to schema URIs.
63-
// These URIs are expected to be used only by fsLoaderFactory (which trims schemaNamespaces defined above)
64-
// and should never cause a network access.
65-
specs = map[Validator]string{
66-
ValidatorMediaTypeDescriptor: "https://opencontainers.org/schema/content-descriptor.json",
67-
ValidatorMediaTypeLayoutHeader: "https://opencontainers.org/schema/image/image-layout-schema.json",
68-
ValidatorMediaTypeManifest: "https://opencontainers.org/schema/image/image-manifest-schema.json",
69-
ValidatorMediaTypeImageIndex: "https://opencontainers.org/schema/image/image-index-schema.json",
70-
ValidatorMediaTypeImageConfig: "https://opencontainers.org/schema/image/config-schema.json",
48+
// specURLs lists the various URLs a given spec may be known by.
49+
// This is generated from the "id" value in each spec and relative ref values they contain.
50+
specURLs = map[string][]string{
51+
"config-schema.json": {
52+
"https://opencontainers.org/schema/image/config",
53+
},
54+
"content-descriptor.json": {
55+
"https://opencontainers.org/schema/descriptor",
56+
"https://opencontainers.org/schema/image/content-descriptor.json",
57+
},
58+
"defs-descriptor.json": {
59+
"https://opencontainers.org/schema/image/descriptor/mediaType",
60+
"https://opencontainers.org/schema/defs-descriptor.json",
61+
"https://opencontainers.org/schema/image/defs-descriptor.json",
62+
},
63+
"defs.json": {
64+
"https://opencontainers.org/schema/defs.json",
65+
"https://opencontainers.org/schema/image/defs.json",
66+
"https://opencontainers.org/schema/image/descriptor/defs.json",
67+
},
68+
"image-index-schema.json": {
69+
"https://opencontainers.org/schema/image/index",
70+
},
71+
"image-layout-schema.json": {
72+
"https://opencontainers.org/schema/image/layout",
73+
},
74+
"image-manifest-schema.json": {
75+
"https://opencontainers.org/schema/image/manifest",
76+
},
7177
}
7278
)
7379

7480
// FileSystem returns an in-memory filesystem including the schema files.
7581
// The schema files are located at the root directory.
7682
func FileSystem() http.FileSystem {
77-
return http.FS(fs)
83+
return http.FS(specFS)
7884
}

schema/spec_test.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,11 @@ func validate(t *testing.T, name string) {
106106
err = schema.Validator(example.Mediatype).Validate(strings.NewReader(example.Body))
107107
if err == nil {
108108
printFields(t, "ok", example.Mediatype, example.Title)
109-
t.Log(example.Body, "---")
110-
continue
111-
}
112-
113-
var errs []error
114-
var verr schema.ValidationError
115-
if errors.As(err, &verr) {
116-
errs = verr.Errs
117109
} else {
118110
printFields(t, "error", example.Mediatype, example.Title, err)
119111
t.Error(err)
120-
t.Log(example.Body, "---")
121-
continue
122-
}
123-
124-
for _, err := range errs {
125-
printFields(t, "invalid", example.Mediatype, example.Title)
126-
t.Error(err)
127-
fmt.Println(example.Body, "---")
128-
continue
129112
}
113+
t.Log(example.Body, "---")
130114
}
131115
}
132116

0 commit comments

Comments
 (0)