Guest User

Untitled

a guest
Aug 22nd, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. {
  2. "users": {
  3. "PUT": {
  4. "required": [
  5. "DisplayName",
  6. "Username",
  7. "Email",
  8. "Password"
  9. ],
  10. "properties": {
  11. "DisplayName": {
  12. "$id": "#/properties/DisplayName",
  13. "type": "string",
  14. "title": "The DisplayName Schema",
  15. "examples": [
  16. "1"
  17. ],
  18. "minLength": 3,
  19. "pattern": "^(.*)$"
  20. },
  21. "Username": {
  22. "$id": "#/properties/Username",
  23. "type": "string",
  24. "title": "The Username Schema",
  25. "examples": [
  26. "2"
  27. ],
  28. "pattern": "^(.*)$"
  29. },
  30. "Email": {
  31. "$id": "#/properties/Email",
  32. "type": "string",
  33. "title": "The Email Schema",
  34. "examples": [
  35. "3"
  36. ],
  37. "minLength": 3,
  38. "pattern": "^(.*)$",
  39. "format": "email"
  40. },
  41. "Password": {
  42. "$id": "#/properties/Password",
  43. "type": "string",
  44. "title": "The Password Schema",
  45. "examples": [
  46. "4"
  47. ],
  48. "pattern": "^(.*)$"
  49. }
  50. }
  51. "additionalProperties":false
  52. }
  53. }
  54. }
  55.  
  56. func Validate(data interface{}, r *http.Request) (interface{}, error) {
  57. // Convert the data struct to a readable JSON bytes
  58. JSONparams, err := json.Marshal(data)
  59. if err != nil {
  60. return nil, err
  61. }
  62.  
  63. // Split URL segments so we know what part of the API they are accessing
  64. modules := strings.Split(r.URL.String(), "/")
  65. modules = modules[(len(modules) - 1):]
  66.  
  67. // Read the schema file
  68. fileSchema, _ := ioutil.ReadFile("config/schema/schema.json")
  69. var object interface{}
  70.  
  71. // Unmarshal it so we can choose what schema we specifically want
  72. err = json.Unmarshal(fileSchema, &object)
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76.  
  77. // Choose the preferred schema
  78. encodedJSON, err := json.Marshal(object.(map[string]interface{})[strings.Join(modules, "") + "s"].(map[string]interface{})[r.Method])
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82.  
  83. // Load the JSON schema
  84. schema := gojsonschema.NewStringLoader(string(encodedJSON))
  85.  
  86. // Load the JSON params
  87. document := gojsonschema.NewStringLoader(string(JSONparams))
  88.  
  89. // Validate the document
  90. result, err := gojsonschema.Validate(schema, document)
  91. if err != nil {
  92. return nil, err
  93. }
  94.  
  95. if !result.Valid() {
  96. // Map the errors into a new array
  97. var errors = make(map[string]string)
  98. for _, err := range result.Errors() {
  99. errors[err.Field()] = err.Description()
  100. }
  101.  
  102. // Convert the array to an interface that we can convert to JSON
  103. resultMap := map[string]interface{}{
  104. "success": false,
  105. "result": map[string]interface{}{},
  106. "errors": errors,
  107. }
  108.  
  109. // Convert the interface to a JSON object
  110. errorObject, err := json.Marshal(resultMap)
  111. if err != nil {
  112. return nil, err
  113. }
  114.  
  115. return errorObject, nil
  116. }
  117.  
  118. return nil, nil
  119. }
  120.  
  121. type CreateParams struct {
  122. DisplayName string
  123. Username string
  124. Email string
  125. Password string
  126. }
  127.  
  128. var (
  129. response interface{}
  130. status int = 0
  131. )
  132.  
  133. func Create(w http.ResponseWriter, r *http.Request) {
  134. status = 0
  135.  
  136. // Parse the request so we can access the query parameters
  137. r.ParseForm()
  138.  
  139. // Assign them to the interface variables
  140. data := &CreateParams{
  141. DisplayName: r.Form.Get("DisplayName"),
  142. Username: r.Form.Get("Username"),
  143. Email: r.Form.Get("Email"),
  144. Password: r.Form.Get("Password"),
  145. }
  146.  
  147. // Validate the JSON data
  148. errors, err := schema.Validate(data, r)
  149.  
  150. if err != nil {
  151. responseJSON := map[string]interface{}{
  152. "success": false,
  153. "result": map[string]interface{}{},
  154. }
  155.  
  156. log.Fatal(err.Error())
  157.  
  158. response, err = json.Marshal(responseJSON)
  159. status = http.StatusInternalServerError
  160. }
  161.  
  162. // Catch any errors generated by the validator and assign them to the response interface
  163. if errors != nil {
  164. response = errors
  165. status = http.StatusBadRequest
  166. }
  167.  
  168. // Status has not been set yet, so it's safe to assume that everything went fine
  169. if status == 0 {
  170. responseJSON := map[string]interface{}{
  171. "success": true,
  172. "result": map[string]interface{} {
  173. "DisplayName": data.DisplayName,
  174. "Username": data.Username,
  175. "Email": data.Email,
  176. "Password": nil,
  177. },
  178. }
  179.  
  180. response, err = json.Marshal(responseJSON)
  181. status = http.StatusOK
  182. }
  183.  
  184. // We are going to respond with JSON, so set the appropriate header
  185. w.Header().Set("Content-Type", "application/json")
  186.  
  187. // Write the header and the response
  188. w.WriteHeader(status)
  189. w.Write(response.([]byte))
  190. }
Add Comment
Please, Sign In to add comment