Skip to content

Commit

Permalink
refactor: Use named parameters instead of positional in `loadAndValid…
Browse files Browse the repository at this point in the history
…ateYaml`
  • Loading branch information
stefreak committed Dec 10, 2024
1 parent bc2812e commit a417eec
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
27 changes: 16 additions & 11 deletions core/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,17 @@ export const allConfigKinds = ["Module", "Workflow", "Project", configTemplateKi
* @param sourceDescription - A description of the location of the yaml file, e.g. "bar.yaml at directory /foo/".
* @param version - YAML standard version. Defaults to "1.2"
*/
export async function loadAndValidateYaml(
content: string,
sourceDescription: string,
filename: string | undefined,
version: DocumentOptions["version"] = "1.2"
): Promise<YamlDocumentWithSource[]> {
export async function loadAndValidateYaml({
content,
sourceDescription,
filename,
version = "1.2",
}: {
content: string
sourceDescription: string
filename: string | undefined
version?: DocumentOptions["version"]
}): Promise<YamlDocumentWithSource[]> {
try {
return Array.from(parseAllDocuments(content, { merge: true, strict: false, version }) || []).map((doc) => {
if (doc.errors.length > 0) {
Expand Down Expand Up @@ -200,11 +205,11 @@ export async function validateRawConfig({
projectRoot: string
allowInvalid?: boolean
}) {
let rawSpecs = await loadAndValidateYaml(
rawConfig,
`${basename(configPath)} in directory ${dirname(configPath)}`,
configPath
)
let rawSpecs = await loadAndValidateYaml({
content: rawConfig,
sourceDescription: `${basename(configPath)} in directory ${dirname(configPath)}`,
filename: configPath,
})

// Ignore empty resources
rawSpecs = rawSpecs.filter(Boolean)
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/kubernetes-type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ async function parseKubernetesManifests(
): Promise<KubernetesResource[]> {
// parse yaml with version 1.1 by default, as Kubernetes still uses this version.
// See also https://github.com/kubernetes/kubernetes/issues/34146
const docs = await loadAndValidateYaml(str, sourceDescription, "1.1")
const docs = await loadAndValidateYaml({ content: str, sourceDescription, filename, version: "1.1" })

// TODO: We should use schema validation to make sure that apiVersion, kind and metadata are always defined as required by the type.
const manifests = docs.map((d) => d.toJS()) as KubernetesResource[]
Expand Down
6 changes: 5 additions & 1 deletion core/test/unit/src/config/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,11 @@ describe("loadAndValidateYaml", () => {
name: foo
`

const yamlDocs = await loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml")
const yamlDocs = await loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
})

expect(yamlDocs).to.have.length(1)
expect(yamlDocs[0].source).to.equal(yaml)
Expand Down
6 changes: 5 additions & 1 deletion core/test/unit/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ describe("validateSchema", () => {
name: bar
`

const yamlDocs = await loadAndValidateYaml(yaml, "foo.yaml in directory bar", "bar/foo.yaml")
const yamlDocs = await loadAndValidateYaml({
content: yaml,
sourceDescription: "foo.yaml in directory bar",
filename: "bar/foo.yaml",
})
const yamlDoc = yamlDocs[1]

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
10 changes: 5 additions & 5 deletions core/test/unit/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,16 @@ describe("resolveTemplateString", () => {

it("if available, should include yaml context in error message", async () => {
const command = "${resol${part}ed}"
const yamlDoc = await loadAndValidateYaml(
dedent`
const yamlDoc = await loadAndValidateYaml({
content: dedent`
name: test,
kind: Build
spec:
command: '${command}'
`,
"test",
"bar/foo.yaml"
)
sourceDescription: "test",
filename: "bar/foo.yaml",
})
void expectError(
() =>
resolveTemplateString({
Expand Down

0 comments on commit a417eec

Please sign in to comment.