github.com/keikoproj/manny@v0.0.0-20210726112440-8571e4c99ced/config/config.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/qri-io/jsonschema" 7 "gopkg.in/yaml.v3" 8 ) 9 10 var Schemaconfig = []byte( 11 `{ 12 "$schema": "http://json-schema.org/draft-04/schema#", 13 "type": "object", 14 "properties": { 15 "template": { 16 "type": "object", 17 "properties":{ 18 "type":{ 19 "type":"string", 20 "enum": [ "file", "http", "s3" ] 21 }, 22 "path": { 23 "type": "string" 24 } 25 } 26 }, 27 "parameters" : { 28 "type": ["object", "null"] 29 }, 30 "timeout":{ 31 "type": ["integer", "null"] 32 }, 33 "stackname":{ 34 "type": ["string", "null"] 35 }, 36 "tags":{ 37 "type": ["object", "null"] 38 }, 39 "rolearn":{ 40 "type": ["string", "null"] 41 }, 42 "servicerolearn":{ 43 "type": ["string", "null"] 44 }, 45 "externalID":{ 46 "type": ["string", "null"] 47 }, 48 "duration":{ 49 "type": ["string", "null"] 50 }, 51 "acctnum":{ 52 "type": ["string", "null"] 53 }, 54 "expirywindow":{ 55 "type": ["string", "null"] 56 }, 57 "env":{ 58 "type": ["string", "null"] 59 }, 60 "region":{ 61 "type": ["string", "null"] 62 }, 63 "syncwave":{ 64 "type": ["integer", "null"] 65 } 66 }, 67 "additionalProperties": false, 68 "required": [ 69 "template" 70 ] 71 }`) 72 73 func ValidateJSONSchema(schemaData []byte) (*jsonschema.RootSchema, error) { 74 rs := &jsonschema.RootSchema{} 75 if err := json.Unmarshal(schemaData, rs); err != nil { 76 return nil, fmt.Errorf( "schema unmarshalling error %s", err) 77 } 78 return rs, nil 79 } 80 81 82 func ConfigValidate(inputSchemaData, schemaData []byte) (bool, error) { 83 rs, err := ValidateJSONSchema(schemaData) 84 if err != nil{ 85 return false, fmt.Errorf("failed ValidateJSONSchema due to %s", err) 86 } 87 var body interface{} 88 if err := yaml.Unmarshal(inputSchemaData, &body); err != nil{ 89 return false, fmt.Errorf("yaml unmarshal failed inputSchemaData due to: %s", err) 90 } 91 inputContentJson, err := json.Marshal(body) 92 if err != nil { 93 return false, fmt.Errorf("json marshal failed inputContentJson due to: %s", err) 94 } 95 96 if errs, _ := rs.ValidateBytes(inputContentJson); len(errs) > 0 { 97 return false, fmt.Errorf("validate failed due to: %s", errs) 98 } 99 100 return true, nil 101 }