diff --git a/yaml/parse.ts b/yaml/parse.ts index 23182072a0c9..72355b4435a0 100644 --- a/yaml/parse.ts +++ b/yaml/parse.ts @@ -4,10 +4,32 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. +import type { Schema } from "./schema.ts"; import { type CbFunction, load, loadAll } from "./_loader/loader.ts"; -import type { LoaderStateOptions } from "./_loader/loader_state.ts"; +import { replaceSchemaNameWithSchemaClass } from "./mod.ts"; -export type ParseOptions = LoaderStateOptions; +/** + * Options for parsing YAML. + */ +export interface ParseOptions { + /** Uses legacy mode */ + legacy?: boolean; + /** The listener */ + // deno-lint-ignore no-explicit-any + listener?: ((...args: any[]) => void) | null; + /** string to be used as a file path in error/warning messages. */ + filename?: string; + /** + * Specifies a schema to use. + * + * Schema class or its name. + */ + schema?: string | Schema; + /** compatibility with JSON.parse behaviour. */ + json?: boolean; + /** function to call on warning messages. */ + onWarning?(this: null, e?: Error): void; +} /** * Parses `content` as single YAML document. @@ -16,7 +38,9 @@ export type ParseOptions = LoaderStateOptions; * By default, does not support regexps, functions and undefined. This method is safe for untrusted data. */ export function parse(content: string, options?: ParseOptions): unknown { - return load(content, options); + replaceSchemaNameWithSchemaClass(options); + // deno-lint-ignore no-explicit-any + return load(content, options as any); } /** @@ -50,8 +74,13 @@ export function parseAll( export function parseAll(content: string, options?: ParseOptions): unknown; export function parseAll( content: string, - iterator?: CbFunction | ParseOptions, + iteratorOrOption?: CbFunction | ParseOptions, options?: ParseOptions, ): unknown { - return loadAll(content, iterator, options); + if (typeof iteratorOrOption !== "function") { + replaceSchemaNameWithSchemaClass(iteratorOrOption); + } + replaceSchemaNameWithSchemaClass(options); + // deno-lint-ignore no-explicit-any + return loadAll(content, iteratorOrOption as any, options as any); } diff --git a/yaml/parse_test.ts b/yaml/parse_test.ts index d04a9d90f7ca..c13134ac9b6e 100644 --- a/yaml/parse_test.ts +++ b/yaml/parse_test.ts @@ -86,6 +86,7 @@ Deno.test({ }; assertEquals(parse(yaml, { schema: EXTENDED_SCHEMA }), expected); + assertEquals(parse(yaml, { schema: "extended" }), expected); }, }); diff --git a/yaml/schema/mod.ts b/yaml/schema/mod.ts index bf20c5809f12..8061ff20fb25 100644 --- a/yaml/schema/mod.ts +++ b/yaml/schema/mod.ts @@ -4,8 +4,38 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. -export { CORE_SCHEMA } from "./core.ts"; -export { DEFAULT_SCHEMA } from "./default.ts"; -export { EXTENDED_SCHEMA } from "./extended.ts"; -export { FAILSAFE_SCHEMA } from "./failsafe.ts"; -export { JSON_SCHEMA } from "./json.ts"; +import type { Schema } from "../schema.ts"; +import { CORE_SCHEMA } from "./core.ts"; +import { DEFAULT_SCHEMA } from "./default.ts"; +import { EXTENDED_SCHEMA } from "./extended.ts"; +import { FAILSAFE_SCHEMA } from "./failsafe.ts"; +import { JSON_SCHEMA } from "./json.ts"; +export { + CORE_SCHEMA, + DEFAULT_SCHEMA, + EXTENDED_SCHEMA, + FAILSAFE_SCHEMA, + JSON_SCHEMA, +}; + +export function replaceSchemaNameWithSchemaClass( + options?: { schema?: string | Schema }, +) { + switch (options?.schema) { + case "core": + options.schema = CORE_SCHEMA; + break; + case "default": + options.schema = DEFAULT_SCHEMA; + break; + case "failsafe": + options.schema = FAILSAFE_SCHEMA; + break; + case "json": + options.schema = JSON_SCHEMA; + break; + case "extended": + options.schema = EXTENDED_SCHEMA; + break; + } +} diff --git a/yaml/stringify.ts b/yaml/stringify.ts index 5b0bfee52103..dac681e115df 100644 --- a/yaml/stringify.ts +++ b/yaml/stringify.ts @@ -4,10 +4,65 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. +import type { Schema } from "./schema.ts"; import { dump } from "./_dumper/dumper.ts"; -import type { DumperStateOptions } from "./_dumper/dumper_state.ts"; +import { replaceSchemaNameWithSchemaClass } from "./mod.ts"; -export type DumpOptions = DumperStateOptions; +/** + * The option for strinigfy. + */ +export type DumpOptions = { + /** Indentation width to use (in spaces). */ + indent?: number; + /** When true, will not add an indentation level to array elements */ + noArrayIndent?: boolean; + /** + * Do not throw on invalid types (like function in the safe schema) + * and skip pairs and single values with such types. + */ + skipInvalid?: boolean; + /** + * Specifies level of nesting, when to switch from + * block to flow style for collections. -1 means block style everywhere + */ + flowLevel?: number; + /** Each tag may have own set of styles. - "tag" => "style" map. */ + styles?: Record; + /** + * Specifies a schema to use. + * + * Schema class or its name. + */ + schema?: string | Schema; + /** + * If true, sort keys when dumping YAML in ascending, ASCII character order. + * If a function, use the function to sort the keys. (default: false) + * If a function is specified, the function must return a negative value + * if first argument is less than second argument, zero if they're equal + * and a positive value otherwise. + */ + sortKeys?: boolean | ((a: string, b: string) => number); + /** Set max line width. (default: 80) */ + lineWidth?: number; + /** + * If true, don't convert duplicate objects + * into references (default: false) + */ + noRefs?: boolean; + /** + * If true don't try to be compatible with older yaml versions. + * Currently: don't quote "yes", "no" and so on, + * as required for YAML 1.1 (default: false) + */ + noCompatMode?: boolean; + /** + * If true flow sequences will be condensed, omitting the + * space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`. + * Can be useful when using yaml for pretty URL query params + * as spaces are %-encoded. (default: false). + */ + condenseFlow?: boolean; +}; /** * Serializes `data` as a YAML document. @@ -18,5 +73,7 @@ export function stringify( data: unknown, options?: DumpOptions, ): string { - return dump(data, options); + replaceSchemaNameWithSchemaClass(options); + // deno-lint-ignore no-explicit-any + return dump(data, options as any); } diff --git a/yaml/stringify_test.ts b/yaml/stringify_test.ts index 545b4ad643de..b18af76adca9 100644 --- a/yaml/stringify_test.ts +++ b/yaml/stringify_test.ts @@ -119,6 +119,7 @@ undefined: ! '' `; assertEquals(stringify(object, { schema: EXTENDED_SCHEMA }), expected); + assertEquals(stringify(object, { schema: "extended" }), expected); }, }); @@ -133,6 +134,9 @@ Deno.test({ assertThrows( () => stringify({ function: func }, { schema: EXTENDED_SCHEMA }), ); + assertThrows( + () => stringify({ function: func }, { schema: "extended" }), + ); }, });