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 all 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
39 changes: 34 additions & 5 deletions yaml/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
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
40 changes: 35 additions & 5 deletions yaml/schema/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
options?: { schema?: string | Schema },
) {
switch (options?.schema) {
case "core":
options.schema = CORE_SCHEMA;
break;

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

View check run for this annotation

Codecov / codecov/patch

yaml/schema/mod.ts#L26-L27

Added lines #L26 - L27 were not covered by tests
case "default":
options.schema = DEFAULT_SCHEMA;
break;

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

View check run for this annotation

Codecov / codecov/patch

yaml/schema/mod.ts#L29-L30

Added lines #L29 - L30 were not covered by tests
case "failsafe":
options.schema = FAILSAFE_SCHEMA;
break;

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

View check run for this annotation

Codecov / codecov/patch

yaml/schema/mod.ts#L32-L33

Added lines #L32 - L33 were not covered by tests
case "json":
options.schema = JSON_SCHEMA;
break;

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

View check run for this annotation

Codecov / codecov/patch

yaml/schema/mod.ts#L35-L36

Added lines #L35 - L36 were not covered by tests
case "extended":
options.schema = EXTENDED_SCHEMA;
break;
}
}
63 changes: 60 additions & 3 deletions yaml/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, "lowercase" | "uppercase" | "camelcase" | "decimal">;
/**
* 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.
Expand All @@ -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);
}
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