Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

schemaYaml.ts

139 lines3.8 KB

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