-
Notifications
You must be signed in to change notification settings - Fork 31
/
schema.go
358 lines (312 loc) · 8.63 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package types
import (
"encoding/json"
"errors"
"fmt"
"github.com/xeipuuv/gojsonschema"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const PATH_BODY = "body"
// ServiceSchemas defines the service schemas
type ServiceSchemas struct {
Input map[string]interface{} `json:"input"`
Output map[string]interface{} `json:"output"`
}
// ValidateServiceSchemas validates the given service schemas
func ValidateServiceSchemas(schemas string) error {
if len(schemas) == 0 {
return sdkerrors.Wrap(ErrInvalidSchemas, "schemas missing")
}
svcSchemas, err := parseServiceSchemas(schemas)
if err != nil {
return err
}
if err := validateInputSchema(svcSchemas.Input); err != nil {
return err
}
if err := validateOutputSchema(svcSchemas.Output); err != nil {
return err
}
return nil
}
// ValidateBindingPricing validates the given pricing against the Pricing JSON Schema
func ValidateBindingPricing(pricing string) error {
if len(pricing) == 0 {
return sdkerrors.Wrap(ErrInvalidPricing, "pricing missing")
}
if err := validateDocument([]byte(PricingSchema), pricing); err != nil {
return sdkerrors.Wrap(ErrInvalidPricing, err.Error())
}
return nil
}
// ValidateRequestInput validates the request input against the input schema
func ValidateRequestInput(input string) error {
if err := validateDocument([]byte(InputSchema), input); err != nil {
return sdkerrors.Wrap(ErrInvalidRequestInput, err.Error())
}
return nil
}
func ValidateRequestInputBody(schemas string, inputBody string) error {
inputSchemaBz, err := parseInputSchema(schemas)
if err != nil {
return err
}
if err := validateDocument(inputSchemaBz, inputBody); err != nil {
return sdkerrors.Wrap(ErrInvalidRequestInputBody, err.Error())
}
return nil
}
// ValidateResponseResult validates the response result against the result schema
func ValidateResponseResult(result string) error {
if len(result) == 0 {
return sdkerrors.Wrap(ErrInvalidResponseResult, "result missing")
}
if err := validateDocument([]byte(ResultSchema), result); err != nil {
return sdkerrors.Wrap(ErrInvalidResponseResult, err.Error())
}
return nil
}
// ValidateResponseOutput validates the response output against the output schema
func ValidateResponseOutput(output string) error {
if err := validateDocument([]byte(OutputSchema), output); err != nil {
return sdkerrors.Wrap(ErrInvalidResponseOutput, err.Error())
}
return nil
}
func ValidateResponseOutputBody(schemas string, outputBody string) error {
outputSchemaBz, err := parseOutputSchema(schemas)
if err != nil {
return err
}
if err := validateDocument(outputSchemaBz, outputBody); err != nil {
return sdkerrors.Wrap(ErrInvalidResponseOutputBody, err.Error())
}
return nil
}
func validateInputSchema(inputSchema map[string]interface{}) error {
inputSchemaBz, err := json.Marshal(inputSchema)
if err != nil {
return sdkerrors.Wrap(ErrInvalidSchemas, fmt.Sprintf("failed to marshal the input schema: %s", err))
}
if _, err = gojsonschema.NewSchema(gojsonschema.NewBytesLoader(inputSchemaBz)); err != nil {
return sdkerrors.Wrap(ErrInvalidSchemas, fmt.Sprintf("invalid input schema: %s", err))
}
return nil
}
func validateOutputSchema(outputSchema map[string]interface{}) error {
outputSchemaBz, err := json.Marshal(outputSchema)
if err != nil {
return sdkerrors.Wrap(ErrInvalidSchemas, fmt.Sprintf("failed to marshal the output schema: %s", err))
}
if _, err = gojsonschema.NewSchema(gojsonschema.NewBytesLoader(outputSchemaBz)); err != nil {
return sdkerrors.Wrap(ErrInvalidSchemas, fmt.Sprintf("invalid output schema: %s", err))
}
return nil
}
// parseServiceSchemas parses the given schemas to ServiceSchemas
func parseServiceSchemas(schemas string) (ServiceSchemas, error) {
var svcSchemas ServiceSchemas
if err := json.Unmarshal([]byte(schemas), &svcSchemas); err != nil {
return svcSchemas, sdkerrors.Wrap(ErrInvalidSchemas, fmt.Sprintf("failed to unmarshal the schemas: %s", err))
}
return svcSchemas, nil
}
// parseInputSchema parses the input schema from the given schemas
func parseInputSchema(schemas string) ([]byte, error) {
svcSchemas, err := parseServiceSchemas(schemas)
if err != nil {
return nil, err
}
inputSchemaBz, err := json.Marshal(svcSchemas.Input)
if err != nil {
return nil, sdkerrors.Wrap(ErrInvalidSchemas, fmt.Sprintf("failed to marshal the input schema: %s", err))
}
return inputSchemaBz, nil
}
// parseOutputSchema parses the output schema from the given schemas
func parseOutputSchema(schemas string) ([]byte, error) {
svcSchemas, err := parseServiceSchemas(schemas)
if err != nil {
return nil, err
}
outputSchemaBz, err := json.Marshal(svcSchemas.Output)
if err != nil {
return nil, sdkerrors.Wrap(ErrInvalidSchemas, fmt.Sprintf("failed to marshal the output schema: %s", err))
}
return outputSchemaBz, nil
}
// validateDocument wraps the gojsonschema validation
func validateDocument(schema []byte, document string) error {
schemaLoader := gojsonschema.NewBytesLoader(schema)
docLoader := gojsonschema.NewStringLoader(document)
res, err := gojsonschema.Validate(schemaLoader, docLoader)
if err != nil {
return err
}
if !res.Valid() {
for _, e := range res.Errors() {
return errors.New(e.String()) // only return the first error
}
}
return nil
}
const (
// PricingSchema is the Pricing JSON Schema
PricingSchema = `
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "iservice-pricing",
"description": "Interchain Service Pricing Schema",
"type": "object",
"definitions": {
"discount": {
"description": "promotion discount, greater than 0 and less than 1",
"type": "string",
"pattern": "^0\\.\\d*[1-9]$"
},
"promotion_by_time": {
"description": "promotion by time",
"type": "object",
"properties": {
"start_time": {
"description": "starting time of the promotion",
"type": "string",
"format": "date-time"
},
"end_time": {
"description": "ending time of the promotion",
"type": "string",
"format": "date-time"
},
"discount": {
"$ref": "#/definitions/discount"
}
},
"additionalProperties": false,
"required": [
"start_time",
"end_time",
"discount"
]
},
"promotion_by_volume": {
"description": "promotion by volume",
"type": "object",
"properties": {
"volume": {
"description": "minimal volume for the promotion",
"type": "integer",
"minimum": 1
},
"discount": {
"$ref": "#/definitions/discount"
}
},
"additionalProperties": false,
"required": [
"volume",
"discount"
]
}
},
"properties": {
"price": {
"description": "base price in min unit, e.g. 5uiris",
"type": "string",
"pattern": "^\\d+[a-zA-Z][a-zA-Z0-9/]{2,127}$"
},
"promotions_by_time": {
"description": "promotions by time, in ascending order",
"type": "array",
"items": {
"$ref": "#/definitions/promotion_by_time"
},
"maxItems": 5,
"uniqueItems": true
},
"promotions_by_volume": {
"description": "promotions by volume, in ascending order",
"type": "array",
"items": {
"$ref": "#/definitions/promotion_by_volume"
},
"maxItems": 5,
"uniqueItems": true
}
},
"additionalProperties": false,
"required": [
"price"
]
}`
// ResultSchema is the JSON Schema for the response result
ResultSchema = `
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "iservice-result",
"description": "Interchain Service Result Schema",
"type": "object",
"properties": {
"code": {
"description": "result code",
"type": "integer",
"enum": [
200,
400,
500
]
},
"message": {
"description": "result message",
"type": "string"
}
},
"additionalProperties": false,
"required": [
"code",
"message"
]
}`
// InputSchema is the JSON Schema for the request input
InputSchema = `
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "iservice-input",
"description": "Interchain Service Input schema",
"type": "object",
"properties": {
"header": {
"description": "header",
"type": "object"
},
"body": {
"description": "body",
"type": "object"
}
},
"required": [
"header"
]
}`
// OutputSchema is the JSON Schema for the response output
OutputSchema = `
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "iservice-output",
"description": "Interchain Service Output Schema",
"type": "object",
"properties": {
"header": {
"description": "header",
"type": "object"
},
"body": {
"description": "body",
"type": "object"
}
},
"required": [
"header"
]
}`
)