MCPcopy Create free account
hub / github.com/goadesign/goa / Validate

Method Validate

expr/http_error.go:28–86  ·  view source on GitHub ↗

Validate makes sure there is a error expression that matches the HTTP error expression.

()

Source from the content-addressed store, hash-verified

26// Validate makes sure there is a error expression that matches the HTTP error
27// expression.
28func (e *HTTPErrorExpr) Validate() *eval.ValidationErrors {
29 verr := new(eval.ValidationErrors)
30 switch p := e.Response.Parent.(type) {
31 case *HTTPEndpointExpr:
32 if p.MethodExpr.Error(e.Name) == nil {
33 verr.Add(e, "Error %#v does not match an error defined in the method", e.Name)
34 }
35 case *HTTPServiceExpr:
36 if p.Error(e.Name) == nil {
37 verr.Add(e, "Error %#v does not match an error defined in the service", e.Name)
38 }
39 case *RootExpr:
40 if Root.Error(e.Name) == nil {
41 verr.Add(e, "Error %#v does not match an error defined in the API", e.Name)
42 }
43 }
44
45 var ee *ErrorExpr
46 switch p := e.Response.Parent.(type) {
47 case *HTTPEndpointExpr:
48 ee = p.MethodExpr.Error(e.Name)
49 case *HTTPServiceExpr:
50 ee = p.Error(e.Name)
51 case *RootExpr:
52 ee = Root.Error(e.Name)
53 }
54
55 // validate headers
56 if e.Response.Headers != nil && !e.Response.Headers.IsEmpty() {
57 verr.Merge(e.Response.Headers.Validate("HTTP error response headers", e.Response))
58 switch {
59 case ee.Type == Empty:
60 verr.Add(e.Response, "response defines headers but error type is empty")
61 case IsObject(ee.Type):
62 for _, h := range *AsObject(e.Response.Headers.Type) {
63 att := ee.Find(h.Name)
64 switch {
65 case att == nil:
66 verr.Add(e.Response, "header %q has no equivalent attribute in error type, use notation 'attribute_name:header_name' to identify corresponding error type attribute.", h.Name)
67 case IsArray(att.Type):
68 if !IsPrimitive(AsArray(att.Type).ElemType.Type) {
69 verr.Add(e.Response, "attribute %q used in HTTP headers must be a primitive type or an array of primitive types.", h.Name)
70 }
71 case !IsPrimitive(att.Type):
72 verr.Add(e.Response, "attribute %q used in HTTP headers must be a primitive type or an array of primitive types.", h.Name)
73 }
74 }
75 case len(*AsObject(e.Response.Headers.Type)) > 1:
76 verr.Add(e.Response, "response defines more than one headers but error type is not an object")
77 case IsArray(ee.Type):
78 if !IsPrimitive(AsArray(ee.Type).ElemType.Type) {
79 verr.Add(e.Response, "Array error type is mapped to an HTTP header but is not an array of primitive types.")
80 }
81 case IsMap(ee.Type):
82 verr.Add(e.Response, "error type must be a primitive type or an array of primitive types.")
83 }
84 }
85 return verr

Callers

nothing calls this directly

Calls 12

IsObjectFunction · 0.85
AsObjectFunction · 0.85
IsArrayFunction · 0.85
IsPrimitiveFunction · 0.85
AsArrayFunction · 0.85
IsMapFunction · 0.85
AddMethod · 0.80
IsEmptyMethod · 0.80
FindMethod · 0.80
ValidateMethod · 0.65
ErrorMethod · 0.45
MergeMethod · 0.45

Tested by

no test coverage detected