Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(schema): simplify handling #33963

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/modules/manager/argocd/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { coerceArray } from '../../../util/array';
import { regEx } from '../../../util/regex';
import { withDebugMessage } from '../../../util/schema-utils';
import { trimTrailingSlash } from '../../../util/url';
import { parseYaml } from '../../../util/yaml';
import { DockerDatasource } from '../../datasource/docker';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { HelmDatasource } from '../../datasource/helm';
Expand All @@ -15,7 +15,8 @@ import type {
PackageFileContent,
} from '../types';
import {
ApplicationDefinition,
type ApplicationDefinition,
ApplicationDefinitionSchema,
type ApplicationSource,
type ApplicationSpec,
} from './schema';
Expand All @@ -36,11 +37,9 @@ export function extractPackageFile(
return null;
}

const definitions = parseYaml(content, {
customSchema: ApplicationDefinition,
failureBehaviour: 'filter',
removeTemplates: true,
});
const definitions = ApplicationDefinitionSchema.catch(
withDebugMessage([], `error parsing ${packageFile}`),
).parse(content);

const deps = definitions.flatMap(processAppSpec);

Expand Down
6 changes: 5 additions & 1 deletion lib/modules/manager/argocd/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { LooseArray } from '../../../util/schema-utils';
import { LooseArray, multidocYaml } from '../../../util/schema-utils';

export const KubernetesResource = z.object({
apiVersion: z.string(),
Expand Down Expand Up @@ -38,3 +38,7 @@ export const ApplicationSet = KubernetesResource.extend({

export const ApplicationDefinition = Application.or(ApplicationSet);
export type ApplicationDefinition = z.infer<typeof ApplicationDefinition>;

export const ApplicationDefinitionSchema = multidocYaml({
removeTemplates: true,
}).pipe(LooseArray(ApplicationDefinition));
21 changes: 21 additions & 0 deletions lib/util/schema-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Toml,
UtcDate,
Yaml,
multidocYaml,
withDebugMessage,
withTraceMessage,
} from './schema-utils';
Expand Down Expand Up @@ -447,6 +448,26 @@ describe('util/schema-utils', () => {
});
});

describe('multidocYaml()', () => {
const Schema = multidocYaml().pipe(
z.array(
z.object({
foo: z.number(),
}),
),
);

it('parses valid yaml', () => {
expect(
Schema.parse(codeBlock`
foo: 111
---
foo: 222
`),
).toEqual([{ foo: 111 }, { foo: 222 }]);
});
});

describe('Toml', () => {
const Schema = Toml.pipe(
z.object({ foo: z.object({ bar: z.literal('baz') }) }),
Expand Down
22 changes: 21 additions & 1 deletion lib/util/schema-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import JSON5 from 'json5';
import * as JSONC from 'jsonc-parser';
import { DateTime } from 'luxon';
import type { JsonArray, JsonValue } from 'type-fest';
import { type ZodEffects, type ZodType, type ZodTypeDef, z } from 'zod';
import {
type ZodEffects,
type ZodString,
type ZodType,
type ZodTypeDef,
z,
} from 'zod';
import { logger } from '../logger';
import type { PackageDependency } from '../modules/manager/types';
import { parse as parseToml } from './toml';
import type { YamlOptions } from './yaml';
import { parseSingleYaml, parseYaml } from './yaml';

interface ErrorContext<T> {
Expand Down Expand Up @@ -257,6 +264,19 @@ export const MultidocYaml = z.string().transform((str, ctx): JsonArray => {
}
});

export function multidocYaml(
opts?: Omit<YamlOptions, 'customSchema'>,
): ZodEffects<ZodString, JsonArray, string> {
return z.string().transform((str, ctx): JsonArray => {
try {
return parseYaml(str, opts) as JsonArray;
} catch {
ctx.addIssue({ code: 'custom', message: 'Invalid YAML' });
return z.NEVER;
}
});
}

export const Toml = z.string().transform((str, ctx) => {
try {
return parseToml(str);
Expand Down
4 changes: 2 additions & 2 deletions lib/util/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ZodType } from 'zod';
import { logger } from '../logger';
import { regEx } from './regex';

interface YamlOptions<
export interface YamlOptions<
ResT = unknown,
Schema extends ZodType<ResT> = ZodType<ResT>,
> extends ParseOptions,
Expand All @@ -28,7 +28,7 @@ interface YamlParseDocumentOptions
removeTemplates?: boolean;
}

interface YamlOptionsMultiple<
export interface YamlOptionsMultiple<
ResT = unknown,
Schema extends ZodType<ResT> = ZodType<ResT>,
> extends YamlOptions<ResT, Schema> {
Expand Down