-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
46 lines (39 loc) · 1.01 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"reflect"
"regexp"
"github.com/Pilladian/go-helper"
)
func validatePID(id string) error {
re, _ := regexp.Compile(` (\w|_|-)+ `)
if !re.Match([]byte(fmt.Sprintf(" %s ", id))) {
return errors.New("ID did not match server requirements")
}
return nil
}
func validateData(data string) error {
if err := helper.ValidateSimpleJSON(data); err != nil {
return err
}
return nil
}
func validateSchema(path string, content string) error {
schema_b, schema_b_err := ioutil.ReadFile(path + "/schema.json")
if schema_b_err != nil {
return errors.New("File schema.json could not be opened")
}
var schema map[string]interface{}
json.Unmarshal(schema_b, &schema)
var content_json map[string]interface{}
json.Unmarshal([]byte(content), &content_json)
for key := range content_json {
if !(reflect.TypeOf((content_json[key])) == reflect.TypeOf(schema[key])) {
return errors.New("Provided data does not match current schema")
}
}
return nil
}