@tsed/schema
Version:
JsonSchema module for Ts.ED Framework
52 lines (51 loc) • 1.47 kB
JavaScript
import { setValue } from "@tsed/core";
import { pascalCase } from "change-case";
import { SpecTypes } from "../domain/SpecTypes.js";
import { mergeSchema } from "./mergeSchema.js";
export function getSchemaFromRef($ref, options) {
const { components } = options;
if (components?.schemas && $ref) {
const host = getHost(options);
const refName = $ref.replace(`${host}/`, "");
return components.schemas[refName];
}
}
/**
* ignore
* @param options
*/
function getHost(options) {
const { host = `#/${[SpecTypes.OPENAPI, SpecTypes.ASYNCAPI].includes(options.specType) ? "components/schemas" : "definitions"}` } = options;
return host;
}
/**
* @ignore
*/
export function createRefName(name, options) {
if (options.groups && options.groups.length) {
return pascalCase(`${name} ${options.groupsName || options.groups.join(" ")}`);
}
return name;
}
/**
* @ignore
*/
export function createRef(name, schema, options) {
const host = getHost(options);
return mergeSchema({
$ref: `${host}/${name}`
}, {
...(schema.isReadOnly ? { readOnly: true } : {}),
...(schema.isWriteOnly ? { writeOnly: true } : {})
});
}
/**
* @ignore
*/
export function toRef(value, schema, options) {
const name = createRefName(value.getName(), options);
if (schema) {
setValue(options, `components.schemas.${name}`, schema);
}
return createRef(name, value, options);
}