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

feat(yaml): support schema name for 'schema' option #5118

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 31 additions & 4 deletions yaml/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,31 @@
// This module is browser compatible.

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.
*/
// deno-lint-ignore no-explicit-any
schema?: any;
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
/** 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.
Expand All @@ -16,6 +38,7 @@ 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 {
replaceSchemaNameWithSchemaClass(options);
return load(content, options);
}

Expand Down Expand Up @@ -50,8 +73,12 @@ 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);
return loadAll(content, iteratorOrOption, options);
}
1 change: 1 addition & 0 deletions yaml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Deno.test({
};

assertEquals(parse(yaml, { schema: EXTENDED_SCHEMA }), expected);
assertEquals(parse(yaml, { schema: "extended" }), expected);
},
});

Expand Down
24 changes: 24 additions & 0 deletions yaml/schema/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { CORE_SCHEMA } from "./core.ts";
export { CORE_SCHEMA } from "./core.ts";
import { DEFAULT_SCHEMA } from "./default.ts";
export { DEFAULT_SCHEMA } from "./default.ts";
import { EXTENDED_SCHEMA } from "./extended.ts";
export { EXTENDED_SCHEMA } from "./extended.ts";
import { FAILSAFE_SCHEMA } from "./failsafe.ts";
export { FAILSAFE_SCHEMA } from "./failsafe.ts";
import { JSON_SCHEMA } from "./json.ts";
export { JSON_SCHEMA } from "./json.ts";
iuioiua marked this conversation as resolved.
Show resolved Hide resolved

export function replaceSchemaNameWithSchemaClass(
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
// deno-lint-ignore no-explicit-any
options?: { schema?: any },
) {
if (!options) return;
const name = options?.schema;
if (name === "core") {
options.schema = CORE_SCHEMA;

Check warning on line 25 in yaml/schema/mod.ts

View check run for this annotation

Codecov / codecov/patch

yaml/schema/mod.ts#L25

Added line #L25 was not covered by tests
} else if (name === "default") {
options.schema = DEFAULT_SCHEMA;

Check warning on line 27 in yaml/schema/mod.ts

View check run for this annotation

Codecov / codecov/patch

yaml/schema/mod.ts#L27

Added line #L27 was not covered by tests
} else if (name === "failsafe") {
options.schema = FAILSAFE_SCHEMA;

Check warning on line 29 in yaml/schema/mod.ts

View check run for this annotation

Codecov / codecov/patch

yaml/schema/mod.ts#L29

Added line #L29 was not covered by tests
} else if (name === "json") {
options.schema = JSON_SCHEMA;

Check warning on line 31 in yaml/schema/mod.ts

View check run for this annotation

Codecov / codecov/patch

yaml/schema/mod.ts#L31

Added line #L31 was not covered by tests
} else if (name === "extended") {
options.schema = EXTENDED_SCHEMA;
}
}
60 changes: 58 additions & 2 deletions yaml/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,64 @@
// This module is browser compatible.

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<string, "lowercase" | "uppercase" | "camelcase" | "decimal">;
/**
* Specifies a schema to use.
*
* Schema class or its name.
*/
// deno-lint-ignore no-explicit-any
schema?: any;
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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.
Expand All @@ -18,5 +73,6 @@ export function stringify(
data: unknown,
options?: DumpOptions,
): string {
replaceSchemaNameWithSchemaClass(options);
return dump(data, options);
}
4 changes: 4 additions & 0 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ undefined: !<tag:yaml.org,2002:js/undefined> ''
`;

assertEquals(stringify(object, { schema: EXTENDED_SCHEMA }), expected);
assertEquals(stringify(object, { schema: "extended" }), expected);
},
});

Expand All @@ -133,6 +134,9 @@ Deno.test({
assertThrows(
() => stringify({ function: func }, { schema: EXTENDED_SCHEMA }),
);
assertThrows(
() => stringify({ function: func }, { schema: "extended" }),
);
},
});

Expand Down