Skip to content

Commit

Permalink
fix(type-safe-api): silence warnings for mock data generation for fie…
Browse files Browse the repository at this point in the history
…lds with pattern constraints (#892)

ReRegExp is used to generate mock data for fields with regex patterns. ReRegExp is quite noisy with
its warnings, printing directly to the console. This change silences these warnings.

Fixes #891
  • Loading branch information
cogwirrel authored Dec 9, 2024
1 parent 88bcd62 commit 08e607b
Showing 1 changed file with 4 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,18 @@ export const generateMockDataForSchema = (

const generateStringFromRegex = (pattern: string): string | undefined => {
const random = Math.random;
const warn = console.warn;
try {
// Fix random to ensure mocked data is deterministic
Math.random = () => 0.5;
// ReRegExp prints warnings to the console which add unnecessary noise
console.warn = () => {};
return new ReRegExp(pattern).build();
} catch {
// Couldn't convert regex to string, don't fail, just be less strict
} finally {
Math.random = random;
console.warn = warn;
}
return undefined;
}
Expand Down

0 comments on commit 08e607b

Please sign in to comment.