aboutsummaryrefslogtreecommitdiff
path: root/rpc/middleware/validation.go
blob: c18ce4f2916fb03b02c06006d2875852845a495a (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Package middleware provides middlewares for rpc server
//
//Copyright (C) 2022 Alexander Kiryukhin <i@neonxp.dev>
//
//This file is part of neonxp.ru/go/jsonrpc2 project.
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <https://www.gnu.org/licenses/>.

package middleware

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/qri-io/jsonschema"

	"neonxp.ru/go/jsonrpc2/rpc"
)

type ServiceSchema map[string]MethodSchema

func MustSchema(schema string) ServiceSchema {
	ss := new(ServiceSchema)
	if err := json.Unmarshal([]byte(schema), ss); err != nil {
		panic(err)
	}
	return *ss
}

type MethodSchema struct {
	Request  *jsonschema.Schema `json:"request"`
	Response *jsonschema.Schema `json:"response"`
}

func Validation(serviceSchema ServiceSchema) (rpc.Middleware, error) {
	return func(handler rpc.RpcHandler) rpc.RpcHandler {
		return func(ctx context.Context, req *rpc.RpcRequest) *rpc.RpcResponse {
			rs, hasSchema := serviceSchema[strings.ToLower(req.Method)]
			if hasSchema && rs.Request != nil {
				if errResp := formatError(ctx, req.Id, *rs.Request, req.Params); errResp != nil {
					return errResp
				}
			}
			resp := handler(ctx, req)
			if hasSchema && rs.Response != nil {
				if errResp := formatError(ctx, req.Id, *rs.Response, resp.Result); errResp != nil {
					return errResp
				}
			}
			return resp
		}
	}, nil
}

func formatError(ctx context.Context, requestId any, schema jsonschema.Schema, data json.RawMessage) *rpc.RpcResponse {
	errs, err := schema.ValidateBytes(ctx, data)
	if err != nil {
		return rpc.ErrorResponse(requestId, err)
	}
	if errs != nil && len(errs) > 0 {
		messages := []string{}
		for _, msg := range errs {
			messages = append(messages, fmt.Sprintf("%s: %s", msg.PropertyPath, msg.Message))
		}
		return rpc.ErrorResponse(requestId, rpc.Error{
			Code:    rpc.ErrCodeInvalidParams,
			Message: strings.Join(messages, "\n"),
		})
	}
	return nil
}