-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
202 lines (152 loc) · 5.09 KB
/
schema.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package schema
import (
"encoding/json"
"reflect"
"github.com/benpate/derp"
"github.com/benpate/exp"
"github.com/benpate/rosetta/compare"
"github.com/benpate/rosetta/convert"
"github.com/benpate/rosetta/list"
)
// Schema defines a (simplified) JSON-Schema object, that can be Marshalled/Unmarshalled to JSON.
type Schema struct {
ID string
Comment string
Element Element
}
// New generates a fully initialized Schema object using the provided properties
func New(element Element) Schema {
return Schema{
Element: element,
}
}
/******************************************
* Validation Methods
******************************************/
// Validate checks a particular value against this schema, updating values when
// possible so that they pass validation. If the provided value is not valid
// (and cannot be coerced into being valid) then it returns an error.
func (schema Schema) Validate(value any) error {
// RULE: Schema element cannot be nil
if isNil(schema.Element) {
return derp.NewInternalError("schema.Schema.Validate", "Schema is nil")
}
// Validate all elements in the value
if err := schema.Element.Validate(value); err != nil {
return derp.Wrap(err, "schema.Schema.Validate", "Error validating value", value)
}
// Handle special cases for "required-if" fields
if err := schema.ValidateRequiredIf(value); err != nil {
return derp.Wrap(err, "schema.Schema.Validate", "Error validating required-if fields", value)
}
return nil
}
// Match returns TRUE if the provided value (as accessed via this schema) matches
// the provided expression. This is useful for server-side data validation.
func (schema Schema) Match(value any, expression exp.Expression) bool {
// Evaluate the predicate
return expression.Match(func(predicate exp.Predicate) bool {
fieldValue, err := schema.Get(value, predicate.Field)
if err != nil {
return false
}
switch predicate.Operator {
case exp.OperatorEqual:
return compare.Equal(fieldValue, predicate.Value)
case exp.OperatorGreaterThan:
return compare.GreaterThan(fieldValue, predicate.Value)
case exp.OperatorLessThan:
return compare.LessThan(fieldValue, predicate.Value)
case exp.OperatorNotEqual:
return !compare.Equal(fieldValue, predicate.Value)
case exp.OperatorGreaterOrEqual:
return !compare.LessThan(fieldValue, predicate.Value)
case exp.OperatorLessOrEqual:
return !compare.GreaterThan(fieldValue, predicate.Value)
}
return false
})
}
func (schema Schema) ValidateRequiredIf(value any) error {
return schema.Element.ValidateRequiredIf(schema, list.ByDot(""), value)
}
/******************************************
* Other Data Access Methods
******************************************/
func (schema Schema) GetElement(path string) (Element, bool) {
if isNil(schema.Element) {
return nil, false
}
return schema.Element.GetElement(path)
}
func (schema *Schema) Inherit(parent Schema) {
if isNil(schema.Element) {
schema.Element = parent.Element
} else {
schema.Element.Inherit(parent.Element)
}
}
/******************************************
* Marshaling Methods
******************************************/
// MarshalJSON converts a schema into JSON.
func (schema Schema) MarshalJSON() ([]byte, error) {
if isNil(schema.Element) {
return []byte("null"), nil
}
return json.Marshal(schema.MarshalMap())
}
// MarshalMap converts a schema into a map[string]any
func (schema Schema) MarshalMap() map[string]any {
if isNil(schema.Element) {
return map[string]any{}
}
result := schema.Element.MarshalMap()
if schema.ID != "" {
result["$id"] = schema.ID
}
if schema.Comment != "" {
result["$comment"] = schema.Comment
}
return result
}
/***********************************
* Marshal / Unmarshal Methods
***********************************/
// UnmarshalJSON creates a new Schema object using a JSON-serialized byte array.
func (schema *Schema) UnmarshalJSON(data []byte) error {
unmarshalled := make(map[string]any)
if err := json.Unmarshal(data, &unmarshalled); err != nil {
return derp.Wrap(err, "schema.UnmarshalJSON", "Invalid JSON", string(data))
}
if err := schema.UnmarshalMap(unmarshalled); err != nil {
return derp.Wrap(err, "schema.UnmarshalJSON", "Unable to unmarshal from Map", unmarshalled)
}
return nil
}
// UnmarshalMap updates a Schema using a map[string]any
func (schema *Schema) UnmarshalMap(data map[string]any) error {
schema.ID = convert.String(data["$id"])
schema.Comment = convert.String(data["$comment"])
element, err := UnmarshalMap(data)
if err != nil {
return derp.Wrap(err, "schema.Schema.UnmarshalMap", "Error unmarshalling element")
}
schema.Element = element
return nil
}
/******************************************
* Other Helpers
******************************************/
// isNil performs a robust nil check on an interface
// Shout out to: https://medium.com/@mangatmodi/go-check-nil-interface-the-right-way-d142776edef1
func isNil(i any) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Array, reflect.Slice, reflect.Chan, reflect.Map:
return reflect.ValueOf(i).IsNil()
}
return false
}