Skip to content

Instantly share code, notes, and snippets.

@jessekelly881
Created November 6, 2023 03:34
Show Gist options
  • Select an option

  • Save jessekelly881/59e7b32ff159af0736a5875307b531a2 to your computer and use it in GitHub Desktop.

Select an option

Save jessekelly881/59e7b32ff159af0736a5875307b531a2 to your computer and use it in GitHub Desktop.
Effect helpers for yaml
import { ParseResult, Schema } from "@effect/schema";
import YAML from "yaml";
export const parseYaml = <I, A extends string>(self: Schema.Schema<I, A>) =>
Schema.transformOrFail(
self,
Schema.unknown,
(s, _, ast) => {
try {
return ParseResult.success<unknown>(YAML.parse(s));
} catch (e: any) {
return ParseResult.failure(ParseResult.type(ast, s, e.message));
}
},
(u, _, ast) => {
try {
return ParseResult.success(YAML.stringify(u));
} catch (e: any) {
return ParseResult.failure(ParseResult.type(ast, u, e.message));
}
},
{ strict: false }
);
export const ParseYaml: Schema.Schema<string, unknown> = parseYaml(
Schema.string
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment