UNPKG

validatees

Version:

✅ Validation library for ES6+ modules

42 lines (41 loc) 1.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isUnique = void 0; const isFalsy_1 = __importDefault(require("../types/isFalsy")); const isDeepMatch_1 = __importDefault(require("./isDeepMatch")); const isSoftMatch_1 = __importDefault(require("./isSoftMatch")); function isUnique(group, value) { if ((0, isFalsy_1.default)(group)) { throw new Error("isUnique expects an array or object to match against"); } else if (Array.isArray(group) && typeof value !== typeof group[0]) { throw new Error("isUnique expects the value to be the same type as the group"); } else if ("object" === typeof group && false === Array.isArray(group) && typeof value !== typeof { [Object.keys(group)[0]]: [Object.values(group)[0]] }) { throw new Error("isUnique expects the value to be the same type as the group"); } else if (Array.isArray(group)) { return group.every((item) => false === (0, isDeepMatch_1.default)(item, value)); } else if ("object" === typeof group) { if ("object" === typeof value) { const hasMatchingKey = Object.keys(group).some((key) => (0, isSoftMatch_1.default)(key, Object.keys(value)[0])); if (hasMatchingKey) { return false; } } return Object.keys(group).every((key) => false === (0, isDeepMatch_1.default)(key, value) && false === (0, isDeepMatch_1.default)(group[key], value) && false === (0, isDeepMatch_1.default)({ [key]: group[key] }, value)); } else { throw new Error("isUnique expects an array or object to match against"); } } exports.isUnique = isUnique; exports.default = isUnique;