1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.checkSchema = void 0;
|
4 | const chain_1 = require("../chain");
|
5 | const check_1 = require("./check");
|
6 | const validLocations = ['body', 'cookies', 'headers', 'params', 'query'];
|
7 | const protectedNames = ['errorMessage', 'in'];
|
8 | function checkSchema(schema, defaultLocations = validLocations) {
|
9 | const chains = Object.keys(schema).map(field => {
|
10 | const config = schema[field];
|
11 | const chain = check_1.check(field, ensureLocations(config, defaultLocations), config.errorMessage);
|
12 | Object.keys(config)
|
13 | .filter((method) => {
|
14 | return config[method] && !protectedNames.includes(method);
|
15 | })
|
16 | .forEach(method => {
|
17 | var _a;
|
18 | if (typeof chain[method] !== 'function') {
|
19 | console.warn(`express-validator: a validator/sanitizer with name ${method} does not exist`);
|
20 | return;
|
21 | }
|
22 |
|
23 | const methodCfg = config[method];
|
24 | let options = methodCfg === true ? [] : (_a = methodCfg.options) !== null && _a !== void 0 ? _a : [];
|
25 | if (options != null && !Array.isArray(options)) {
|
26 | options = [options];
|
27 | }
|
28 | if (isValidatorOptions(method, methodCfg) && methodCfg.if) {
|
29 | chain.if(methodCfg.if);
|
30 | }
|
31 | if (isValidatorOptions(method, methodCfg) && methodCfg.negated) {
|
32 | chain.not();
|
33 | }
|
34 | chain[method](...options);
|
35 | if (isValidatorOptions(method, methodCfg) && methodCfg.errorMessage) {
|
36 | chain.withMessage(methodCfg.errorMessage);
|
37 | }
|
38 | if (isValidatorOptions(method, methodCfg) && methodCfg.bail) {
|
39 | chain.bail();
|
40 | }
|
41 | });
|
42 | return chain;
|
43 | });
|
44 | const run = async (req) => {
|
45 | return await Promise.all(chains.map(chain => chain.run(req)));
|
46 | };
|
47 | return Object.assign(chains, { run });
|
48 | }
|
49 | exports.checkSchema = checkSchema;
|
50 | function isValidatorOptions(method, methodCfg) {
|
51 | return methodCfg !== true && method in chain_1.ValidatorsImpl.prototype;
|
52 | }
|
53 | function ensureLocations(config, defaults) {
|
54 |
|
55 |
|
56 | const locations = Array.isArray(config.in)
|
57 | ? config.in
|
58 | : [config.in].filter(Boolean);
|
59 | const actualLocations = locations.length ? locations : defaults;
|
60 | return actualLocations.filter(location => validLocations.includes(location));
|
61 | }
|