@common-grants/cli
Version:
The CommonGrants protocol CLI tool
62 lines (61 loc) • 2.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepFlattenAllOf = deepFlattenAllOf;
const json_schema_merge_allof_1 = __importDefault(require("json-schema-merge-allof"));
/**
* Deeply flatten `allOf` in a schema by:
* 1) Merging top-level allOf into a single schema
* 2) Recursively descending into properties, items, additionalProperties, etc.
* 3) Repeating if new allOfs appear after merging
*/
function deepFlattenAllOf(schema) {
// 1) Merge top-level allOf (if any)
let mergedSchema = mergeOneLevelAllOf(schema);
// 2) Recursively flatten sub-schemas
mergedSchema = recursivelyFlattenSubSchemas(mergedSchema);
// 3) It's possible the recursion introduced new top-level allOfs
// (in rare cases if merging merges references in a certain way).
// So we can repeat until stable if desired:
// But usually one pass is sufficient for top-level.
mergedSchema = mergeOneLevelAllOf(mergedSchema);
return mergedSchema;
}
/**
* Merge top-level allOf (only) if present on a schema using `json-schema-merge-allof`.
* Returns a new schema with top-level `allOf` removed.
*/
function mergeOneLevelAllOf(schema) {
if (schema.allOf && schema.allOf.length > 0) {
// The library merges top-level `allOf` into a new schema
const merged = (0, json_schema_merge_allof_1.default)(schema, { ignoreAdditionalProperties: false });
return merged;
}
return schema;
}
/**
* Recursively flatten sub-schemas in properties, items, additionalProperties, etc.
*/
function recursivelyFlattenSubSchemas(schema) {
// We only handle object fields if the schema is actually an object in practice
// (or we handle them unconditionally, depending on your usage).
// If `schema.type` is "object" or is missing, we still might have `properties`.
if (schema.properties && typeof schema.properties === "object") {
for (const [propName, propSchema] of Object.entries(schema.properties)) {
if (propSchema && typeof propSchema === "object") {
schema.properties[propName] = deepFlattenAllOf(propSchema);
}
}
}
// If the schema is an array type and has an `items` property that is an object
if (schema.type === "array" && schema.items && typeof schema.items === "object") {
schema.items = deepFlattenAllOf(schema.items);
}
// If the schema has `additionalProperties` as an object
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
schema.additionalProperties = deepFlattenAllOf(schema.additionalProperties);
}
return schema;
}