Skip to content

Commit dddce79

Browse files
committed
chore: address reviewer feedback 3
1 parent 06d5731 commit dddce79

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

src/yaml.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ObjectSerializer } from './serializer.js';
99
* @param opts - Optional YAML parse options.
1010
* @returns The deserialized Kubernetes object.
1111
*/
12-
export function loadYaml<T>(data: string, opts?: yaml.ParseOptions): T {
12+
export function loadYaml<T>(data: string, opts?: yaml.ParseOptions & yaml.DocumentOptions): T {
1313
const yml = yaml.parse(data, { version: '1.1', ...opts }) as any as KubernetesObject;
1414
if (!yml) {
1515
throw new Error('Failed to load yaml');
@@ -24,7 +24,10 @@ export function loadYaml<T>(data: string, opts?: yaml.ParseOptions): T {
2424
* @param opts - Optional YAML parse options.
2525
* @returns An array of deserialized Kubernetes objects.
2626
*/
27-
export function loadAllYaml(data: string, opts?: yaml.ParseOptions): KubernetesObject[] {
27+
export function loadAllYaml(
28+
data: string,
29+
opts?: yaml.ParseOptions & yaml.DocumentOptions & yaml.SchemaOptions,
30+
): any[] {
2831
const ymls = yaml.parseAllDocuments(data, { version: '1.1', ...opts });
2932
return ymls.map((doc) => {
3033
const obj = doc.toJSON() as KubernetesObject;

src/yaml_test.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it } from 'node:test';
22
import { deepEqual, deepStrictEqual, strictEqual } from 'node:assert';
3-
import { V1CustomResourceDefinition, V1Namespace, V1Pod } from './api.js';
3+
import { V1CustomResourceDefinition, V1Namespace } from './api.js';
44
import { dumpYaml, loadAllYaml, loadYaml } from './yaml.js';
55

66
describe('yaml', () => {
@@ -84,27 +84,22 @@ spec:
8484
const objects = loadAllYaml(yaml);
8585

8686
strictEqual(objects.length, 3);
87-
// Assert specific types for each object
88-
const ns = objects[0] as V1Namespace;
89-
const pod = objects[1] as V1Pod;
90-
const crd = objects[2] as V1CustomResourceDefinition;
91-
92-
strictEqual(ns.kind, 'Namespace');
93-
strictEqual(pod.kind, 'Pod');
94-
strictEqual(ns.metadata!.name, 'some-namespace');
95-
strictEqual(pod.metadata!.name, 'some-pod');
96-
strictEqual(pod.metadata!.namespace, 'some-ns');
97-
98-
strictEqual(crd.apiVersion, 'apiextensions.k8s.io/v1');
99-
strictEqual(crd.kind, 'CustomResourceDefinition');
100-
strictEqual(crd.metadata!.name, 'my-crd.example.com');
87+
strictEqual(objects[0].kind, 'Namespace');
88+
strictEqual(objects[1].kind, 'Pod');
89+
strictEqual(objects[0].metadata.name, 'some-namespace');
90+
strictEqual(objects[1].metadata.name, 'some-pod');
91+
strictEqual(objects[1].metadata.namespace, 'some-ns');
92+
strictEqual(objects[2].apiVersion, 'apiextensions.k8s.io/v1');
93+
strictEqual(objects[2].kind, 'CustomResourceDefinition');
94+
strictEqual(objects[2].metadata!.name, 'my-crd.example.com');
10195
strictEqual(
102-
crd.spec.versions[0]!.schema!.openAPIV3Schema!.properties!['foobar'].x_kubernetes_int_or_string,
96+
objects[2].spec.versions[0]!.schema!.openAPIV3Schema!.properties!['foobar']
97+
.x_kubernetes_int_or_string,
10398
true,
10499
);
105100
strictEqual(
106-
crd.spec.versions[0]!.schema!.openAPIV3Schema!.properties!['foobar'][
107-
'x-kubernetes-int-or-string' // This access is still on the parsed object before type mapping, hence `undefined` is correct
101+
objects[2].spec.versions[0]!.schema!.openAPIV3Schema!.properties!['foobar'][
102+
'x-kubernetes-int-or-string'
108103
],
109104
undefined,
110105
);

0 commit comments

Comments
 (0)