-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathis-secure-string.ts
49 lines (44 loc) · 1.47 KB
/
is-secure-string.ts
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
import StringUtils from "../Utils/StringUtils/index.js";
import { SchemaValidateFunction } from "ajv";
import {
DataValidateFunction,
KeywordDefinition
} from "ajv/dist/types/index.js";
/**
* The 'compile' method is a function that AJV calls during the schema compilation.
* It should return a validation function that will be used by AJV for data validation.
* In our case, the validation function uses the StringUtils.isSecureString method
* to check if the string is secure, and if not, throws an error with an appropriate
* message and parameters.
*
* @property {string} keyword - The name of the keyword.
* @property {string} type - The type of data the keyword applies to.
* @property {boolean} errors - Indicates that the keyword returns errors.
* @property {function} compile - A function that AJV calls during schema compilation.
*/
const keyword: KeywordDefinition = {
keyword: "secure-string",
type: "string",
errors: true,
compile: function compile() {
const validate: SchemaValidateFunction | DataValidateFunction = (
data: any
) => {
validate.errors = [];
const { isValid, errorMessage } = StringUtils.isSecureString(data);
if (!isValid) {
validate.errors = [
{
keyword: "secure-string",
message: `${errorMessage}`,
params: { invalidInput: data }
}
];
return false;
}
return true;
};
return validate;
}
};
export default keyword;