uniqueItemProperties.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const equal = require("fast-deep-equal");
  4. const SCALAR_TYPES = ["number", "integer", "string", "boolean", "null"];
  5. function getDef() {
  6. return {
  7. keyword: "uniqueItemProperties",
  8. type: "array",
  9. schemaType: "array",
  10. compile(keys, parentSchema) {
  11. const scalar = getScalarKeys(keys, parentSchema);
  12. return (data) => {
  13. if (data.length <= 1)
  14. return true;
  15. for (let k = 0; k < keys.length; k++) {
  16. const key = keys[k];
  17. if (scalar[k]) {
  18. const hash = {};
  19. for (const x of data) {
  20. if (!x || typeof x != "object")
  21. continue;
  22. let p = x[key];
  23. if (p && typeof p == "object")
  24. continue;
  25. if (typeof p == "string")
  26. p = '"' + p;
  27. if (hash[p])
  28. return false;
  29. hash[p] = true;
  30. }
  31. }
  32. else {
  33. for (let i = data.length; i--;) {
  34. const x = data[i];
  35. if (!x || typeof x != "object")
  36. continue;
  37. for (let j = i; j--;) {
  38. const y = data[j];
  39. if (y && typeof y == "object" && equal(x[key], y[key]))
  40. return false;
  41. }
  42. }
  43. }
  44. }
  45. return true;
  46. };
  47. },
  48. metaSchema: {
  49. type: "array",
  50. items: { type: "string" },
  51. },
  52. };
  53. }
  54. exports.default = getDef;
  55. function getScalarKeys(keys, schema) {
  56. return keys.map((key) => {
  57. var _a, _b, _c;
  58. const t = (_c = (_b = (_a = schema.items) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b[key]) === null || _c === void 0 ? void 0 : _c.type;
  59. return Array.isArray(t)
  60. ? !t.includes("object") && !t.includes("array")
  61. : SCALAR_TYPES.includes(t);
  62. });
  63. }
  64. module.exports = getDef;
  65. //# sourceMappingURL=uniqueItemProperties.js.map