You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1014 B
JavaScript

"use strict";
const assureArray = require("assure-array");
const isPlainObj = require("is-plain-obj");
const defaultValue = require("default-value");
const flat = require("array.prototype.flat");
const flatMap = require("array.prototype.flatmap");
const matchSpecial = require("@validatem/match-special");
const isPlainObjectValidator = require("@validatem/is-plain-object");
const hasShape = require("@validatem/has-shape");
// FIXME: Write an example.js
module.exports = function normalizeRules(rules, options) {
let normalizeObjects = defaultValue(options.normalizeObjects, false);
// TODO: Switch to `Array#flat` once it is available
let flattened = flat(assureArray(rules));
let actualRules = flattened.filter((rule) => rule != null);
if (normalizeObjects) {
return flatMap(actualRules, (rule) => {
if (isPlainObj(rule) && !matchSpecial(rule)) {
return [
isPlainObjectValidator,
hasShape(rule)
];
} else {
return rule;
}
});
} else {
return actualRules;
}
};