Skip to content

Commit

Permalink
fix(yaml): speciously restrictive type for stringify() (#4507)
Browse files Browse the repository at this point in the history
* fix(yaml): speciously restrictive type for stringify()

* fmt

---------

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
  • Loading branch information
hudlow and kt3k authored Mar 23, 2024
1 parent d78b585 commit 6be1650
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
6 changes: 3 additions & 3 deletions yaml/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import type { DumperStateOptions } from "./_dumper/dumper_state.ts";
export type DumpOptions = DumperStateOptions;

/**
* Serializes `object` as a YAML document.
* Serializes `data` as a YAML document.
*
* You can disable exceptions by setting the skipInvalid option to true.
*/
export function stringify(
obj: Record<string, unknown>,
data: unknown,
options?: DumpOptions,
): string {
return dump(obj, options);
return dump(data, options);
}
44 changes: 44 additions & 0 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,50 @@ binary: !<tag:yaml.org,2002:binary> SGVsbG8=
},
});

Deno.test({
name: "arrays can be stringified directly",
fn() {
const array = [1, 2, 3];

const expected = "- 1\n- 2\n- 3\n";

assertEquals(stringify(array), expected);
},
});

Deno.test({
name: "strings can be stringified directly",
fn() {
const string = "Hello world";

const expected = "Hello world\n";

assertEquals(stringify(string), expected);
},
});

Deno.test({
name: "numbers can be stringified directly",
fn() {
const number = 1.01;

const expected = "1.01\n";

assertEquals(stringify(number), expected);
},
});

Deno.test({
name: "booleans can be stringified directly",
fn() {
const boolean = true;

const expected = "true\n";

assertEquals(stringify(boolean), expected);
},
});

Deno.test({
name: "stringify() throws with `!!js/*` yaml types with default schemas",
fn() {
Expand Down

0 comments on commit 6be1650

Please sign in to comment.