Skip to content

Commit d0a900b

Browse files
committed
types: validators
1 parent bcb4f9f commit d0a900b

File tree

8 files changed

+127
-30
lines changed

8 files changed

+127
-30
lines changed

src/cachers/base.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const { isObject, isFunction, isDate } = require("../utils");
1616
/**
1717
* Import types
1818
*
19+
* @typedef {import("../service-broker")} ServiceBroker
1920
* @typedef {import("../context")} Context
2021
* @typedef {import("./base").CacherOptions} CacherOptions
2122
* @typedef {import("./base")} CacherBaseClass
@@ -50,7 +51,7 @@ class Cacher {
5051
/**
5152
* Initialize cacher
5253
*
53-
* @param {any} broker
54+
* @param {ServiceBroker} broker
5455
*
5556
* @memberof Cacher
5657
*/

src/cachers/memory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MemoryCacher extends BaseCacher {
3030
/**
3131
* Creates an instance of MemoryCacher.
3232
*
33-
* @param {MemoryCacherOptions} opts
33+
* @param {MemoryCacherOptions?} opts
3434
*
3535
* @memberof MemoryCacher
3636
*/

src/validators/base.d.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ declare namespace BaseValidator {
55
export type ValidatorNames = "Fastest";
66

77
export interface ValidatorOptions {
8-
type: string;
9-
options?: Record<string, any>;
8+
paramName?: string;
109
}
10+
11+
export type CheckerFunction = Function & { async: boolean };
1112
}
13+
1214
declare abstract class BaseValidator {
13-
constructor();
15+
constructor(opts: BaseValidator.ValidatorOptions);
16+
17+
broker: ServiceBroker;
18+
opts: BaseValidator.ValidatorOptions;
1419

1520
init(broker: ServiceBroker): void;
1621

17-
compile(schema: Record<string, any>): Function;
22+
abstract compile(schema: Record<string, any>): BaseValidator.CheckerFunction;
23+
24+
abstract validate(params: Record<string, any>, schema: Record<string, any>): boolean;
1825

19-
validate(params: Record<string, any>, schema: Record<string, any>): boolean;
26+
abstract convertSchemaToMoleculer(schema: any): Record<string, any>;
2027

21-
middleware(): (handler: ActionHandler, action: ActionSchema) => any;
28+
middleware(broker: ServiceBroker);
2229

23-
convertSchemaToMoleculer(schema: any): Record<string, any>;
2430
}
2531
export = BaseValidator;

src/validators/base.js

+45-9
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,94 @@
11
/*
22
* moleculer
3-
* Copyright (c) 2020 MoleculerJS (https://github.com/moleculerjs/moleculer)
3+
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
44
* MIT Licensed
55
*/
66

7+
/* eslint-disable no-unused-vars */
8+
79
"use strict";
810

911
const { ValidationError } = require("../errors");
1012
const _ = require("lodash");
1113

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+
*/
1229
class BaseValidator {
30+
/**
31+
* Creates an instance of Validator.
32+
*
33+
* @param {ValidatorOptions} opts
34+
*
35+
* @memberof Cacher
36+
*/
1337
constructor(opts) {
38+
/** @type {ValidatorOptions} */
1439
this.opts = _.defaultsDeep(opts, {
1540
paramName: "params"
1641
});
1742
}
1843

44+
/**
45+
* Initialize cacher
46+
*
47+
* @param {ServiceBroker} broker
48+
*
49+
* @memberof Cacher
50+
*/
1951
init(broker) {
2052
this.broker = broker;
2153
}
2254

2355
/**
2456
* Compile a validation schema to a checker function.
25-
* @param {any} schema
26-
* @returns {Function}
57+
*
58+
* @param {Record<string, any>} schema
59+
* @returns {CheckerFunction}
2760
*/
28-
compile(/*schema*/) {
61+
compile(schema) {
2962
throw new Error("Abstract method");
3063
}
3164

3265
/**
3366
* Validate params againt the schema
34-
* @param {any} params
35-
* @param {any} schema
67+
*
68+
* @param {Record<string, any>} params
69+
* @param {Record<string, any>} schema
3670
* @returns {boolean}
3771
*/
38-
validate(/*params, schema*/) {
72+
validate(params, schema) {
3973
throw new Error("Abstract method");
4074
}
4175

4276
/**
4377
* Convert the specific validation schema to
4478
* the Moleculer (fastest-validator) validation schema format.
4579
*
46-
* @param {any} schema
80+
* @param {Record<string, any>} schema
4781
* @returns {Object}
4882
*/
49-
convertSchemaToMoleculer(/*schema*/) {
83+
convertSchemaToMoleculer(schema) {
5084
throw new Error("Abstract method");
5185
}
5286

5387
/**
5488
* Register validator as a middleware
5589
*
90+
* @param {ServiceBroker} broker
91+
*
5692
* @memberof BaseValidator
5793
*/
5894
middleware(broker) {

src/validators/fastest.d.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
import type BaseValidator = require("./base");
22

3-
declare class FastestValidator extends BaseValidator {}
3+
declare namespace FastestValidator {
4+
export type ValidatorNames = "Fastest";
5+
6+
export interface FastestValidatorOptions extends BaseValidator.ValidatorOptions {
7+
useNewCustomCheckerFunction?: string;
8+
[key: string]: any;
9+
}
10+
}
11+
12+
declare class FastestValidator extends BaseValidator {
13+
constructor(opts?: FastestValidator.FastestValidatorOptions);
14+
15+
opts: FastestValidator.FastestValidatorOptions;
16+
17+
compile(schema: Record<string, any>): BaseValidator.CheckerFunction;
18+
validate(params: Record<string, any>, schema: Record<string, any>): boolean;
19+
convertSchemaToMoleculer(schema: any): Record<string, any>;
20+
21+
}
422
export = FastestValidator;

src/validators/fastest.js

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* moleculer
3-
* Copyright (c) 2020 MoleculerJS (https://github.com/moleculerjs/moleculer)
3+
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
44
* MIT Licensed
55
*/
66

@@ -11,30 +11,57 @@ const { ValidationError } = require("../errors");
1111
const BaseValidator = require("./base");
1212
const _ = require("lodash");
1313

14+
/**
15+
* Import types
16+
*
17+
* @typedef {import("../service-broker")} ServiceBroker
18+
* @typedef {import("../context")} Context
19+
* @typedef {import("./fastest")} FastestValidatorClass
20+
* @typedef {import("./fastest").FastestValidatorOptions} FastestValidatorOptions
21+
* @typedef {import("./base").CheckerFunction} CheckerFunction
22+
* @typedef {import("fastest-validator").default} Validator
23+
*/
24+
25+
/**
26+
* Fastest validator class
27+
*
28+
* @implements {FastestValidatorClass}
29+
*/
1430
class FastestValidator extends BaseValidator {
31+
/**
32+
* Creates an instance of FastestValidator.
33+
*
34+
* @param {FastestValidatorOptions} opts
35+
*
36+
*/
1537
constructor(opts) {
1638
super(opts);
39+
/** @type {FastestValidatorOptions} */
1740
this.opts = _.defaultsDeep(this.opts, {
1841
useNewCustomCheckerFunction: true
1942
});
43+
44+
/** @type {Validator} */
45+
// @ts-ignore
2046
this.validator = new Validator(this.opts);
2147
}
2248

2349
/**
2450
* Compile a validation schema to a checker function.
2551
* Need a clone because FV manipulate the schema (removing $$... props)
2652
*
27-
* @param {any} schema
28-
* @returns {Function}
53+
* @param {Record<string, any>} schema
54+
* @returns {CheckerFunction}
2955
*/
3056
compile(schema) {
3157
return this.validator.compile(_.cloneDeep(schema));
3258
}
3359

3460
/**
3561
* Validate params against the schema
36-
* @param {any} params
37-
* @param {any} schema
62+
*
63+
* @param {Record<string, any>} params
64+
* @param {Record<string, any>} schema
3865
* @returns {boolean}
3966
*/
4067
validate(params, schema) {
@@ -48,7 +75,7 @@ class FastestValidator extends BaseValidator {
4875
* Convert the specific validation schema to
4976
* the Moleculer (fastest-validator) validation schema format.
5077
*
51-
* @param {any} schema
78+
* @param {Record<string, any>} schema
5279
* @returns {Object}
5380
*/
5481
convertSchemaToMoleculer(schema) {

src/validators/index.d.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import BaseValidator = require("./base");
1+
import Validator = require("./base");
22
import type { ValidatorNames, ValidatorOptions } from "./base";
33
import FastestValidator = require("./fastest");
44

5-
export { BaseValidator as Base, FastestValidator as Fastest, ValidatorNames, ValidatorOptions };
5+
export { Validator as Base, FastestValidator as Fastest, ValidatorNames, ValidatorOptions };
6+
7+
export declare function resolve(opt: Record<string,any>|string): Validator;
8+
export declare function register(name: string, value: Validator): void;

src/validators/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* moleculer
3-
* Copyright (c) 2020 MoleculerJS (https://github.com/moleculerjs/moleculer)
3+
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
44
* MIT Licensed
55
*/
66

@@ -25,8 +25,8 @@ function getByName(name) {
2525
/**
2626
* Resolve validator by name
2727
*
28-
* @param {object|string} opt
29-
* @returns {Validator}
28+
* @param {Record<string,any>|string} opt
29+
* @returns {any}
3030
* @memberof ServiceBroker
3131
*/
3232
function resolve(opt) {
@@ -49,6 +49,12 @@ function resolve(opt) {
4949
return new Validators.Fastest();
5050
}
5151

52+
/**
53+
* Register a custom validator
54+
*
55+
* @param {string} name
56+
* @param {any} value
57+
*/
5258
function register(name, value) {
5359
Validators[name] = value;
5460
}

0 commit comments

Comments
 (0)