Skip to content

Commit 9863e9a

Browse files
committed
better validation for integer types
1 parent afdd3e6 commit 9863e9a

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

pkg/datastore/data_rpc.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"math"
78
"strconv"
89
"strings"
910
"sync"
@@ -602,6 +603,7 @@ func validateFieldValue(f *sdcpb.LeafSchema, v any) error {
602603
func validateLeafTypeValue(lt *sdcpb.SchemaLeafType, v any) error {
603604
switch lt.GetType() {
604605
case "string":
606+
// TODO: validate length and range
605607
return nil
606608
case "int8":
607609
switch v := v.(type) {
@@ -610,6 +612,10 @@ func validateLeafTypeValue(lt *sdcpb.SchemaLeafType, v any) error {
610612
if err != nil {
611613
return err
612614
}
615+
case int64:
616+
if v > math.MaxInt8 || v < math.MinInt8 {
617+
return fmt.Errorf("value %v out of bound for type %s", v, lt.GetType())
618+
}
613619
default:
614620
return fmt.Errorf("unexpected casted type %T in %v", v, lt.GetType())
615621
}
@@ -621,6 +627,10 @@ func validateLeafTypeValue(lt *sdcpb.SchemaLeafType, v any) error {
621627
if err != nil {
622628
return err
623629
}
630+
case int64:
631+
if v > math.MaxInt16 || v < math.MinInt16 {
632+
return fmt.Errorf("value %v out of bound for type %s", v, lt.GetType())
633+
}
624634
default:
625635
return fmt.Errorf("unexpected casted type %T in %v", v, lt.GetType())
626636
}
@@ -632,6 +642,10 @@ func validateLeafTypeValue(lt *sdcpb.SchemaLeafType, v any) error {
632642
if err != nil {
633643
return err
634644
}
645+
case int64:
646+
if v > math.MaxInt32 || v < math.MinInt32 {
647+
return fmt.Errorf("value %v out of bound for type %s", v, lt.GetType())
648+
}
635649
default:
636650
return fmt.Errorf("unexpected casted type %T in %v", v, lt.GetType())
637651
}
@@ -654,6 +668,10 @@ func validateLeafTypeValue(lt *sdcpb.SchemaLeafType, v any) error {
654668
if err != nil {
655669
return err
656670
}
671+
case uint64:
672+
if v > math.MaxUint8 {
673+
return fmt.Errorf("value %v out of bound for type %s", v, lt.GetType())
674+
}
657675
default:
658676
return fmt.Errorf("unexpected casted type %T in %v", v, lt.GetType())
659677
}
@@ -665,6 +683,10 @@ func validateLeafTypeValue(lt *sdcpb.SchemaLeafType, v any) error {
665683
if err != nil {
666684
return err
667685
}
686+
case uint64:
687+
if v > math.MaxUint16 {
688+
return fmt.Errorf("value %v out of bound for type %s", v, lt.GetType())
689+
}
668690
default:
669691
return fmt.Errorf("unexpected casted type %T in %v", v, lt.GetType())
670692
}
@@ -676,6 +698,10 @@ func validateLeafTypeValue(lt *sdcpb.SchemaLeafType, v any) error {
676698
if err != nil {
677699
return err
678700
}
701+
case uint64:
702+
if v > math.MaxUint32 {
703+
return fmt.Errorf("value %v out of bound for type %s", v, lt.GetType())
704+
}
679705
default:
680706
return fmt.Errorf("unexpected casted type %T in %v", v, lt.GetType())
681707
}
@@ -694,11 +720,9 @@ func validateLeafTypeValue(lt *sdcpb.SchemaLeafType, v any) error {
694720
case "boolean":
695721
switch v := v.(type) {
696722
case string:
697-
switch {
698-
case v == "true":
699-
case v == "false":
700-
default:
701-
return fmt.Errorf("value %v must be a boolean", v)
723+
_, err := strconv.ParseBool(v)
724+
if err != nil {
725+
return fmt.Errorf("value %v must be a boolean: %v", v, err)
702726
}
703727
case bool:
704728
return nil

0 commit comments

Comments
 (0)