Skip to content

Commit 2d87019

Browse files
committed
Removes nil check for required properties
1 parent f2a0dad commit 2d87019

File tree

14 files changed

+172
-23
lines changed

14 files changed

+172
-23
lines changed

pkg/generator/validator.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ type requiredValidator struct {
3535
}
3636

3737
func (v *requiredValidator) generate(out *codegen.Emitter) {
38-
out.Printlnf(`if v, ok := %s["%s"]; !ok || v == nil {`, varNameRawMap, v.jsonName)
38+
// The container itself may be null (if the type is ["null", "object"]), in which case
39+
// the map will be nil and none of the properties are present. This shouldn't fail
40+
// the validation, though, as that's allowed as long as the container is allowed to be null.
41+
out.Printlnf(`if _, ok := %s["%s"]; %s != nil && !ok {`, varNameRawMap, v.jsonName, varNameRawMap)
3942
out.Indent(1)
4043
out.Printlnf(`return fmt.Errorf("field %s in %s: required")`, v.jsonName, v.declName)
4144
out.Indent(-1)

tests/data/core/date/date.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/core/dateTime/dateTime.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/core/ip/ip.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/core/object/object.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/core/time/time.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/miscWithDefaults/cyclicAndRequired1/cyclicAndRequired1.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/regressions/issue32/issue32.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/validation/maxLength/maxLength.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/validation/minLength/minLength.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/validation/requiredFields/requiredFields.go

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/validation/requiredFields/requiredNullable.go

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"id": "https://example.com/requiredNullable",
4+
"type": "object",
5+
"properties": {
6+
"myNullableString": {
7+
"type": [
8+
"string",
9+
"null"
10+
]
11+
},
12+
"myNullableStringArray": {
13+
"type": [
14+
"array",
15+
"null"
16+
],
17+
"items": {
18+
"type": "string"
19+
}
20+
},
21+
"myNullableObject": {
22+
"type": [
23+
"object",
24+
"null"
25+
],
26+
"properties": {
27+
"myNestedProp": {
28+
"type": "string"
29+
}
30+
},
31+
"required": [
32+
"myNestedProp"
33+
]
34+
}
35+
},
36+
"required": [
37+
"myNullableString",
38+
"myNullableStringArray",
39+
"myNullableObject"
40+
]
41+
}

tests/validation_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
testMaxLength "github.com/atombender/go-jsonschema/tests/data/validation/maxLength"
99
testMinLength "github.com/atombender/go-jsonschema/tests/data/validation/minLength"
10+
testRequiredFields "github.com/atombender/go-jsonschema/tests/data/validation/requiredFields"
1011
"github.com/atombender/go-jsonschema/tests/helpers"
1112
)
1213

@@ -112,3 +113,42 @@ func TestMinStringLength(t *testing.T) {
112113
})
113114
}
114115
}
116+
117+
func TestRequiredFields(t *testing.T) {
118+
t.Parallel()
119+
120+
testCases := []struct {
121+
desc string
122+
data string
123+
wantErr error
124+
}{
125+
{
126+
desc: "object without required property fails validation",
127+
data: `{}`,
128+
wantErr: errors.New("field myNullableObject in RequiredNullable: required"),
129+
},
130+
{
131+
desc: "required properties may be null",
132+
data: `{ "myNullableObject": null, "myNullableStringArray": null, "myNullableString": null }`,
133+
wantErr: nil,
134+
},
135+
{
136+
desc: "required properties may have a non-null value",
137+
data: `{ "myNullableObject": { "myNestedProp": "世界" }, "myNullableStringArray": ["hello"], "myNullableString": "world" }`,
138+
wantErr: nil,
139+
},
140+
}
141+
for _, tC := range testCases {
142+
tC := tC
143+
144+
t.Run(tC.desc, func(t *testing.T) {
145+
t.Parallel()
146+
147+
model := testRequiredFields.RequiredNullable{}
148+
149+
err := json.Unmarshal([]byte(tC.data), &model)
150+
151+
helpers.CheckError(t, tC.wantErr, err)
152+
})
153+
}
154+
}

0 commit comments

Comments
 (0)