deepProperties.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const _util_1 = require("./_util");
  4. function getDef(opts) {
  5. return {
  6. keyword: "deepProperties",
  7. type: "object",
  8. schemaType: "object",
  9. macro: function (schema) {
  10. const allOf = [];
  11. for (const pointer in schema)
  12. allOf.push(getSchema(pointer, schema[pointer]));
  13. return { allOf };
  14. },
  15. metaSchema: {
  16. type: "object",
  17. propertyNames: { type: "string", format: "json-pointer" },
  18. additionalProperties: (0, _util_1.metaSchemaRef)(opts),
  19. },
  20. };
  21. }
  22. exports.default = getDef;
  23. function getSchema(jsonPointer, schema) {
  24. const segments = jsonPointer.split("/");
  25. const rootSchema = {};
  26. let pointerSchema = rootSchema;
  27. for (let i = 1; i < segments.length; i++) {
  28. let segment = segments[i];
  29. const isLast = i === segments.length - 1;
  30. segment = unescapeJsonPointer(segment);
  31. const properties = (pointerSchema.properties = {});
  32. let items;
  33. if (/[0-9]+/.test(segment)) {
  34. let count = +segment;
  35. items = pointerSchema.items = [];
  36. pointerSchema.type = ["object", "array"];
  37. while (count--)
  38. items.push({});
  39. }
  40. else {
  41. pointerSchema.type = "object";
  42. }
  43. pointerSchema = isLast ? schema : {};
  44. properties[segment] = pointerSchema;
  45. if (items)
  46. items.push(pointerSchema);
  47. }
  48. return rootSchema;
  49. }
  50. function unescapeJsonPointer(str) {
  51. return str.replace(/~1/g, "/").replace(/~0/g, "~");
  52. }
  53. module.exports = getDef;
  54. //# sourceMappingURL=deepProperties.js.map