@mintlify/validation
Version:
Validates mint.json files
54 lines (53 loc) • 2.43 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 mapParameter = ({ parameter, refUuidMap, uuidObjectHashMap, hashedNodeMap, uuid, }) => {
const objectHash = hash(parameter);
uuidObjectHashMap[uuid] = objectHash;
// if we've seen this exact object before, skip processing
if (objectHash in hashedNodeMap) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameter = objectHash;
return;
}
// if ref, replace with uuid
if ('$ref' in parameter && parameter.$ref) {
const refId = parameter.$ref;
const refUuid = refUuidMap[refId] || uuidv4();
parameter.$ref = refUuid;
}
// if not ref
// if schema in parameter, create SchemaOrRef node
if ('schema' in parameter && parameter.schema && typeof parameter.schema === 'object') {
const uuid = uuidv4();
mapSchema({ schema: parameter.schema, refUuidMap, uuidObjectHashMap, hashedNodeMap, uuid });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameter.schema = uuid;
}
// if examples in parameter, create ExampleOrRef node
if ('examples' in parameter && parameter.examples && typeof parameter.examples === 'object') {
Object.entries(parameter.examples).forEach(([exampleName, example]) => {
const uuid = uuidv4();
mapExample({ example, refUuidMap, uuidObjectHashMap, hashedNodeMap, uuid });
if (parameter.examples) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameter.examples[exampleName] = uuid;
}
});
}
// if content in parameter, create Media node
if ('content' in parameter && parameter.content && typeof parameter.content === 'object') {
Object.entries(parameter.content).forEach(([contentType, content]) => {
const uuid = uuidv4();
mapMedia({ content, refUuidMap, uuidObjectHashMap, hashedNodeMap, uuid });
if (parameter.content) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameter.content[contentType] = uuid;
}
});
}
// add to hashedNodeMap
hashedNodeMap[objectHash] = parameter;
};