|
|
@ -3,8 +3,7 @@
|
|
|
|
const assureArray = require("assure-array");
|
|
|
|
const assureArray = require("assure-array");
|
|
|
|
const isPlainObj = require("is-plain-obj");
|
|
|
|
const isPlainObj = require("is-plain-obj");
|
|
|
|
const defaultValue = require("default-value");
|
|
|
|
const defaultValue = require("default-value");
|
|
|
|
const flat = require("array.prototype.flat");
|
|
|
|
const flatten = require("flatten");
|
|
|
|
const flatMap = require("array.prototype.flatmap");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const matchSpecial = require("@validatem/match-special");
|
|
|
|
const matchSpecial = require("@validatem/match-special");
|
|
|
|
const isPlainObjectValidator = require("@validatem/is-plain-object");
|
|
|
|
const isPlainObjectValidator = require("@validatem/is-plain-object");
|
|
|
@ -15,12 +14,13 @@ const hasShape = require("@validatem/has-shape");
|
|
|
|
module.exports = function normalizeRules(rules, options) {
|
|
|
|
module.exports = function normalizeRules(rules, options) {
|
|
|
|
let normalizeObjects = defaultValue(options.normalizeObjects, false);
|
|
|
|
let normalizeObjects = defaultValue(options.normalizeObjects, false);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Switch to `Array#flat` once it is available
|
|
|
|
// TODO: Switch to `Array#flat` once Node 10.x goes EOL (April 2021)
|
|
|
|
let flattened = flat(assureArray(rules));
|
|
|
|
let flattened = flatten(assureArray(rules));
|
|
|
|
let actualRules = flattened.filter((rule) => rule != null);
|
|
|
|
let actualRules = flattened.filter((rule) => rule != null);
|
|
|
|
|
|
|
|
|
|
|
|
if (normalizeObjects) {
|
|
|
|
if (normalizeObjects) {
|
|
|
|
return flatMap(actualRules, (rule) => {
|
|
|
|
// TODO: Switch to `Array#flatmap` once Node 10.x goes EOL (April 2021)
|
|
|
|
|
|
|
|
let mapped = actualRules.map((rule) => {
|
|
|
|
if (isPlainObj(rule) && !matchSpecial(rule)) {
|
|
|
|
if (isPlainObj(rule) && !matchSpecial(rule)) {
|
|
|
|
return [
|
|
|
|
return [
|
|
|
|
isPlainObjectValidator,
|
|
|
|
isPlainObjectValidator,
|
|
|
@ -30,6 +30,8 @@ module.exports = function normalizeRules(rules, options) {
|
|
|
|
return rule;
|
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return flatten(mapped);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return actualRules;
|
|
|
|
return actualRules;
|
|
|
|
}
|
|
|
|
}
|
|
|
|