Created
November 6, 2023 03:34
-
-
Save jessekelly881/59e7b32ff159af0736a5875307b531a2 to your computer and use it in GitHub Desktop.
Effect helpers for yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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