@tsed/schema
Version:
JsonSchema module for Ts.ED Framework
51 lines • 1.38 kB
JavaScript
import { cleanObject, setValue } from "@tsed/core";
import { pascalCase } from "change-case";
import { SpecTypes } from "../domain/SpecTypes.js";
/**
* 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);
const ref = {
$ref: `${host}/${name}`
};
const readOnly = schema.isReadOnly;
const writeOnly = schema.isWriteOnly;
return cleanObject({
readOnly: readOnly ? true : undefined,
writeOnly: writeOnly ? true : undefined,
...(readOnly || writeOnly
? {
anyOf: [ref]
}
: ref)
});
}
/**
* @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);
}
//# sourceMappingURL=ref.js.map