-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
173 lines (151 loc) · 3.7 KB
/
validator.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package jzon
import (
"fmt"
"regexp"
"strings"
)
// Condition tags
const (
COND_REGEXP = "regexp:"
COND_RANGE = "range:"
COND_BOOL = "bool:"
COND_NULL = "null:"
)
// States
const (
RANGE_CLOSE int = iota
RANGE_EXCEPT
RANGE_ARRAY
)
// Error levels
const (
LEVEL_FETAL int = iota
LEVEL_EMERGENCY
LEVEL_EXCEPTION
LEVEL_LOWEST
)
// Validator defines a validator to validate if a JSON value can be acceptable
type Validator func(*Jzon) bool
// Range defines a numeric range
// for RANGE_CLOSE:
// LowerBound <= target < UpperBound
// for RANGE_EXCEPT:
// LowerBound > target || target > UpperBound
// for RANGE_ARRAY:
// array.Contains(target)
type Range struct {
Type int
UpperBound int64
LowerBound int64
Array []int64
}
// CanAccept judges whether the target number should be accepted
func (rng *Range) CanAccept(n int64) bool {
switch rng.Type {
case RANGE_CLOSE:
return rng.LowerBound <= n && n < rng.UpperBound
case RANGE_EXCEPT:
return n < rng.LowerBound || rng.UpperBound < n
case RANGE_ARRAY:
for _, v := range rng.Array {
if v == n {
return true
}
}
return false
}
return false
}
/*
sample:
{
"str": "regexp: 0 *"
"num": "range: 2 100-200 | -1+2 | 1,2,3"
"bol": "bool: 1 true|false"
"nul": "null: 2 null"
}
*/
func compileCondition(cond string) (fn Validator, err error) {
switch {
case strings.HasPrefix(cond, COND_REGEXP):
return compileRegExp(cond)
case strings.HasPrefix(cond, COND_RANGE):
return compileRange(cond)
case strings.HasPrefix(cond, COND_BOOL):
return compileBool(cond)
case strings.HasPrefix(cond, COND_NULL):
return compileNull(cond)
}
return nil, fmt.Errorf("expect a string with prefix `%s` | `%s` | `%s` | `%s` but found `%s`",
COND_REGEXP, COND_BOOL, COND_NULL, COND_RANGE, cond)
}
func compileRegExp(cond string) (fn Validator, err error) {
reg, err := regexp.Compile(strings.TrimPrefix(cond, COND_REGEXP))
if err != nil {
return nil, err
}
return func(jz *Jzon) bool {
str, err := jz.String()
if err != nil {
return false
}
return reg.Match([]byte(str))
}, nil
}
func compileRange(cond string) (fn Validator, err error) {
var rng Range
rangeStr := strings.TrimPrefix(cond, COND_RANGE)
nParsed, err := fmt.Sscanf(rangeStr, "%d,%d", &rng.LowerBound, &rng.UpperBound)
if err != nil {
return nil, err
}
if nParsed != 2 {
return nil, fmt.Errorf("expect 2 range numbers, but found %d", nParsed)
}
return func(jz *Jzon) bool {
var n int64
if n, err = jz.Integer(); err != nil {
return false
}
return rng.CanAccept(n)
}, nil
}
func compileBool(cond string) (fn Validator, err error) {
boolStr := strings.TrimPrefix(cond, COND_BOOL)
var b bool
if boolStr == "true" {
b = true
} else if boolStr == "false" {
b = false
} else if boolStr == "both" {
return func(jz *Jzon) bool {
return jz.Type == JzTypeBol
}, nil
} else {
return nil, fmt.Errorf("expect `true` | `fasle` | `both` but found `%s`", boolStr)
}
return func(jz *Jzon) bool {
v, err := jz.Bool()
if err != nil {
return false
}
return v == b
}, nil
}
func compileNull(cond string) (fn Validator, err error) {
nullStr := strings.TrimPrefix(cond, COND_NULL)
if nullStr == "null" {
return func(jz *Jzon) bool {
return jz.Type == JzTypeNul
}, nil
}
return nil, expectString("null", []byte(nullStr))
}
// Validate verifies this node by another JSON which has a particular grammar,
// the given JSON should define the format of each field by a validator and a
// level number. If there were any fields can't pass the relying validators,
// `Validate` would generate detailed report into another JSON respectively
func (jz *Jzon) Validate(validator *Jzon) (ok bool, report *Jzon, err error) {
// TODO:
return
}