Skip to content

Commit

Permalink
Refactor findCommonOpenapiSchemaProperties using functional approach
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Dec 20, 2024
1 parent 5c164ae commit 97b8de8
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions src/utils/find-common-openapi-schema-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,20 @@ import type { OpenapiSchema } from 'lib/openapi.js'
export function findCommonOpenapiSchemaProperties(
schemas: OpenapiSchema[],
): Record<string, OpenapiSchema> {
if (schemas.length === 0 || schemas[0]?.properties == null) {
return {}
}

const firstSchema = schemas[0]
if (firstSchema.properties == null) {
if (schemas.length === 0 || !firstSchema?.properties) {

Check failure on line 7 in src/utils/find-common-openapi-schema-properties.ts

View workflow job for this annotation

GitHub Actions / Format code

Unexpected nullable object value in conditional. An explicit null check is required

Check failure on line 7 in src/utils/find-common-openapi-schema-properties.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v18)

Unexpected nullable object value in conditional. An explicit null check is required

Check failure on line 7 in src/utils/find-common-openapi-schema-properties.ts

View workflow job for this annotation

GitHub Actions / Lint (Node.js v20)

Unexpected nullable object value in conditional. An explicit null check is required
return {}
}

const firstSchemaPropKeys = Object.keys(firstSchema.properties)

const commonPropKeys = firstSchemaPropKeys.filter((propKey) =>
schemas.every((schema) =>
return Object.entries(firstSchema.properties).reduce<
Record<string, OpenapiSchema>
>((commonProps, [propKey, propValue]) => {
const isPropInAllSchemas = schemas.every((schema) =>
Object.keys(schema.properties ?? {}).includes(propKey),
),
)
)

return commonPropKeys.reduce<Record<string, OpenapiSchema>>(
(result, propKey) => {
const propValue = firstSchema.properties?.[propKey]
if (propValue != null) {
result[propKey] = propValue
}
return result
},
{},
)
return isPropInAllSchemas
? { ...commonProps, [propKey]: propValue }
: commonProps
}, {})
}

0 comments on commit 97b8de8

Please sign in to comment.