diff --git a/lib/modules/platform/github/common.ts b/lib/modules/platform/github/common.ts index 9a4853d3c2790527f0d6fe1b3b8058770f83b2fd..fb5f083802db9af47c4952ee49580c428b58480d 100644 --- a/lib/modules/platform/github/common.ts +++ b/lib/modules/platform/github/common.ts @@ -1,6 +1,6 @@ import is from '@sindresorhus/is'; import { PrState } from '../../../types'; -import { checkSchema } from '../../../util/schema'; +import * as schema from '../../../util/schema'; import { getPrBodyStruct } from '../pr-body'; import * as platformSchemas from '../schemas'; import type { GhPr, GhRestPr } from './types'; @@ -52,6 +52,6 @@ export function coerceRestPr(pr: GhRestPr): GhPr { result.closedAt = pr.closed_at; } - checkSchema(platformSchemas.Pr, result); + schema.match(platformSchemas.Pr, result, true); return result; } diff --git a/lib/util/schema.spec.ts b/lib/util/schema.spec.ts index 608b8dadc239460e8061359c65beca4177784298..590ed4d10b84f809610a42cbf8d0ba22c3ff8502 100644 --- a/lib/util/schema.spec.ts +++ b/lib/util/schema.spec.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; import { logger } from '../../test/util'; import * as memCache from './cache/memory'; -import { checkSchema, reportErrors } from './schema'; +import * as schema from './schema'; describe('util/schema', () => { beforeEach(() => { @@ -10,33 +10,41 @@ describe('util/schema', () => { }); it('validates data', () => { - const schema = z.object({ foo: z.string() }); + const testSchema = z.object({ foo: z.string() }); const validData = { foo: 'bar' }; - const res = checkSchema(schema, validData); + const res = schema.match(testSchema, validData); expect(res).toBeTrue(); + }); + + it('returns false for invalid data', () => { + const testSchema = z.object({ foo: z.string() }); + const invalidData = { foo: 123 }; - reportErrors(); - expect(logger.logger.warn).not.toHaveBeenCalledOnce(); + const res = schema.match(testSchema, invalidData); + expect(res).toBeFalse(); + + schema.reportErrors(); + expect(logger.logger.warn).not.toHaveBeenCalled(); }); it('reports nothing if there are no any reports', () => { - reportErrors(); + schema.reportErrors(); expect(logger.logger.warn).not.toHaveBeenCalled(); }); - it('reports same warning once', () => { - const schema = z.object( + it('reports same warning one time', () => { + const testSchema = z.object( { foo: z.string() }, { description: 'Some test schema' } ); const invalidData = { foo: 42 }; - checkSchema(schema, invalidData); - checkSchema(schema, invalidData); - checkSchema(schema, invalidData); - checkSchema(schema, invalidData); - reportErrors(); + schema.match(testSchema, invalidData, true); + schema.match(testSchema, invalidData, true); + schema.match(testSchema, invalidData, true); + schema.match(testSchema, invalidData, true); + schema.reportErrors(); expect(logger.logger.warn).toHaveBeenCalledOnce(); expect(logger.logger.warn.mock.calls[0]).toMatchObject([ @@ -46,11 +54,11 @@ describe('util/schema', () => { }); it('reports unspecified schema', () => { - const schema = z.object({ foo: z.string() }); + const testSchema = z.object({ foo: z.string() }); const invalidData = { foo: 42 }; - checkSchema(schema, invalidData); - reportErrors(); + schema.match(testSchema, invalidData, true); + schema.reportErrors(); expect(logger.logger.warn).toHaveBeenCalledOnce(); expect(logger.logger.warn.mock.calls[0]).toMatchObject([ diff --git a/lib/util/schema.ts b/lib/util/schema.ts index 22e91073793c5ec88b542e50067b62aa62fdb00d..ce00525f94abcbe8982412ffe68b3c500b486237 100644 --- a/lib/util/schema.ts +++ b/lib/util/schema.ts @@ -44,14 +44,18 @@ export function reportErrors(): void { memCache.set('schema-errors', null); } -export function checkSchema<T extends z.ZodSchema>( +export function match<T extends z.ZodSchema>( schema: T, - input: unknown + input: unknown, + report = false ): input is z.infer<T> { const res = schema.safeParse(input); const { success } = res; if (!success) { - collectError(schema, res.error); + if (report) { + collectError(schema, res.error); + } + return false; }