MCPcopy Create free account
hub / github.com/middleapi/orpc / separateObjectSchema

Function separateObjectSchema

packages/openapi/src/schema-utils.ts:63–138  ·  view source on GitHub ↗
(schema: ObjectSchema, separatedProperties: string[])

Source from the content-addressed store, hash-verified

61 * @internal
62 */
63export function separateObjectSchema(schema: ObjectSchema, separatedProperties: string[]): [matched: ObjectSchema, rest: ObjectSchema] {
64 if (Object.keys(schema).some(
65 k => !['type', 'properties', 'required', 'additionalProperties'].includes(k)
66 && LOGIC_KEYWORDS.includes(k)
67 && (schema as any)[k] !== undefined,
68 )) {
69 return [{ type: 'object' }, schema]
70 }
71
72 const matched: ObjectSchema = { ...schema }
73 const rest: ObjectSchema = { ...schema }
74
75 matched.properties = separatedProperties
76 .reduce((acc: Record<string, JSONSchema>, key) => {
77 const keySchema = schema.properties?.[key] ?? schema.additionalProperties
78
79 if (keySchema !== undefined) {
80 acc[key] = keySchema
81 }
82
83 return acc
84 }, {})
85
86 if (Object.keys(matched.properties).length === 0) {
87 matched.properties = undefined
88 }
89
90 matched.required = schema.required?.filter(key => separatedProperties.includes(key))
91
92 if (matched.required?.length === 0) {
93 matched.required = undefined
94 }
95
96 matched.examples = schema.examples?.map((example) => {
97 if (!isObject(example)) {
98 return example
99 }
100
101 return Object.entries(example).reduce((acc, [key, value]) => {
102 if (separatedProperties.includes(key)) {
103 acc[key] = value
104 }
105
106 return acc
107 }, {} as Record<string, unknown>)
108 })
109
110 rest.properties = schema.properties && Object.entries(schema.properties)
111 .filter(([key]) => !separatedProperties.includes(key))
112 .reduce((acc: Record<string, JSONSchema> = {}, [key, value]) => {
113 acc[key] = value
114 return acc
115 }, undefined)
116
117 rest.required = schema.required?.filter(key => !separatedProperties.includes(key))
118
119 if (rest.required?.length === 0) {
120 rest.required = undefined

Callers 2

#requestMethod · 0.90

Calls 1

isObjectFunction · 0.90

Tested by

no test coverage detected