| ... | ... | @@ -8,6 +8,104 @@ import * as SchemaGetter from "effect/SchemaGetter"; |
| 8 | 8 | import * as SchemaIssue from "effect/SchemaIssue"; |
| 9 | 9 | import * as SchemaTransformation from "effect/SchemaTransformation"; |
| 10 | 10 | |
| 11 | const MAX_SCHEMA_DIAGNOSTIC_ISSUES = 8; |
| 12 | const MAX_SCHEMA_DIAGNOSTIC_PATH_SEGMENTS = 16; |
| 13 | const MAX_SCHEMA_DIAGNOSTIC_PATH_SEGMENT_LENGTH = 64; |
| 14 | const MAX_SCHEMA_DIAGNOSTIC_LENGTH = 2_048; |
| 15 | |
| 16 | interface SchemaDiagnosticIssue { |
| 17 | readonly message: string; |
| 18 | readonly path: ReadonlyArray<PropertyKey>; |
| 19 | } |
| 20 | |
| 21 | // Schema's default formatter includes actual values. These diagnostics cross |
| 22 | // process and UI boundaries, so retain only issue kinds and bounded paths. |
| 23 | |
| 24 | function truncateDiagnostic(value: string, maxLength: number): string { |
| 25 | return value.length <= maxLength ? value : `${value.slice(0, maxLength - 3)}...`; |
| 26 | } |
| 27 | |
| 28 | function formatDiagnosticPathSegment(key: PropertyKey): string { |
| 29 | if (typeof key === "number") { |
| 30 | return `[${key}]`; |
| 31 | } |
| 32 | const value = truncateDiagnostic( |
| 33 | typeof key === "symbol" ? String(key) : key, |
| 34 | MAX_SCHEMA_DIAGNOSTIC_PATH_SEGMENT_LENGTH, |
| 35 | ); |
| 36 | return `[${JSON.stringify(value)}]`; |
| 37 | } |
| 38 | |
| 39 | function formatDiagnosticIssue(issue: SchemaDiagnosticIssue): string { |
| 40 | if (issue.path.length === 0) { |
| 41 | return issue.message; |
| 42 | } |
| 43 | const path = issue.path |
| 44 | .slice(0, MAX_SCHEMA_DIAGNOSTIC_PATH_SEGMENTS) |
| 45 | .map(formatDiagnosticPathSegment) |
| 46 | .join(""); |
| 47 | const suffix = issue.path.length > MAX_SCHEMA_DIAGNOSTIC_PATH_SEGMENTS ? "[...]" : ""; |
| 48 | return `${issue.message}\n at ${path}${suffix}`; |
| 49 | } |
| 50 | |
| 51 | function schemaDiagnosticMessage(issue: SchemaIssue.Issue): string { |
| 52 | switch (issue._tag) { |
| 53 | case "InvalidType": |
| 54 | return "Invalid type"; |
| 55 | case "InvalidValue": |
| 56 | case "Filter": |
| 57 | case "AnyOf": |
| 58 | case "Encoding": |
| 59 | case "Pointer": |
| 60 | case "Composite": |
| 61 | return "Invalid value"; |
| 62 | case "MissingKey": |
| 63 | return "Missing key"; |
| 64 | case "UnexpectedKey": |
| 65 | return "Unexpected key"; |
| 66 | case "Forbidden": |
| 67 | return "Forbidden operation"; |
| 68 | case "OneOf": |
| 69 | return "Expected exactly one schema member to match"; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | function collectSchemaDiagnosticIssues( |
| 74 | issue: SchemaIssue.Issue, |
| 75 | path: ReadonlyArray<PropertyKey>, |
| 76 | diagnostics: Array<SchemaDiagnosticIssue>, |
| 77 | ): number { |
| 78 | switch (issue._tag) { |
| 79 | case "Encoding": |
| 80 | return collectSchemaDiagnosticIssues(issue.issue, path, diagnostics); |
| 81 | case "Filter": |
| 82 | if (issue.issue._tag !== "InvalidValue") { |
| 83 | return collectSchemaDiagnosticIssues(issue.issue, path, diagnostics); |
| 84 | } |
| 85 | break; |
| 86 | case "Pointer": |
| 87 | return collectSchemaDiagnosticIssues(issue.issue, [...path, ...issue.path], diagnostics); |
| 88 | case "Composite": |
| 89 | return issue.issues.reduce( |
| 90 | (count, issue) => count + collectSchemaDiagnosticIssues(issue, path, diagnostics), |
| 91 | 0, |
| 92 | ); |
| 93 | case "AnyOf": |
| 94 | if (issue.issues.length > 0) { |
| 95 | return issue.issues.reduce( |
| 96 | (count, issue) => count + collectSchemaDiagnosticIssues(issue, path, diagnostics), |
| 97 | 0, |
| 98 | ); |
| 99 | } |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | if (diagnostics.length < MAX_SCHEMA_DIAGNOSTIC_ISSUES) { |
| 104 | diagnostics.push({ message: schemaDiagnosticMessage(issue), path }); |
| 105 | } |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 11 | 109 | export const decodeJsonResult = <S extends Schema.Codec<unknown, unknown, never, never>>( |
| 12 | 110 | schema: S, |
| 13 | 111 | ) => { |
| ... | ... | @@ -35,10 +133,40 @@ export const decodeUnknownJsonResult = <S extends Schema.Codec<unknown, unknown, |
| 35 | 133 | }; |
| 36 | 134 | |
| 37 | 135 | export const formatSchemaError = (cause: Cause.Cause<Schema.SchemaError>) => { |
| 38 | | const squashed = Cause.squash(cause); |
| 39 | | return Schema.isSchemaError(squashed) |
| 40 | | ? SchemaIssue.makeFormatterDefault()(squashed.issue) |
| 41 | | : Cause.pretty(cause); |
| 136 | const issues: Array<SchemaDiagnosticIssue> = []; |
| 137 | let issueCount = 0; |
| 138 | let failureCount = 0; |
| 139 | let defectCount = 0; |
| 140 | let interruptionCount = 0; |
| 141 | |
| 142 | for (const reason of cause.reasons) { |
| 143 | switch (reason._tag) { |
| 144 | case "Fail": |
| 145 | failureCount += 1; |
| 146 | if (Schema.isSchemaError(reason.error)) { |
| 147 | issueCount += collectSchemaDiagnosticIssues(reason.error.issue, [], issues); |
| 148 | } |
| 149 | break; |
| 150 | case "Die": |
| 151 | defectCount += 1; |
| 152 | break; |
| 153 | case "Interrupt": |
| 154 | interruptionCount += 1; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (issues.length === 0) { |
| 160 | return `Schema validation failed (failureCount=${failureCount}, defectCount=${defectCount}, interruptionCount=${interruptionCount}).`; |
| 161 | } |
| 162 | |
| 163 | const omittedIssueCount = issueCount - issues.length; |
| 164 | const formatted = issues.map(formatDiagnosticIssue).join("\n"); |
| 165 | if (omittedIssueCount === 0) { |
| 166 | return truncateDiagnostic(formatted, MAX_SCHEMA_DIAGNOSTIC_LENGTH); |
| 167 | } |
| 168 | const suffix = `\n... and ${omittedIssueCount} more issue(s)`; |
| 169 | return truncateDiagnostic(formatted, MAX_SCHEMA_DIAGNOSTIC_LENGTH - suffix.length) + suffix; |
| 42 | 170 | }; |
| 43 | 171 | |
| 44 | 172 | /** |
| ... | ... | @@ -67,9 +195,7 @@ const parseLenientJsonGetter = SchemaGetter.onSome((input: string) => { |
| 67 | 195 | |
| 68 | 196 | return decodeJsonString(stripped).pipe( |
| 69 | 197 | Effect.map(Option.some), |
| 70 | | Effect.mapError( |
| 71 | | (error) => new SchemaIssue.InvalidValue(Option.some(input), { message: String(error) }), |
| 72 | | ), |
| 198 | Effect.mapError((error) => error.issue), |
| 73 | 199 | ); |
| 74 | 200 | }); |
| 75 | 201 | |