diff --git a/lib/modules/platform/github/common.ts b/lib/modules/platform/github/common.ts index fb5f083802db9af47c4952ee49580c428b58480d..7a5748c52b540e1c53f2897d2ee550f09044d0b1 100644 --- a/lib/modules/platform/github/common.ts +++ b/lib/modules/platform/github/common.ts @@ -52,6 +52,6 @@ export function coerceRestPr(pr: GhRestPr): GhPr { result.closedAt = pr.closed_at; } - schema.match(platformSchemas.Pr, result, true); + schema.match(platformSchemas.Pr, result, 'warn'); return result; } diff --git a/lib/util/schema.spec.ts b/lib/util/schema.spec.ts index 590ed4d10b84f809610a42cbf8d0ba22c3ff8502..1b15920c73dcca28257856b3663c25c3bf07d53a 100644 --- a/lib/util/schema.spec.ts +++ b/lib/util/schema.spec.ts @@ -28,42 +28,57 @@ describe('util/schema', () => { expect(logger.logger.warn).not.toHaveBeenCalled(); }); - it('reports nothing if there are no any reports', () => { - schema.reportErrors(); - expect(logger.logger.warn).not.toHaveBeenCalled(); - }); + describe('warn', () => { + it('reports nothing if there are no any reports', () => { + schema.reportErrors(); + expect(logger.logger.warn).not.toHaveBeenCalled(); + }); - it('reports same warning one time', () => { - const testSchema = z.object( - { foo: z.string() }, - { description: 'Some test schema' } - ); - const invalidData = { foo: 42 }; + it('reports same warning one time', () => { + const testSchema = z.object( + { foo: z.string() }, + { description: 'Some test schema' } + ); + const invalidData = { foo: 42 }; - schema.match(testSchema, invalidData, true); - schema.match(testSchema, invalidData, true); - schema.match(testSchema, invalidData, true); - schema.match(testSchema, invalidData, true); - schema.reportErrors(); + schema.match(testSchema, invalidData, 'warn'); + schema.match(testSchema, invalidData, 'warn'); + schema.match(testSchema, invalidData, 'warn'); + schema.match(testSchema, invalidData, 'warn'); + schema.reportErrors(); - expect(logger.logger.warn).toHaveBeenCalledOnce(); - expect(logger.logger.warn.mock.calls[0]).toMatchObject([ - { description: 'Some test schema' }, - 'Schema validation error', - ]); - }); + expect(logger.logger.warn).toHaveBeenCalledOnce(); + expect(logger.logger.warn.mock.calls[0]).toMatchObject([ + { description: 'Some test schema' }, + 'Schema validation error', + ]); + }); - it('reports unspecified schema', () => { - const testSchema = z.object({ foo: z.string() }); - const invalidData = { foo: 42 }; + it('reports unspecified schema', () => { + const testSchema = z.object({ foo: z.string() }); + const invalidData = { foo: 42 }; - schema.match(testSchema, invalidData, true); - schema.reportErrors(); + schema.match(testSchema, invalidData, 'warn'); + schema.reportErrors(); + + expect(logger.logger.warn).toHaveBeenCalledOnce(); + expect(logger.logger.warn.mock.calls[0]).toMatchObject([ + { description: 'Unspecified schema' }, + 'Schema validation error', + ]); + }); + }); + + describe('throw', () => { + it('throws for invalid data', () => { + const testSchema = z.object({ + foo: z.string({ invalid_type_error: 'foobar' }), + }); + const invalidData = { foo: 123 }; - expect(logger.logger.warn).toHaveBeenCalledOnce(); - expect(logger.logger.warn.mock.calls[0]).toMatchObject([ - { description: 'Unspecified schema' }, - 'Schema validation error', - ]); + expect(() => schema.match(testSchema, invalidData, 'throw')).toThrow( + 'foobar' + ); + }); }); }); diff --git a/lib/util/schema.ts b/lib/util/schema.ts index ce00525f94abcbe8982412ffe68b3c500b486237..8e874cb957ed98bca50fc53df68293625378b837 100644 --- a/lib/util/schema.ts +++ b/lib/util/schema.ts @@ -47,15 +47,19 @@ export function reportErrors(): void { export function match<T extends z.ZodSchema>( schema: T, input: unknown, - report = false + onError?: 'warn' | 'throw' ): input is z.infer<T> { const res = schema.safeParse(input); const { success } = res; if (!success) { - if (report) { + if (onError === 'warn') { collectError(schema, res.error); } + if (onError === 'throw') { + throw res.error; + } + return false; }