• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moleculerjs / moleculer / 6997741482

26 Nov 2023 08:09PM UTC coverage: 94.161% (-0.001%) from 94.162%
6997741482

push

github

icebob
types: loggers

5314 of 5865 branches covered (0.0%)

Branch coverage included in aggregate %.

7201 of 7426 relevant lines covered (96.97%)

998.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.74
/src/validators/base.js
1
/*
2
 * moleculer
3
 * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
4
 * MIT Licensed
5
 */
6

7
/* eslint-disable no-unused-vars */
8

9
"use strict";
10

11
const { ValidationError } = require("../errors");
612✔
12
const _ = require("lodash");
612✔
13

14
/**
15
 * Import types
16
 *
17
 * @typedef {import("../service-broker")} ServiceBroker
18
 * @typedef {import("../context")} Context
19
 * @typedef {import("./base")} BaseValidatorClass
20
 * @typedef {import("./base").ValidatorOptions} ValidatorOptions
21
 * @typedef {import("./base").CheckerFunction} CheckerFunction
22
 */
23

24
/**
25
 * Abstract validator class
26
 *
27
 * @implements {BaseValidatorClass}
28
 */
29
class BaseValidator {
30
        /**
31
         * Creates an instance of Validator.
32
         *
33
         * @param {ValidatorOptions} opts
34
         *
35
         * @memberof Cacher
36
         */
37
        constructor(opts) {
38
                /** @type {ValidatorOptions} */
39
                this.opts = _.defaultsDeep(opts, {
4,044✔
40
                        paramName: "params"
41
                });
42
        }
43

44
        /**
45
         * Initialize cacher
46
         *
47
         * @param {ServiceBroker} broker
48
         *
49
         * @memberof Cacher
50
         */
51
        init(broker) {
52
                this.broker = broker;
3,972✔
53
        }
54

55
        /**
56
         * Compile a validation schema to a checker function.
57
         *
58
         * @param {Record<string, any>} schema
59
         * @returns {CheckerFunction}
60
         */
61
        compile(schema) {
62
                throw new Error("Abstract method");
×
63
        }
64

65
        /**
66
         * Validate params againt the schema
67
         *
68
         * @param {Record<string, any>} params
69
         * @param {Record<string, any>} schema
70
         * @returns {boolean}
71
         */
72
        validate(params, schema) {
73
                throw new Error("Abstract method");
×
74
        }
75

76
        /**
77
         * Convert the specific validation schema to
78
         * the Moleculer (fastest-validator) validation schema format.
79
         *
80
         * @param {Record<string, any>} schema
81
         * @returns {Object}
82
         */
83
        convertSchemaToMoleculer(schema) {
84
                throw new Error("Abstract method");
×
85
        }
86

87
        /**
88
         * Register validator as a middleware
89
         *
90
         * @param {ServiceBroker} broker
91
         *
92
         * @memberof BaseValidator
93
         */
94
        middleware(broker) {
95
                const self = this;
3,984✔
96
                const paramName = this.opts.paramName;
3,984✔
97

98
                const processCheckResponse = function (ctx, handler, res, additionalInfo) {
3,984✔
99
                        if (res === true) return handler(ctx);
186✔
100
                        else {
101
                                res = res.map(data => Object.assign(data, additionalInfo));
36✔
102
                                return broker.Promise.reject(
36✔
103
                                        new ValidationError("Parameters validation error!", null, res)
104
                                );
105
                        }
106
                };
107

108
                return {
3,984✔
109
                        name: "Validator",
110
                        localAction: function validatorMiddleware(handler, action) {
111
                                // Wrap a param validator
112
                                if (action[paramName] && typeof action[paramName] === "object") {
31,080✔
113
                                        const check = self.compile(action[paramName]);
25,362✔
114
                                        return function validateContextParams(ctx) {
25,362✔
115
                                                const res = check(ctx.params != null ? ctx.params : {}, { meta: ctx });
156!
116
                                                if (check.async)
156✔
117
                                                        return res.then(res =>
12✔
118
                                                                processCheckResponse(ctx, handler, res, {
12✔
119
                                                                        nodeID: ctx.nodeID,
120
                                                                        action: ctx.action.name
121
                                                                })
122
                                                        );
123
                                                else
124
                                                        return processCheckResponse(ctx, handler, res, {
144✔
125
                                                                nodeID: ctx.nodeID,
126
                                                                action: ctx.action.name
127
                                                        });
128
                                        };
129
                                }
130
                                return handler;
5,718✔
131
                        },
132

133
                        localEvent: function validatorMiddleware(handler, event) {
134
                                // Wrap a param validator
135
                                if (event[paramName] && typeof event[paramName] === "object") {
324✔
136
                                        const check = self.compile(event[paramName]);
30✔
137
                                        return function validateContextParams(ctx) {
30✔
138
                                                const res = check(ctx.params != null ? ctx.params : {}, { meta: ctx });
30!
139

140
                                                if (check.async)
30✔
141
                                                        return res.then(res =>
12✔
142
                                                                processCheckResponse(ctx, handler, res, {
12✔
143
                                                                        nodeID: ctx.nodeID,
144
                                                                        event: ctx.event.name
145
                                                                })
146
                                                        );
147
                                                else
148
                                                        return processCheckResponse(ctx, handler, res, {
18✔
149
                                                                nodeID: ctx.nodeID,
150
                                                                event: ctx.event.name
151
                                                        });
152
                                        };
153
                                }
154
                                return handler;
294✔
155
                        }
156
                };
157
        }
158
}
159

160
module.exports = BaseValidator;
612✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc