UNPKG

@tsed/schema

Version:
31 lines (30 loc) 1.27 kB
import { isArray } from "@tsed/core"; import { VendorKeys } from "../../constants/VendorKeys.js"; import { alterIgnore } from "../../hooks/alterIgnore.js"; import { execMapper, registerJsonSchemaMapper } from "../../registries/JsonSchemaMapperContainer.js"; /** * Serialize Any object to a json schema * @param input * @param options * @ignore */ export function objectMapper(input, options) { const { specType, operationIdFormatter, root, components, useAlias, ...ctx } = options; return Object.entries(input).reduce((obj, [key, value]) => { if (options.withIgnoredProps !== false && !alterIgnore(value, ctx)) { const opts = { ...options, parent: value, groups: input?.get?.(VendorKeys.FORWARD_GROUPS) || value?.get?.(VendorKeys.FORWARD_GROUPS) ? options.groups : undefined }; obj[key] = execMapper("item", [value], opts); obj[key] = execMapper("nullable", [obj[key], value], opts); if (value.isGeneric && obj[key]?.type) { const { type, ...rest } = obj[key]; obj[key] = rest; } } return obj; }, isArray(input) ? [] : {}); } registerJsonSchemaMapper("object", objectMapper);