Skip to content

Commit

Permalink
OptionsRequired to CastAssoc (#33)
Browse files Browse the repository at this point in the history
* add options required to cast assoc

* change assocRequired to required
  • Loading branch information
Kiswanto D authored and Fs02 committed Oct 8, 2018
1 parent 4b3f0f1 commit ff85db4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions changeset/cast_assoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// CastAssocErrorMessage is the default error message for CastAssoc.
var CastAssocErrorMessage = "{field} is invalid"
var CastAssocRequiredMessage = "{field} is required"

// ChangeFunc is changeset function.
type ChangeFunc func(interface{}, params.Params) *Changeset
Expand Down Expand Up @@ -38,6 +39,13 @@ func CastAssoc(ch *Changeset, field string, fn ChangeFunc, opts ...Option) {
msg := strings.Replace(options.message, "{field}", field, 1)
AddError(ch, field, msg)
}

_, found := ch.changes[field]
if options.required && !found {
options.message = CastAssocRequiredMessage
msg := strings.Replace(options.message, "{field}", field, 1)
AddError(ch, field, msg)
}
}

func castOne(ch *Changeset, field string, fn ChangeFunc) bool {
Expand Down
42 changes: 42 additions & 0 deletions changeset/cast_assoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,45 @@ func TestCastAssoc_manyInnerChangesetError(t *testing.T) {
assert.Equal(t, "field4 is invalid", ch.Error().Error())
assert.Equal(t, "field3[0].field4", ch.Error().(errors.Error).Field)
}

func TestCastAssoc_optionRequired(t *testing.T) {
var data struct {
Field1 int
Field2 string
Field3 []Inner
}

changeInner := func(data interface{}, input params.Params) *Changeset {
ch := Cast(data, input, []string{"field4", "field5"})
return ch
}

invalidInput := params.Map{
"field1": 1,
"field2": "2",
}

validInput := params.Map{
"field1": 1,
"field2": "2",
"field3": []params.Map{
{
"field4": 14,
"field5": "15",
},
{
"field4": 24,
"field5": "25",
},
},
}

invalidCh := Cast(data, invalidInput, []string{"field1", "field2"})
CastAssoc(invalidCh, "field3", changeInner, Required(true))

validCh := Cast(data, validInput, []string{"field1", "field2"})
CastAssoc(validCh, "field3", changeInner, Required(true))

assert.NotNil(t, invalidCh.Errors())
assert.Nil(t, validCh.Errors())
}
7 changes: 7 additions & 0 deletions changeset/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Options struct {
name string
exact bool
changeOnly bool
required bool
}

// Option for changeset operation.
Expand Down Expand Up @@ -52,3 +53,9 @@ func ChangeOnly(changeOnly bool) Option {
opts.changeOnly = changeOnly
}
}

func Required(required bool) Option {
return func(opts *Options) {
opts.required = required
}
}
2 changes: 2 additions & 0 deletions changeset/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ func TestOptions(t *testing.T) {
Name("name_fk"),
Exact(true),
ChangeOnly(true),
Required(true),
})

assert.Equal(t, "message", opts.message)
assert.Equal(t, 1000, opts.code)
assert.Equal(t, "name_fk", opts.name)
assert.Equal(t, true, opts.exact)
assert.Equal(t, true, opts.changeOnly)
assert.Equal(t, true, opts.required)
}

0 comments on commit ff85db4

Please sign in to comment.