forked from ferranbt/fastssz
/
validate.go
58 lines (53 loc) · 1.35 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package generator
func (v *Value) validate() string {
switch v.t {
case TypeBitList, TypeBytes:
// this is a fixed-length array, not a slice, so it's size is a constant we don't need to check
if v.c {
return ""
}
// for fixed size collections, we need to ensure the size is an exact match
cmp := "!="
// for variable size values, we want to ensure it doesn't exceed max size bound
if !v.isFixed() {
cmp = ">"
}
tmpl := `if size := len(::.{{.name}}); size {{.cmp}} {{.size}} {
err = ssz.ErrBytesLengthFn("--.{{.name}}", size, {{.size}})
return
}
`
return execTmpl(tmpl, map[string]interface{}{
"cmp": cmp,
"name": v.name,
"size": v.s,
})
case TypeVector:
// this is a fixed-length array, not a slice, so it's size is a constant we don't need to check
if v.c {
return ""
}
// We only have vectors for [][]byte roots
tmpl := `if size := len(::.{{.name}}); size != {{.size}} {
err = ssz.ErrVectorLengthFn("--.{{.name}}", size, {{.size}})
return
}
`
return execTmpl(tmpl, map[string]interface{}{
"name": v.name,
"size": v.s,
})
case TypeList:
tmpl := `if size := len(::.{{.name}}); size > {{.size}} {
err = ssz.ErrListTooBigFn("--.{{.name}}", size, {{.size}})
return
}
`
return execTmpl(tmpl, map[string]interface{}{
"name": v.name,
"size": v.s,
})
default:
return ""
}
}