Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import * as SchemaGetter from "effect/SchemaGetter";
4import * as SchemaIssue from "effect/SchemaIssue";
5import * as SchemaTransformation from "effect/SchemaTransformation";
6import {
7 YAMLParseError,
8 parse as parseYamlString,
9 stringify as stringifyYamlValue,
10 type CreateNodeOptions,
11 type DocumentOptions,
12 type ParseOptions,
13 type SchemaOptions,
14 type ToJSOptions,
15 type ToStringOptions,
16} from "yaml";
17
18export type YamlParseOptions = ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions;
19export type YamlStringifyOptions = DocumentOptions &
20 SchemaOptions &
21 ParseOptions &
22 CreateNodeOptions &
23 ToStringOptions;
24
25function formatYamlParseError(error: unknown): string {
26 if (!(error instanceof YAMLParseError)) {
27 return "Invalid YAML.";
28 }
29
30 const position = error.linePos?.[0];
31 const location = position === undefined ? "" : `, line=${position.line}, column=${position.col}`;
32 return `Invalid YAML (code=${error.code}${location}).`;
33}
34
35/**
36 * Parses a YAML string into a value.
37 *
38 * **When to use**
39 *
40 * Use when you need a schema getter to parse a present encoded YAML string
41 * during decoding.
42 *
43 * **Details**
44 *
45 * Parse failures become `SchemaIssue.InvalidValue` values.
46 *
47 * **Example** (Parse YAML)
48 *
49 * ```ts
50 * import { parseYaml } from "@t3tools/shared/schemaYaml"
51 *
52 * const parse = parseYaml<string>()
53 * // Getter<unknown, string>
54 * ```
55 *
56 * @see {@link stringifyYaml} for the inverse operation
57 */
58export function parseYaml<E extends string>(
59 options?: YamlParseOptions,
60): SchemaGetter.Getter<unknown, E> {
61 return SchemaGetter.transformOrFail((input: E) =>
62 Effect.try({
63 try: () => parseYamlString(input, options) as unknown,
64 catch: (error) => new SchemaIssue.InvalidValue({ message: formatYamlParseError(error) }),
65 }),
66 );
67}
68
69/**
70 * Stringifies a present value as YAML.
71 *
72 * **When to use**
73 *
74 * Use when you need a schema getter to serialize a present decoded value to
75 * YAML text during encoding.
76 *
77 * **Details**
78 *
79 * Stringify failures become `SchemaIssue.InvalidValue` values.
80 *
81 * **Example** (Stringify YAML)
82 *
83 * ```ts
84 * import { stringifyYaml } from "@t3tools/shared/schemaYaml"
85 *
86 * const stringify = stringifyYaml()
87 * // Getter<string, unknown>
88 * ```
89 *
90 * @see {@link parseYaml} for the inverse operation
91 */
92export function stringifyYaml(
93 options?: YamlStringifyOptions,
94): SchemaGetter.Getter<string, unknown> {
95 return SchemaGetter.transformOrFail((input: unknown) =>
96 Effect.try({
97 try: () => stringifyYamlValue(input, options),
98 catch: () => new SchemaIssue.InvalidValue({ message: "Failed to stringify YAML." }),
99 }),
100 );
101}
102
103/**
104 * Decodes a YAML string and encodes a value as YAML text.
105 *
106 * **When to use**
107 *
108 * Use when you need a schema transformation to decode YAML stored or
109 * transmitted as a string before validating the parsed structure.
110 *
111 * **Details**
112 *
113 * Decode and encode failures become `InvalidValue` schema issues.
114 *
115 * **Example** (Parsing YAML)
116 *
117 * ```ts
118 * import * as Schema from "effect/Schema"
119 * import { fromYamlString } from "@t3tools/shared/schemaYaml"
120 *
121 * const schema = Schema.String.pipe(Schema.decodeTo(Schema.Unknown, fromYamlString))
122 * ```
123 */
124export const fromYamlString = new SchemaTransformation.Transformation<unknown, string>(
125 parseYaml(),
126 stringifyYaml(),
127);
128
129/**
130 * Build a schema that decodes a YAML string into `A`.
131 *
132 * Decode parses the input as YAML before validating the parsed value with the
133 * provided schema. Encode validates the value and serializes it as YAML text.
134 */
135export const fromYaml = <S extends Schema.Top>(schema: S) =>
136 Schema.String.pipe(Schema.decodeTo(schema, fromYamlString));