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.

42 lines
1.3 KiB
JavaScript

"use strict";
const assureArray = require("assure-array");
const isPlainObj = require("is-plain-obj");
const defaultValue = require("default-value");
const flatten = require("flatten");
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 Node 10.x goes EOL (April 2021)
let flattened = flatten(assureArray(rules));
let actualRules = flattened.filter((rule) => rule != null);
if (normalizeObjects) {
// TODO: Switch to `Array#flatmap` once Node 10.x goes EOL (April 2021)
// TODO: Better input validation of some sort here
let mapped = actualRules.map((rule) => {
// TODO: Figure out why isPlainObj in the below line breaks within a component in Shayu. Something something cross-realm objects something?
// if (isPlainObj(rule) && !matchSpecial(rule)) {
if (typeof rule === "object" && !matchSpecial(rule)) {
return [
isPlainObjectValidator,
hasShape(rule)
];
} else {
return rule;
}
});
return flatten(mapped);
} else {
return actualRules;
}
};