Skip to content

Commit

Permalink
improve logging of schema validation errors (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-tate authored Sep 24, 2024
1 parent dca2d04 commit 1494219
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/smooth-clouds-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@jpmorganchase/mosaic-schemas': patch
---

- switch the default behaviour of schema validation to exit on error
- improve logging of schema validation errors to prevent silent failures
3 changes: 2 additions & 1 deletion packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"lint": "eslint --ignore-pattern \"**/__tests__/**\""
},
"dependencies": {
"zod": "^3.22.3"
"zod": "^3.22.3",
"zod-validation-error": "^3.4.0"
}
}
16 changes: 10 additions & 6 deletions packages/schemas/src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { z, ZodError, ZodTypeAny } from 'zod';
import { z, ZodTypeAny } from 'zod';
import { fromZodError } from 'zod-validation-error';

// eslint-disable-next-line import/prefer-default-export
export function validateMosaicSchema<T extends ZodTypeAny>(
schema: T,
options: Record<string, unknown>,
exitOnError = false
exitOnError = true
): z.infer<typeof schema> {
try {
return schema.parse(options);
} catch (err: unknown) {
console.group('[Mosaic] schema validation error');
console.table((err as ZodError).issues);
console.groupEnd();
} catch (err: any) {
if (err instanceof z.ZodError) {
const formattedError = fromZodError(err);
console.error(`[Mosaic] schema validation error - ${formattedError.message}`);
} else {
console.error('[Mosaic] schema validation error - an unexpected error occurred:', err);
}
if (exitOnError) {
process.exit(1);
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13639,6 +13639,11 @@ zenscroll@^4.0.2:
resolved "https://registry.yarnpkg.com/zenscroll/-/zenscroll-4.0.2.tgz#e8d5774d1c0738a47bcfa8729f3712e2deddeb25"
integrity sha512-jEA1znR7b4C/NnaycInCU6h/d15ZzCd1jmsruqOKnZP6WXQSMH3W2GL+OXbkruslU4h+Tzuos0HdswzRUk/Vgg==

zod-validation-error@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/zod-validation-error/-/zod-validation-error-3.4.0.tgz#3a8a1f55c65579822d7faa190b51336c61bee2a6"
integrity sha512-ZOPR9SVY6Pb2qqO5XHt+MkkTRxGXb4EVtnjc9JpXUOtUB1T9Ru7mZOT361AN3MsetVe7R0a1KZshJDZdgp9miQ==

zod@^3.22.3:
version "3.22.3"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.3.tgz#2fbc96118b174290d94e8896371c95629e87a060"
Expand Down

0 comments on commit 1494219

Please sign in to comment.