1
1
/*
2
2
* moleculer
3
- * Copyright (c) 2020 MoleculerJS (https://github.com/moleculerjs/moleculer)
3
+ * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer)
4
4
* MIT Licensed
5
5
*/
6
6
@@ -11,30 +11,57 @@ const { ValidationError } = require("../errors");
11
11
const BaseValidator = require ( "./base" ) ;
12
12
const _ = require ( "lodash" ) ;
13
13
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
+ */
14
30
class FastestValidator extends BaseValidator {
31
+ /**
32
+ * Creates an instance of FastestValidator.
33
+ *
34
+ * @param {FastestValidatorOptions } opts
35
+ *
36
+ */
15
37
constructor ( opts ) {
16
38
super ( opts ) ;
39
+ /** @type {FastestValidatorOptions } */
17
40
this . opts = _ . defaultsDeep ( this . opts , {
18
41
useNewCustomCheckerFunction : true
19
42
} ) ;
43
+
44
+ /** @type {Validator } */
45
+ // @ts -ignore
20
46
this . validator = new Validator ( this . opts ) ;
21
47
}
22
48
23
49
/**
24
50
* Compile a validation schema to a checker function.
25
51
* Need a clone because FV manipulate the schema (removing $$... props)
26
52
*
27
- * @param {any } schema
28
- * @returns {Function }
53
+ * @param {Record<string, any> } schema
54
+ * @returns {CheckerFunction }
29
55
*/
30
56
compile ( schema ) {
31
57
return this . validator . compile ( _ . cloneDeep ( schema ) ) ;
32
58
}
33
59
34
60
/**
35
61
* 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
38
65
* @returns {boolean }
39
66
*/
40
67
validate ( params , schema ) {
@@ -48,7 +75,7 @@ class FastestValidator extends BaseValidator {
48
75
* Convert the specific validation schema to
49
76
* the Moleculer (fastest-validator) validation schema format.
50
77
*
51
- * @param {any } schema
78
+ * @param {Record<string, any> } schema
52
79
* @returns {Object }
53
80
*/
54
81
convertSchemaToMoleculer ( schema ) {
0 commit comments