@diia-inhouse/validators 2.3.2 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/interfaces/rule.d.ts +13 -0
- package/dist/interfaces/schema.d.ts +7 -0
- package/dist/rules/buffer.js +6 -0
- package/dist/rules/date.js +13 -0
- package/dist/rules/internationalPhoneNumber.js +7 -0
- package/dist/rules/objectId.js +8 -0
- package/dist/rules/phoneNumber.js +6 -0
- package/dist/rules/version.js +5 -0
- package/dist/schemas/index.d.ts +24 -7
- package/dist/schemas/index.js +24 -60
- package/dist/validator.d.ts +30 -0
- package/dist/validator.js +30 -0
- package/package.json +4 -8
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ import { ParameterValidation } from "./interfaces/schema.js";
|
|
|
3
3
|
import { ArrayRule, BooleanRule, CheckerFunctionError, CustomDateRule, DateRule, EmailRule, EnumRule, ListValidationSchema, NumberRule, ObjectIdRule, ObjectRule, RecordRule, RuleType, StringRule, ValidationRule, ValidationSchema, VersionRule } from "./interfaces/validationSchema.js";
|
|
4
4
|
import "./interfaces/index.js";
|
|
5
5
|
import { AppValidator } from "./validator.js";
|
|
6
|
-
import {
|
|
7
|
-
export { AppValidator, ArrayRule, BooleanRule, CheckerFunctionError, CustomDateRule, DateRule, EmailRule, EnumRule, ListValidationSchema, NumberRule, ObjectIdRule, ObjectRule, ParameterValidation, RecordRule, Rule, RuleType, RuleValidator, StringRule, ValidationRule, ValidationSchema, VersionRule,
|
|
6
|
+
import { availableMobileCodes, emailRuValidation, emailValidation, getListValidationSchema, getStringDateSchema, phoneNumberValidation, phoneNumberWithoutPlusValidation } from "./schemas/index.js";
|
|
7
|
+
export { AppValidator, ArrayRule, BooleanRule, CheckerFunctionError, CustomDateRule, DateRule, EmailRule, EnumRule, ListValidationSchema, NumberRule, ObjectIdRule, ObjectRule, ParameterValidation, RecordRule, Rule, RuleType, RuleValidator, StringRule, ValidationRule, ValidationSchema, VersionRule, availableMobileCodes, emailRuValidation, emailValidation, getListValidationSchema, getStringDateSchema, phoneNumberValidation, phoneNumberWithoutPlusValidation };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { AppValidator } from "./validator.js";
|
|
2
|
-
import {
|
|
3
|
-
export { AppValidator,
|
|
2
|
+
import { availableMobileCodes, emailRuValidation, emailValidation, getListValidationSchema, getStringDateSchema, phoneNumberValidation, phoneNumberWithoutPlusValidation } from "./schemas/index.js";
|
|
3
|
+
export { AppValidator, availableMobileCodes, emailRuValidation, emailValidation, getListValidationSchema, getStringDateSchema, phoneNumberValidation, phoneNumberWithoutPlusValidation };
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import FastestValidator, { MessagesType, ValidationSchema } from "fastest-validator";
|
|
2
|
+
/**
|
|
3
|
+
* fastest-validator custom rule factory: receives the rule's schema and messages and returns the JS source
|
|
4
|
+
* of the checker function `(value, field, parent, errors, context)`, which must `return value`.
|
|
5
|
+
* Set `sanitized: true` when the source reassigns `value`, so the converted value is written back to the parent object.
|
|
6
|
+
*/
|
|
2
7
|
export type RuleValidator = (param: {
|
|
3
8
|
messages: MessagesType;
|
|
4
9
|
schema: ValidationSchema;
|
|
@@ -6,8 +11,16 @@ export type RuleValidator = (param: {
|
|
|
6
11
|
source: string;
|
|
7
12
|
sanitized?: boolean;
|
|
8
13
|
};
|
|
14
|
+
/** Custom rule type registered by `AppValidator`. */
|
|
9
15
|
export interface Rule {
|
|
16
|
+
/** Rule type used as `type` in schemas and as the key of its error message. */
|
|
10
17
|
getName(): string;
|
|
18
|
+
/** Error message template; `{field}` is replaced with the field path. */
|
|
11
19
|
getMessage(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Builds the checker for this rule type.
|
|
22
|
+
*
|
|
23
|
+
* @param validator - fastest-validator instance, used to generate error-reporting code via `makeError`.
|
|
24
|
+
*/
|
|
12
25
|
getRule(validator: FastestValidator): RuleValidator;
|
|
13
26
|
}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regex-based input validation in a serializable form: used in Design System input `validation` lists sent to the app,
|
|
3
|
+
* or as a `pattern` in server-side schemas.
|
|
4
|
+
*/
|
|
1
5
|
export interface ParameterValidation {
|
|
6
|
+
/** Pattern source for the `RegExp` constructor. */
|
|
2
7
|
regexp: string;
|
|
8
|
+
/** `RegExp` flags, e.g. `['i', 'g']`. */
|
|
3
9
|
flags: string[];
|
|
10
|
+
/** User-facing error message in Ukrainian. */
|
|
4
11
|
errorMessage: string;
|
|
5
12
|
}
|
package/dist/rules/buffer.js
CHANGED
package/dist/rules/date.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
// src/rules/date.ts
|
|
2
|
+
/**
|
|
3
|
+
* `customDate` rule: a string containing a `DD.MM.YYYY` date.
|
|
4
|
+
*
|
|
5
|
+
* The default pattern is not anchored and does not check day/month ranges.
|
|
6
|
+
* With `convert: true` the value is replaced with a `Date` at UTC midnight; out-of-range parts roll over,
|
|
7
|
+
* e.g. `32.13.2024` becomes 2025-02-01.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* { birthDate: { type: 'customDate', convert: true } }
|
|
11
|
+
*/
|
|
2
12
|
var DateValidationRule = class {
|
|
3
13
|
checkPattern;
|
|
14
|
+
/**
|
|
15
|
+
* @param checkPattern - Pattern the string must match.
|
|
16
|
+
*/
|
|
4
17
|
constructor(checkPattern = /(?:\d{2}\.){2}\d{4}/) {
|
|
5
18
|
this.checkPattern = checkPattern;
|
|
6
19
|
}
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
// src/rules/internationalPhoneNumber.ts
|
|
2
|
+
/**
|
|
3
|
+
* `internationalPhoneNumber` rule: a string with a phone number in a loose international format:
|
|
4
|
+
* optional `+` and country code, optional area code in parentheses, digit groups separated by spaces, dots or dashes.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* { phone: { type: 'internationalPhoneNumber' } } // '+48 123 456 789', '+1 (555) 123-4567'
|
|
8
|
+
*/
|
|
2
9
|
var InternationalPhoneNumberValidationRule = class {
|
|
3
10
|
checkPattern = /^\+?\d{1,4}?[\s.-]?\(?\d{1,3}?\)?(?:[\s.-]?\d{1,4}){2}[\s.-]?\d{1,9}$/g;
|
|
4
11
|
getName() {
|
package/dist/rules/objectId.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
// src/rules/objectId.ts
|
|
2
2
|
const PATTERN = /^[\da-f]{24}$/i;
|
|
3
|
+
/**
|
|
4
|
+
* `objectId` rule: a 24-character hex string, case-insensitive.
|
|
5
|
+
*
|
|
6
|
+
* `ObjectId` instances are rejected, and `convert` has no effect: the value stays a string.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* { userId: { type: 'objectId' } }
|
|
10
|
+
*/
|
|
3
11
|
var ObjectIdValidationRule = class {
|
|
4
12
|
getName() {
|
|
5
13
|
return "objectId";
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { availableMobileCodes } from "../schemas/index.js";
|
|
2
2
|
// src/rules/phoneNumber.ts
|
|
3
|
+
/**
|
|
4
|
+
* `phoneNumber` rule: a Ukrainian mobile number without `+`: `380`, an operator code from {@link availableMobileCodes}, then 7 digits.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* { phone: { type: 'phoneNumber' } } // '380671234567'
|
|
8
|
+
*/
|
|
3
9
|
var PhoneNumberValidationRule = class {
|
|
4
10
|
checkPattern;
|
|
5
11
|
constructor() {
|
package/dist/rules/version.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
// src/rules/version.ts
|
|
2
2
|
const PATTERN = /^v\d+$/;
|
|
3
3
|
/**
|
|
4
|
+
* `version` rule: a string like `v1`. When the schema has a non-empty `versions` list, the value must also be one of them.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* { version: { type: 'version', versions: ['v1', 'v2'] } }
|
|
8
|
+
*
|
|
4
9
|
* @deprecated
|
|
5
10
|
*/
|
|
6
11
|
var VersionValidationRule = class {
|
package/dist/schemas/index.d.ts
CHANGED
|
@@ -1,16 +1,33 @@
|
|
|
1
1
|
import { ParameterValidation } from "../interfaces/schema.js";
|
|
2
|
-
import { ListValidationSchema,
|
|
3
|
-
import { AppVersions } from "@diia-inhouse/types";
|
|
2
|
+
import { ListValidationSchema, StringRule } from "../interfaces/validationSchema.js";
|
|
4
3
|
import "../interfaces/index.js";
|
|
4
|
+
/** Ukrainian mobile operator codes without the leading `0`, e.g. `67` for `067`. */
|
|
5
5
|
export declare const availableMobileCodes: string[];
|
|
6
|
+
/** Ukrainian mobile number in the `+380XXXXXXXXX` format. */
|
|
6
7
|
export declare const phoneNumberValidation: ParameterValidation;
|
|
8
|
+
/** Ukrainian mobile number in the `380XXXXXXXXX` format, without `+`. */
|
|
7
9
|
export declare const phoneNumberWithoutPlusValidation: ParameterValidation;
|
|
10
|
+
/** Email address with a top-level domain of 2 to 64 letters. */
|
|
8
11
|
export declare const emailValidation: ParameterValidation;
|
|
12
|
+
/**
|
|
13
|
+
* Email address like {@link emailValidation} whose top-level domain does not start with `ru` or `su`.
|
|
14
|
+
* This also rejects unrelated domains such as `.run` or `.surf`.
|
|
15
|
+
*/
|
|
9
16
|
export declare const emailRuValidation: ParameterValidation;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Builds a pagination schema with optional `skip` and `limit`, both converted to numbers.
|
|
19
|
+
*
|
|
20
|
+
* @param min - Lower bound for both `skip` and `limit`.
|
|
21
|
+
* @param max - Upper bound for `limit`.
|
|
22
|
+
*/
|
|
14
23
|
export declare function getListValidationSchema(min?: number, max?: number): ListValidationSchema;
|
|
15
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Builds a rule for a non-empty string that luxon parses with `format`. The value stays a string.
|
|
26
|
+
*
|
|
27
|
+
* @param format - luxon format tokens; the default is an ISO 8601 UTC timestamp with milliseconds.
|
|
28
|
+
* @param optional - Allow `undefined` or `null`; an empty string is still rejected.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* { birthDate: getStringDateSchema('dd.MM.yyyy') }
|
|
32
|
+
*/
|
|
16
33
|
export declare function getStringDateSchema(format?: string, optional?: boolean): StringRule;
|
package/dist/schemas/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import fromPairs from "lodash.frompairs";
|
|
2
1
|
import { DateTime } from "luxon";
|
|
3
|
-
import { PlatformType } from "@diia-inhouse/types";
|
|
4
2
|
// src/schemas/index.ts
|
|
3
|
+
/** Ukrainian mobile operator codes without the leading `0`, e.g. `67` for `067`. */
|
|
5
4
|
const availableMobileCodes = [
|
|
6
5
|
"39",
|
|
7
6
|
"50",
|
|
@@ -23,52 +22,39 @@ const availableMobileCodes = [
|
|
|
23
22
|
"75",
|
|
24
23
|
"77,"
|
|
25
24
|
];
|
|
25
|
+
/** Ukrainian mobile number in the `+380XXXXXXXXX` format. */
|
|
26
26
|
const phoneNumberValidation = {
|
|
27
27
|
regexp: String.raw`^\+380(${availableMobileCodes.join("|")})\d{7}$`,
|
|
28
28
|
flags: ["i", "g"],
|
|
29
29
|
errorMessage: "Упс, ви ввели неправильний номер телефону. Використовуйте формат + 38 (0ХХ) ХХХ ХХ ХХ."
|
|
30
30
|
};
|
|
31
|
+
/** Ukrainian mobile number in the `380XXXXXXXXX` format, without `+`. */
|
|
31
32
|
const phoneNumberWithoutPlusValidation = {
|
|
32
33
|
regexp: String.raw`^380(${availableMobileCodes.join("|")})\d{7}$`,
|
|
33
34
|
flags: ["i", "g"],
|
|
34
35
|
errorMessage: "Упс, ви ввели неправильний номер телефону. Використовуйте формат 38 (0ХХ) ХХХ ХХ ХХ."
|
|
35
36
|
};
|
|
37
|
+
/** Email address with a top-level domain of 2 to 64 letters. */
|
|
36
38
|
const emailValidation = {
|
|
37
39
|
regexp: String.raw`^([a-zA-Z0-9_%+-]{1,}\.){0,}[a-zA-Z0-9_%+-]{1,}@([a-zA-Z0-9_%+-]{1,}\.){1,}[A-Za-z]{2,64}$`,
|
|
38
40
|
flags: ["i"],
|
|
39
41
|
errorMessage: "Упс, ви ввели неправильний email. Виправте дані або спробуйте іншу пошту."
|
|
40
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* Email address like {@link emailValidation} whose top-level domain does not start with `ru` or `su`.
|
|
45
|
+
* This also rejects unrelated domains such as `.run` or `.surf`.
|
|
46
|
+
*/
|
|
41
47
|
const emailRuValidation = {
|
|
42
48
|
regexp: String.raw`^([a-zA-Z0-9_%+-]{1,}\.){0,}[a-zA-Z0-9_%+-]{1,}@([a-zA-Z0-9_%+-]{1,}\.){1,}(?!ru|su)[A-Za-z]{2,64}$`,
|
|
43
49
|
flags: ["i"],
|
|
44
50
|
errorMessage: "Йой, це ж електронна адреса з російським доменом. Ми не можемо її прийняти. Спробуйте іншу скриньку, будь ласка."
|
|
45
51
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
regexp: String.raw`^\d{5}$`,
|
|
53
|
-
flags: [],
|
|
54
|
-
errorMessage: "Введіть індекс з 5 цифр"
|
|
55
|
-
};
|
|
56
|
-
/** @deprecated use getListValidationSchema function instead */
|
|
57
|
-
const listValidationSchema = {
|
|
58
|
-
skip: {
|
|
59
|
-
type: "number",
|
|
60
|
-
convert: true,
|
|
61
|
-
min: 0,
|
|
62
|
-
optional: true
|
|
63
|
-
},
|
|
64
|
-
limit: {
|
|
65
|
-
type: "number",
|
|
66
|
-
convert: true,
|
|
67
|
-
min: 0,
|
|
68
|
-
max: 100,
|
|
69
|
-
optional: true
|
|
70
|
-
}
|
|
71
|
-
};
|
|
52
|
+
/**
|
|
53
|
+
* Builds a pagination schema with optional `skip` and `limit`, both converted to numbers.
|
|
54
|
+
*
|
|
55
|
+
* @param min - Lower bound for both `skip` and `limit`.
|
|
56
|
+
* @param max - Upper bound for `limit`.
|
|
57
|
+
*/
|
|
72
58
|
function getListValidationSchema(min = 0, max = 100) {
|
|
73
59
|
return {
|
|
74
60
|
skip: {
|
|
@@ -86,37 +72,15 @@ function getListValidationSchema(min = 0, max = 100) {
|
|
|
86
72
|
}
|
|
87
73
|
};
|
|
88
74
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
optional: true
|
|
99
|
-
}]))
|
|
100
|
-
},
|
|
101
|
-
minVersion: {
|
|
102
|
-
type: "object",
|
|
103
|
-
optional: true,
|
|
104
|
-
props: fromPairs(Object.values(PlatformType).map((platformType) => [platformType, {
|
|
105
|
-
type: "string",
|
|
106
|
-
optional: true
|
|
107
|
-
}]))
|
|
108
|
-
},
|
|
109
|
-
maxVersion: {
|
|
110
|
-
type: "object",
|
|
111
|
-
optional: true,
|
|
112
|
-
props: fromPairs(Object.values(PlatformType).map((platformType) => [platformType, {
|
|
113
|
-
type: "string",
|
|
114
|
-
optional: true
|
|
115
|
-
}]))
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
optional: true
|
|
119
|
-
};
|
|
75
|
+
/**
|
|
76
|
+
* Builds a rule for a non-empty string that luxon parses with `format`. The value stays a string.
|
|
77
|
+
*
|
|
78
|
+
* @param format - luxon format tokens; the default is an ISO 8601 UTC timestamp with milliseconds.
|
|
79
|
+
* @param optional - Allow `undefined` or `null`; an empty string is still rejected.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* { birthDate: getStringDateSchema('dd.MM.yyyy') }
|
|
83
|
+
*/
|
|
120
84
|
function getStringDateSchema(format = `yyyy-MM-dd'T'HH:mm:ss.SSS'Z'`, optional = false) {
|
|
121
85
|
return {
|
|
122
86
|
type: "string",
|
|
@@ -142,4 +106,4 @@ function getStringDateSchema(format = `yyyy-MM-dd'T'HH:mm:ss.SSS'Z'`, optional =
|
|
|
142
106
|
optional
|
|
143
107
|
};
|
|
144
108
|
}
|
|
145
|
-
export {
|
|
109
|
+
export { availableMobileCodes, emailRuValidation, emailValidation, getListValidationSchema, getStringDateSchema, phoneNumberValidation, phoneNumberWithoutPlusValidation };
|
package/dist/validator.d.ts
CHANGED
|
@@ -1,10 +1,40 @@
|
|
|
1
1
|
import { ValidationSchema } from "fastest-validator";
|
|
2
2
|
import { ErrorType } from "@diia-inhouse/errors";
|
|
3
|
+
/**
|
|
4
|
+
* Schema validator built on fastest-validator that throws {@link ValidationError} instead of returning an error list.
|
|
5
|
+
*
|
|
6
|
+
* Registers the custom rule types `buffer`, `customDate`, `objectId`, `phoneNumber`,
|
|
7
|
+
* `internationalPhoneNumber` and `version`, so they can be used as `type` in any schema.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const validator = new AppValidator()
|
|
11
|
+
*
|
|
12
|
+
* validator.validate({ phone: '380671234567' }, { phone: { type: 'phoneNumber' } })
|
|
13
|
+
*/
|
|
3
14
|
export declare class AppValidator {
|
|
4
15
|
private readonly validator;
|
|
5
16
|
private customRules;
|
|
6
17
|
constructor();
|
|
18
|
+
/**
|
|
19
|
+
* Binds `schema` to a reusable check function with the same contract as {@link AppValidator.validate}.
|
|
20
|
+
*
|
|
21
|
+
* The schema is not precompiled: fastest-validator compiles it again on every call of the returned function.
|
|
22
|
+
*
|
|
23
|
+
* @param schema - fastest-validator schema to bind.
|
|
24
|
+
* @returns Function that returns `true` for valid params and throws {@link ValidationError} otherwise.
|
|
25
|
+
*/
|
|
7
26
|
compile(schema: ValidationSchema): (params: unknown) => boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Validates `params` against `schema`.
|
|
29
|
+
*
|
|
30
|
+
* Rules with `convert: true` (and other sanitizing rules) mutate `params` in place.
|
|
31
|
+
*
|
|
32
|
+
* @param params - Value to validate.
|
|
33
|
+
* @param schema - fastest-validator schema; may use the custom rule types registered by this class.
|
|
34
|
+
* @param errorType - {@link ErrorType} set on the thrown error; the errors package default is used when omitted.
|
|
35
|
+
* @returns Always `true`; invalid params never return.
|
|
36
|
+
* @throws {ValidationError} With HTTP 422, process code {@link ErrorCode.ValidationError} and the list of failed fields in `data.errors`.
|
|
37
|
+
*/
|
|
8
38
|
validate(params: unknown, schema: ValidationSchema, errorType?: ErrorType): boolean;
|
|
9
39
|
private getCustomValidatorsMessages;
|
|
10
40
|
private addCustomValidationRules;
|
package/dist/validator.js
CHANGED
|
@@ -8,6 +8,17 @@ import { ErrorCode, ValidationError } from "@diia-inhouse/errors";
|
|
|
8
8
|
import "./rules/index.js";
|
|
9
9
|
import { InternationalPhoneNumberValidationRule } from "./rules/internationalPhoneNumber.js";
|
|
10
10
|
// src/validator.ts
|
|
11
|
+
/**
|
|
12
|
+
* Schema validator built on fastest-validator that throws {@link ValidationError} instead of returning an error list.
|
|
13
|
+
*
|
|
14
|
+
* Registers the custom rule types `buffer`, `customDate`, `objectId`, `phoneNumber`,
|
|
15
|
+
* `internationalPhoneNumber` and `version`, so they can be used as `type` in any schema.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const validator = new AppValidator()
|
|
19
|
+
*
|
|
20
|
+
* validator.validate({ phone: '380671234567' }, { phone: { type: 'phoneNumber' } })
|
|
21
|
+
*/
|
|
11
22
|
var AppValidator = class {
|
|
12
23
|
validator;
|
|
13
24
|
customRules;
|
|
@@ -26,9 +37,28 @@ var AppValidator = class {
|
|
|
26
37
|
});
|
|
27
38
|
this.addCustomValidationRules();
|
|
28
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Binds `schema` to a reusable check function with the same contract as {@link AppValidator.validate}.
|
|
42
|
+
*
|
|
43
|
+
* The schema is not precompiled: fastest-validator compiles it again on every call of the returned function.
|
|
44
|
+
*
|
|
45
|
+
* @param schema - fastest-validator schema to bind.
|
|
46
|
+
* @returns Function that returns `true` for valid params and throws {@link ValidationError} otherwise.
|
|
47
|
+
*/
|
|
29
48
|
compile(schema) {
|
|
30
49
|
return (params) => this.validate(params, schema);
|
|
31
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Validates `params` against `schema`.
|
|
53
|
+
*
|
|
54
|
+
* Rules with `convert: true` (and other sanitizing rules) mutate `params` in place.
|
|
55
|
+
*
|
|
56
|
+
* @param params - Value to validate.
|
|
57
|
+
* @param schema - fastest-validator schema; may use the custom rule types registered by this class.
|
|
58
|
+
* @param errorType - {@link ErrorType} set on the thrown error; the errors package default is used when omitted.
|
|
59
|
+
* @returns Always `true`; invalid params never return.
|
|
60
|
+
* @throws {ValidationError} With HTTP 422, process code {@link ErrorCode.ValidationError} and the list of failed fields in `data.errors`.
|
|
61
|
+
*/
|
|
32
62
|
validate(params, schema, errorType) {
|
|
33
63
|
const res = this.validator.validate(params, schema);
|
|
34
64
|
if (res !== true) throw new ValidationError(res, ErrorCode.ValidationError, errorType);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@diia-inhouse/validators",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Data validators package",
|
|
5
5
|
"license": "SEE LICENCE IN LICENCE.md",
|
|
6
6
|
"author": "diia-team",
|
|
@@ -32,15 +32,12 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"bson": "7.3.4",
|
|
34
34
|
"fastest-validator": "1.19.1",
|
|
35
|
-
"lodash.frompairs": "4.0.1",
|
|
36
35
|
"luxon": "3.7.2"
|
|
37
36
|
},
|
|
38
37
|
"devDependencies": {
|
|
39
|
-
"@diia-inhouse/configs": "7.
|
|
40
|
-
"@diia-inhouse/errors": "2.3.
|
|
38
|
+
"@diia-inhouse/configs": "7.6.0",
|
|
39
|
+
"@diia-inhouse/errors": "2.3.52",
|
|
41
40
|
"@diia-inhouse/oxc-config": "6.2.2",
|
|
42
|
-
"@diia-inhouse/types": "14.13.3",
|
|
43
|
-
"@types/lodash.frompairs": "4.0.9",
|
|
44
41
|
"@types/luxon": "3.7.6",
|
|
45
42
|
"@types/node": "26.6.6",
|
|
46
43
|
"@vitest/coverage-v8": "5.1.1",
|
|
@@ -54,8 +51,7 @@
|
|
|
54
51
|
"vitest": "5.1.1"
|
|
55
52
|
},
|
|
56
53
|
"peerDependencies": {
|
|
57
|
-
"@diia-inhouse/errors": ">=2.0.0"
|
|
58
|
-
"@diia-inhouse/types": ">=13.1.0"
|
|
54
|
+
"@diia-inhouse/errors": ">=2.0.0"
|
|
59
55
|
},
|
|
60
56
|
"commitlint": {
|
|
61
57
|
"extends": "@diia-inhouse/configs/dist/commitlint"
|