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