Skip to content

Commit 4022063

Browse files
feat: add json schema conditional validations (#65)
* feat: add json schema conditional validations * test: add JSONSchema tests * fix: linting issues * fix: remove unnecessary imports * fix(kubernetes): add check for host field for URI format and http/s prefix Co-authored-by: Rohil Surana <[email protected]>
1 parent 076dd28 commit 4022063

File tree

2 files changed

+164
-3
lines changed

2 files changed

+164
-3
lines changed

modules/kubernetes/config_schema.json

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"type": "object",
55
"properties": {
66
"host": {
7-
"type": "string"
7+
"type": "string",
8+
"format": "uri",
9+
"pattern": "^https?://"
810
},
911
"insecure": {
1012
"type": "boolean",
@@ -27,7 +29,53 @@
2729
"default": 100000000
2830
}
2931
},
30-
"required": [
31-
"host"
32+
"allOf": [
33+
{
34+
"oneOf": [
35+
{
36+
"required": [
37+
"token"
38+
]
39+
},
40+
{
41+
"allOf": [
42+
{
43+
"required": [
44+
"client_key"
45+
]
46+
},
47+
{
48+
"required": [
49+
"client_certificate"
50+
]
51+
}
52+
]
53+
}
54+
]
55+
},
56+
{
57+
"oneOf": [
58+
{
59+
"required": [
60+
"cluster_ca_certificate"
61+
]
62+
},
63+
{
64+
"properties": {
65+
"insecure": {
66+
"const": true
67+
}
68+
},
69+
"required": [
70+
"insecure"
71+
]
72+
}
73+
]
74+
},
75+
{
76+
"required": [
77+
"host"
78+
]
79+
}
3280
]
3381
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package kubernetes
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/xeipuuv/gojsonschema"
9+
)
10+
11+
func TestModule_KubernetesJSONSchema(t *testing.T) {
12+
tests := []struct {
13+
Case string
14+
wantErr error
15+
want bool
16+
}{
17+
{
18+
Case: `{
19+
"host": "http://0.0.0.0:1234",
20+
"insecure": true,
21+
"token": "token"
22+
}`,
23+
wantErr: nil,
24+
want: true,
25+
},
26+
{
27+
Case: `{
28+
"host": "http://0.0.0.0:1234",
29+
"insecure": false,
30+
"token": "token"
31+
}`,
32+
wantErr: nil,
33+
want: false,
34+
},
35+
{
36+
Case: `{
37+
"host": "http://0.0.0.0:1234",
38+
"insecure": false,
39+
"cluster_ca_certificate": "c_ca_cert",
40+
"token": "token"
41+
}`,
42+
wantErr: nil,
43+
want: true,
44+
},
45+
{
46+
Case: `{
47+
"host": "http://0.0.0.0:1234",
48+
"cluster_ca_certificate": "c_ca_cert",
49+
"token": "token"
50+
}`,
51+
wantErr: nil,
52+
want: true,
53+
},
54+
{
55+
Case: `{
56+
"host": "http://0.0.0.0:1234",
57+
"insecure": true,
58+
"client_key": "c_key",
59+
"client_certificate": "c_cert"
60+
}`,
61+
wantErr: nil,
62+
want: true,
63+
},
64+
{
65+
Case: ` "host": "http://0.0.0.0:1234",
66+
"insecure": true,
67+
"client_key": "c_key"
68+
}`,
69+
wantErr: nil,
70+
want: false,
71+
},
72+
{
73+
Case: `{
74+
"host": "http://0.0.0.0:1234",
75+
"insecure": true,
76+
"token": "token",
77+
"client_key": "c_key",
78+
"client_certificate": "c_cert"
79+
}`,
80+
wantErr: nil,
81+
want: false,
82+
},
83+
{
84+
Case: `{
85+
"host": "http://0.0.0.0:1234",
86+
"insecure": true,
87+
"token": "token",
88+
"client_key": "c_key"
89+
}`,
90+
wantErr: nil,
91+
want: true,
92+
},
93+
}
94+
loader := gojsonschema.NewStringLoader(configSchema)
95+
schema, _ := gojsonschema.NewSchema(loader)
96+
97+
for _, tt := range tests {
98+
tt := tt
99+
t.Run(tt.Case, func(t *testing.T) {
100+
t.Parallel()
101+
102+
c := gojsonschema.NewStringLoader(tt.Case)
103+
result, err := schema.Validate(c)
104+
if tt.wantErr != nil {
105+
assert.Error(t, err)
106+
assert.True(t, errors.Is(err, tt.wantErr))
107+
} else {
108+
assert.NoError(t, err)
109+
}
110+
assert.Equal(t, tt.want, result.Valid())
111+
})
112+
}
113+
}

0 commit comments

Comments
 (0)