Skip to content

Commit 7f81341

Browse files
authored
Fix: add validate:"dive" tag for the rpccore.LimitSlice type (#3419)
* Update limit_slice.go Signed-off-by: Thiago Ribeiro <62709592+thiagodeev@users.noreply.github.com> * test: Add validation test for LimitSlice type * test: Update validation test to use a new random type in LimitSlice --------- Signed-off-by: Thiago Ribeiro <62709592+thiagodeev@users.noreply.github.com>
1 parent 95a1967 commit 7f81341

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

rpc/rpccore/limit_slice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (l FunctionCalldataLimit) Limit() int { return functionCalldataLimit }
2727
func (l SenderAddressLimit) Limit() int { return senderAddressLimit }
2828

2929
type LimitSlice[T any, L Limit] struct {
30-
Data []T
30+
Data []T `validate:"dive"`
3131
}
3232

3333
func (l LimitSlice[T, L]) MarshalJSON() ([]byte, error) {

rpc/rpccore/limit_slice_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/NethermindEth/juno/core/felt"
1212
"github.com/NethermindEth/juno/rpc/rpccore"
1313
rpcv9 "github.com/NethermindEth/juno/rpc/v9"
14+
"github.com/NethermindEth/juno/validator"
1415
"github.com/stretchr/testify/require"
1516
)
1617

@@ -56,6 +57,28 @@ func TestLazySlice(t *testing.T) {
5657
nil,
5758
)
5859
})
60+
61+
// This test ensures that the validation logic works for the values inside the Data slice.
62+
t.Run("ValidateRequiredFields", func(t *testing.T) {
63+
// Random type with validation tags
64+
type RandType struct {
65+
A int `validate:"required"`
66+
B int `validate:"gt=5"`
67+
}
68+
69+
type RandLimitSlice = rpccore.LimitSlice[
70+
RandType,
71+
rpccore.SimulationLimit,
72+
]
73+
74+
withEmptyValues := RandLimitSlice{
75+
Data: make([]RandType, 10),
76+
}
77+
78+
validate := validator.Validator()
79+
err := validate.Struct(withEmptyValues)
80+
require.Error(t, err, "Validation is not working for the values inside the Data slice")
81+
})
5982
}
6083

6184
func runTest[T any, L rpccore.Limit](

0 commit comments

Comments
 (0)