Skip to content

Commit

Permalink
Removes nil check for required properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Henkoglobin authored and omissis committed Apr 16, 2024
1 parent 83bd689 commit 8407170
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 23 deletions.
5 changes: 4 additions & 1 deletion pkg/generator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ type requiredValidator struct {
}

func (v *requiredValidator) generate(out *codegen.Emitter) {
out.Printlnf(`if v, ok := %s["%s"]; !ok || v == nil {`, varNameRawMap, v.jsonName)
// The container itself may be null (if the type is ["null", "object"]), in which case
// the map will be nil and none of the properties are present. This shouldn't fail
// the validation, though, as that's allowed as long as the container is allowed to be null.
out.Printlnf(`if _, ok := %s["%s"]; %s != nil && !ok {`, varNameRawMap, v.jsonName, varNameRawMap)
out.Indent(1)
out.Printlnf(`return fmt.Errorf("field %s in %s: required")`, v.jsonName, v.declName)
out.Indent(-1)
Expand Down
2 changes: 1 addition & 1 deletion tests/data/core/date/date.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/data/core/dateTime/dateTime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/data/core/ip/ip.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/data/core/object/object.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/data/core/time/time.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/data/regressions/issue32/issue32.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/data/validation/maxLength/maxLength.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/data/validation/minLength/minLength.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions tests/data/validation/requiredFields/requiredFields.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions tests/data/validation/requiredFields/requiredNullable.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/data/validation/requiredFields/requiredNullable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/requiredNullable",
"type": "object",
"properties": {
"myNullableString": {
"type": [
"string",
"null"
]
},
"myNullableStringArray": {
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
},
"myNullableObject": {
"type": [
"object",
"null"
],
"properties": {
"myNestedProp": {
"type": "string"
}
},
"required": [
"myNestedProp"
]
}
},
"required": [
"myNullableString",
"myNullableStringArray",
"myNullableObject"
]
}
40 changes: 40 additions & 0 deletions tests/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

testMaxLength "github.com/atombender/go-jsonschema/tests/data/validation/maxLength"
testMinLength "github.com/atombender/go-jsonschema/tests/data/validation/minLength"
testRequiredFields "github.com/atombender/go-jsonschema/tests/data/validation/requiredFields"
"github.com/atombender/go-jsonschema/tests/helpers"
)

Expand Down Expand Up @@ -112,3 +113,42 @@ func TestMinStringLength(t *testing.T) {
})
}
}

func TestRequiredFields(t *testing.T) {
t.Parallel()

testCases := []struct {
desc string
data string
wantErr error
}{
{
desc: "object without required property fails validation",
data: `{}`,
wantErr: errors.New("field myNullableObject in RequiredNullable: required"),
},
{
desc: "required properties may be null",
data: `{ "myNullableObject": null, "myNullableStringArray": null, "myNullableString": null }`,
wantErr: nil,
},
{
desc: "required properties may have a non-null value",
data: `{ "myNullableObject": { "myNestedProp": "世界" }, "myNullableStringArray": ["hello"], "myNullableString": "world" }`,
wantErr: nil,
},
}
for _, tC := range testCases {
tC := tC

t.Run(tC.desc, func(t *testing.T) {
t.Parallel()

model := testRequiredFields.RequiredNullable{}

err := json.Unmarshal([]byte(tC.data), &model)

helpers.CheckError(t, tC.wantErr, err)
})
}
}

0 comments on commit 8407170

Please sign in to comment.