@mintlify/validation
Version:
Validates mint.json files
58 lines (57 loc) • 2.35 kB
JavaScript
import hash from 'object-hash';
import { v4 as uuidv4 } from 'uuid';
import { mapExample } from './mapExample.js';
import { mapMedia } from './mapMedia.js';
import { mapSchema } from './mapSchema.js';
export const mapHeader = ({ header, refUuidMap, uuidObjectHashMap, hashedNodeMap, uuid, }) => {
// hash the raw object
const objectHash = hash(header);
// map uuid and hash to uuidObjectHashMap
uuidObjectHashMap[uuid] = objectHash;
// if ref, replace with uuid
if ('$ref' in header && header.$ref) {
const refId = header.$ref;
const refUuid = refUuidMap[refId] || uuidv4();
header.$ref = refUuid;
}
// if not ref
// if schema in header, create SchemaOrRef node
if ('schema' in header && header.schema && typeof header.schema === 'object') {
const uuid = uuidv4();
mapSchema({
schema: header.schema,
refUuidMap,
uuidObjectHashMap,
hashedNodeMap,
uuid,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
header.schema = uuid;
}
// if examples in header, create ExampleOrRef node
if ('examples' in header && header.examples && typeof header.examples === 'object') {
Object.entries(header.examples).forEach(([exampleName, example]) => {
const uuid = uuidv4();
mapExample({ example, refUuidMap, uuidObjectHashMap, hashedNodeMap, uuid });
if (header.examples) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
header.examples[exampleName] = uuid;
}
});
}
// if content in header, create Media node
if ('content' in header && header.content && typeof header.content === 'object') {
Object.entries(header.content).forEach(([contentType, content]) => {
const uuid = uuidv4();
mapMedia({ content, refUuidMap, uuidObjectHashMap, hashedNodeMap, uuid });
if (header.content) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
header.content[contentType] = uuid;
}
});
}
// if encoding in header, create Encoding node
// TODO: add support for encoding
// add to hashedNodeMap
hashedNodeMap[objectHash] = header;
};