Skip to content

Commit

Permalink
Use Readonly to prevent modifying input
Browse files Browse the repository at this point in the history
  • Loading branch information
amcaplan committed Dec 9, 2024
1 parent fef9338 commit d55cc35
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ export async function writeAppConfigurationFile(configuration: CurrentAppConfigu
writeFileSync(configuration.path, file)
}

export const rewriteConfiguration = <T extends zod.ZodTypeAny>(schema: T, config: unknown): unknown => {
const configCopy = config

export const rewriteConfiguration = <T extends zod.ZodTypeAny>(schema: T, config: Readonly<unknown> | undefined): unknown => {

Check failure on line 30 in packages/app/src/cli/services/app/write-app-configuration-file.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/write-app-configuration-file.ts#L30

[prettier/prettier] Replace `schema:·T,·config:·Readonly<unknown>·|·undefined` with `⏎··schema:·T,⏎··config:·Readonly<unknown>·|·undefined,⏎`
if (schema === null || schema === undefined) return null
if (schema instanceof zod.ZodNullable || schema instanceof zod.ZodOptional)
return rewriteConfiguration(schema.unwrap(), configCopy)
return rewriteConfiguration(schema.unwrap(), config)
if (schema instanceof zod.ZodArray) {
return (configCopy as unknown[]).map((item) => rewriteConfiguration(schema.element, item))
return (config as Readonly<unknown>[]).map((item) => rewriteConfiguration(schema.element, item))
}
if (schema instanceof zod.ZodEffects) {
return rewriteConfiguration(schema._def.schema, configCopy)
return rewriteConfiguration(schema._def.schema, config)
}
if (schema instanceof zod.ZodObject) {
const entries = Object.entries(schema.shape)
const confObj = configCopy as {[key: string]: unknown}
const confObj = config as {[key: string]: Readonly<unknown>}
let result: {[key: string]: unknown} = {}
entries.forEach(([key, subSchema]) => {
if (confObj !== undefined && confObj[key] !== undefined) {
Expand Down

0 comments on commit d55cc35

Please sign in to comment.