Skip to content
Snippets Groups Projects
Unverified Commit eb5181e2 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor: Make invalid schema reporting optional (#18024)

parent d0027cf2
No related branches found
No related tags found
No related merge requests found
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;
}
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([
......
......@@ -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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment