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

refactor(yaml): consolidate yaml/schema/*.ts code into yaml/_schema.ts #5225

Merged
merged 1 commit into from
Jul 1, 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
108 changes: 108 additions & 0 deletions yaml/_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
import { YamlError } from "./_error.ts";
import type { KindType, Type } from "./_type.ts";
import type { Any, ArrayObject } from "./_utils.ts";
import {
binary,
bool,
float,
int,
map,
merge,
nil,
omap,
pairs,
regexp,
seq,
set,
str,
timestamp,
undefinedType,
} from "./_type/mod.ts";

function compileList(
schema: Schema,
Expand Down Expand Up @@ -92,3 +109,94 @@
explicit?: Type[];
include?: Schema[];
}

/**
* Standard YAML's failsafe schema.
*
* @see {@link http://www.yaml.org/spec/1.2/spec.html#id2802346}
*/
const FAILSAFE_SCHEMA: Schema = new Schema({
explicit: [str, seq, map],
});

/**
* Standard YAML's JSON schema.
*
* @see {@link http://www.yaml.org/spec/1.2/spec.html#id2803231}
*/
const JSON_SCHEMA: Schema = new Schema({
implicit: [nil, bool, int, float],
include: [FAILSAFE_SCHEMA],
});

/**
* Standard YAML's core schema.
*
* @see {@link http://www.yaml.org/spec/1.2/spec.html#id2804923}
*/
const CORE_SCHEMA: Schema = new Schema({
include: [JSON_SCHEMA],
});

/**
* Default YAML schema. It is not described in the YAML specification.
*/
export const DEFAULT_SCHEMA: Schema = new Schema({
explicit: [binary, omap, pairs, set],
implicit: [timestamp, merge],
include: [CORE_SCHEMA],
});

/***
* Extends JS-YAML default schema with additional JavaScript types
* It is not described in the YAML specification.
* Functions are no longer supported for security reasons.
*
* @example
* ```ts
* import { parse } from "@std/yaml";
*
* const data = parse(
* `
* regexp:
* simple: !!js/regexp foobar
* modifiers: !!js/regexp /foobar/mi
* undefined: !!js/undefined ~
* # Disabled, see: https://github.com/denoland/deno_std/pull/1275
* # function: !!js/function >
* # function foobar() {
* # return 'hello world!';
* # }
* `,
* { schema: "extended" },
* );
* ```
*/
const EXTENDED_SCHEMA: Schema = new Schema({
explicit: [regexp, undefinedType],
include: [DEFAULT_SCHEMA],
});

export function replaceSchemaNameWithSchemaClass(
options?: {
schema?: "core" | "default" | "failsafe" | "json" | "extended" | unknown;
},
) {
switch (options?.schema) {
case "core":
options.schema = CORE_SCHEMA;
break;

Check warning on line 188 in yaml/_schema.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_schema.ts#L187-L188

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

Check warning on line 191 in yaml/_schema.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_schema.ts#L190-L191

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

Check warning on line 194 in yaml/_schema.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_schema.ts#L193-L194

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

Check warning on line 197 in yaml/_schema.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_schema.ts#L196-L197

Added lines #L196 - L197 were not covered by tests
case "extended":
options.schema = EXTENDED_SCHEMA;
break;
}
}
3 changes: 1 addition & 2 deletions yaml/_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import type { SchemaDefinition } from "./_schema.ts";
import { DEFAULT_SCHEMA } from "./schema/mod.ts";
import { DEFAULT_SCHEMA, type SchemaDefinition } from "./_schema.ts";

export abstract class State {
schema: SchemaDefinition;
Expand Down
2 changes: 1 addition & 1 deletion yaml/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// This module is browser compatible.

import { load, loadDocuments } from "./_loader/loader.ts";
import { replaceSchemaNameWithSchemaClass } from "./schema/mod.ts";
import { replaceSchemaNameWithSchemaClass } from "./_schema.ts";

/**
* Options for parsing YAML.
Expand Down
17 changes: 0 additions & 17 deletions yaml/schema/core.ts

This file was deleted.

18 changes: 0 additions & 18 deletions yaml/schema/default.ts

This file was deleted.

36 changes: 0 additions & 36 deletions yaml/schema/extended.ts

This file was deleted.

17 changes: 0 additions & 17 deletions yaml/schema/failsafe.ts

This file was deleted.

19 changes: 0 additions & 19 deletions yaml/schema/json.ts

This file was deleted.

42 changes: 0 additions & 42 deletions yaml/schema/mod.ts

This file was deleted.

2 changes: 1 addition & 1 deletion yaml/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// This module is browser compatible.

import { dump } from "./_dumper/dumper.ts";
import { replaceSchemaNameWithSchemaClass } from "./schema/mod.ts";
import { replaceSchemaNameWithSchemaClass } from "./_schema.ts";

/**
* The option for strinigfy.
Expand Down