Open History to see the last commit
1import {
2 Cause,
3 Effect,
4 Exit,
5 Option,
6 Result,
7 Schema,
8 SchemaGetter,
9 SchemaIssue,
10 SchemaTransformation,
11} from "effect";
12
13export const decodeJsonResult = <S extends Schema.Codec<unknown, unknown, never, never>>(
14 schema: S,
15) => {
16 const decode = Schema.decodeExit(Schema.fromJsonString(schema));
17 return (input: string) => {
18 const result = decode(input);
19 if (Exit.isFailure(result)) {
20 return Result.fail(result.cause);
21 }
22 return Result.succeed(result.value);
23 };
24};
25
26export const decodeUnknownJsonResult = <S extends Schema.Codec<unknown, unknown, never, never>>(
27 schema: S,
28) => {
29 const decode = Schema.decodeUnknownExit(Schema.fromJsonString(schema));
30 return (input: unknown) => {
31 const result = decode(input);
32 if (Exit.isFailure(result)) {
33 return Result.fail(result.cause);
34 }
35 return Result.succeed(result.value);
36 };
37};
38
39export const formatSchemaError = (cause: Cause.Cause<Schema.SchemaError>) => {
40 const squashed = Cause.squash(cause);
41 return Schema.isSchemaError(squashed)
42 ? SchemaIssue.makeFormatterDefault()(squashed.issue)
43 : Cause.pretty(cause);
44};
45
46/**
47 * A `Getter` that parses a lenient JSON string (tolerating trailing commas
48 * and JS-style comments) into an unknown value.
49 *
50 * Mirrors `SchemaGetter.parseJson()` but uses `parseLenientJson` instead
51 * of `JSON.parse`.
52 */
53const parseLenientJsonGetter = SchemaGetter.onSome((input: string) =>
54 Effect.try({
55 try: () => {
56 // Strip single-line comments — alternation preserves quoted strings.
57 let stripped = input.replace(
58 /("(?:[^"\\]|\\.)*")|\/\/[^\n]*/g,
59 (match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
60 );
61
62 // Strip multi-line comments.
63 stripped = stripped.replace(
64 /("(?:[^"\\]|\\.)*")|\/\*[\s\S]*?\*\//g,
65 (match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
66 );
67
68 // Strip trailing commas before `}` or `]`.
69 stripped = stripped.replace(/,(\s*[}\]])/g, "$1");
70
71 return Option.some(JSON.parse(stripped));
72 },
73 catch: (e) => new SchemaIssue.InvalidValue(Option.some(input), { message: String(e) }),
74 }),
75);
76
77/**
78 * Schema transformation: lenient JSONC string ↔ unknown.
79 *
80 * Same API as `SchemaTransformation.fromJsonString`, but the decode side
81 * strips trailing commas and JS-style comments before parsing.
82 * Encoding produces strict JSON via `JSON.stringify`.
83 */
84export const fromLenientJsonString = new SchemaTransformation.Transformation(
85 parseLenientJsonGetter,
86 SchemaGetter.stringifyJson(),
87);
88
89/**
90 * Build a schema that decodes a lenient JSON string into `A`.
91 *
92 * Drop-in replacement for `Schema.fromJsonString(schema)` that tolerates
93 * trailing commas and comments in the input.
94 */
95export const fromLenientJson = <S extends Schema.Top>(schema: S) =>
96 Schema.String.pipe(Schema.decodeTo(schema, fromLenientJsonString));