validateParams checks the endpoint parameters are of an allowed type and the method payload contains the parameters.
()
| 843 | // validateParams checks the endpoint parameters are of an allowed type and the |
| 844 | // method payload contains the parameters. |
| 845 | func (e *HTTPEndpointExpr) validateParams() *eval.ValidationErrors { |
| 846 | if e.Params.IsEmpty() { |
| 847 | return nil |
| 848 | } |
| 849 | |
| 850 | var ( |
| 851 | pparams = DupMappedAtt(e.PathParams()) |
| 852 | qparams = DupMappedAtt(e.QueryParams()) |
| 853 | ) |
| 854 | // We have to figure out the actual type for the params because the actual |
| 855 | // type is initialized only during the finalize phase. In the validation |
| 856 | // phase, all param types are string type by default unless specified |
| 857 | // explicitly. |
| 858 | initAttr(pparams, e.MethodExpr.Payload) |
| 859 | initAttr(qparams, e.MethodExpr.Payload) |
| 860 | |
| 861 | invalidTypeErr := func(verr *eval.ValidationErrors, e *HTTPEndpointExpr, name string) { |
| 862 | verr.Add(e, "path parameter %s cannot be an object, path parameter types must be primitive, array or map (query string only)", name) |
| 863 | } |
| 864 | verr := new(eval.ValidationErrors) |
| 865 | WalkMappedAttr(pparams, func(name, _ string, a *AttributeExpr) error { // nolint: errcheck |
| 866 | switch { |
| 867 | case IsObject(a.Type), IsMap(a.Type), IsUnion(a.Type): |
| 868 | invalidTypeErr(verr, e, name) |
| 869 | case IsArray(a.Type): |
| 870 | arr := AsArray(a.Type) |
| 871 | if !IsPrimitive(arr.ElemType.Type) { |
| 872 | verr.Add(e, "elements of array path parameter %q must be primitive", name) |
| 873 | } |
| 874 | default: |
| 875 | ctx := fmt.Sprintf("path parameter %s", name) |
| 876 | verr.Merge(a.Validate(ctx, e)) |
| 877 | } |
| 878 | return nil |
| 879 | }) |
| 880 | WalkMappedAttr(qparams, func(name, _ string, a *AttributeExpr) error { // nolint: errcheck |
| 881 | switch { |
| 882 | case IsObject(a.Type), IsUnion(a.Type): |
| 883 | invalidTypeErr(verr, e, name) |
| 884 | case IsArray(a.Type): |
| 885 | arr := AsArray(a.Type) |
| 886 | if !IsPrimitive(arr.ElemType.Type) { |
| 887 | verr.Add(e, "elements of array query parameter %q must be primitive", name) |
| 888 | } |
| 889 | default: |
| 890 | ctx := fmt.Sprintf("query parameter %s", name) |
| 891 | verr.Merge(a.Validate(ctx, e)) |
| 892 | } |
| 893 | return nil |
| 894 | }) |
| 895 | if e.MethodExpr.Payload != nil { |
| 896 | switch e.MethodExpr.Payload.Type.(type) { |
| 897 | case *Object, UserType: |
| 898 | WalkMappedAttr(pparams, func(name, _ string, _ *AttributeExpr) error { // nolint: errcheck |
| 899 | if e.MethodExpr.Payload.Find(name) == nil { |
| 900 | verr.Add(e, "Path parameter %q not found in payload.", name) |
| 901 | } |
| 902 | return nil |
no test coverage detected