forked from NethermindEth/juno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation rules to rpc.Transaction
- Loading branch information
1 parent
681e2af
commit a4e7477
Showing
3 changed files
with
57 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package validator | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
|
||
"github.com/NethermindEth/juno/core/felt" | ||
"github.com/NethermindEth/juno/rpc" | ||
"github.com/go-playground/validator/v10" | ||
) | ||
|
||
var ( | ||
once sync.Once | ||
v *validator.Validate | ||
) | ||
|
||
// Validator returns a singleton that can be used to validate various objects | ||
func Validator() *validator.Validate { | ||
once.Do(func() { | ||
v = validator.New() | ||
// Register these types to use their string representation for validation | ||
// purposes | ||
v.RegisterCustomTypeFunc(func(field reflect.Value) any { | ||
switch f := field.Interface().(type) { | ||
case felt.Felt: | ||
return f.String() | ||
case *felt.Felt: | ||
return f.String() | ||
} | ||
panic("not a felt") | ||
}, felt.Felt{}, &felt.Felt{}) | ||
v.RegisterCustomTypeFunc(func(field reflect.Value) any { | ||
if t, ok := field.Interface().(rpc.TransactionType); ok { | ||
return t.String() | ||
} | ||
panic("not a TransactionType") | ||
}, rpc.TransactionType(0)) | ||
}) | ||
return v | ||
} |