Skip to content

Commit

Permalink
fix: lint exception (api7#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Feb 29, 2024
1 parent ade3a47 commit eb863f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
34 changes: 26 additions & 8 deletions apps/cli/src/command/lint.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ADCSDK from '@api7/adc-sdk';
import { Listr, ListrTask } from 'listr2';
import pluralize from 'pluralize';
import { ZodError } from 'zod';

import { check } from '../linter';
Expand All @@ -8,20 +9,37 @@ import { BaseCommand } from './helper';

export const LintTask = (): ListrTask<{ local: ADCSDK.Configuration }> => ({
title: 'Lint configuration',
task: (ctx, task) => {
task: (ctx) => {
const result = check(ctx.local);

if (result.success) {
task.output = 'No errors found in configuration';
} else {
if (!result.success) {
let err =
'Lint configuration\nThe following errors were found in configuration:\n';
'error' in result &&
let pathException = false;

if ('error' in result) {
(result.error as ZodError).errors.forEach((error, idx) => {
err += `#${idx + 1} ${error.message} at ${error.path[0]}: "${
ctx.local[error.path[0]][error.path[1]].name
}", field: "${error.path.slice(2, error.path.length).join('.')}"`;
if (error.path.length < 2) {
// special case: not enough information is available to indicate the location of the error
pathException = true;
err += `#${idx + 1} raw error: ${JSON.stringify(error)}\n`;
return;
}

// normal case
const resourceType = pluralize.singular(error.path[0] as string);
const resourceName = ctx.local[error.path[0]][error.path[1]].name;
err += `#${idx + 1} ${
error.message
} at ${resourceType}: "${resourceName}", field: "${(
error.path.slice(2, error.path.length) ?? []
).join('.')}"\n`;
});
}

if (pathException)
err +=
'NOTE: There are some unsummarizable errors in the lint results that are presented as "raw error". You can report such unexpected cases.';

throw new Error(err);
}
Expand Down
6 changes: 4 additions & 2 deletions apps/cli/src/linter/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ export const ConfigurationSchema = z.object({
consumers: z.array(consumerSchema).optional(),
consumer_groups: z.array(consumerGroupSchema).optional(),
stream_routes: z.array(streamRouteSchema).optional(),
global_rules: z.record(z.string(), z.record(z.string(), z.any())),
plugin_metadata: z.record(z.string(), z.record(z.string(), z.any())),
global_rules: z.record(z.string(), z.record(z.string(), z.any())).optional(),
plugin_metadata: z
.record(z.string(), z.record(z.string(), z.any()))
.optional(),
});

/* const res = configurationSchema.safeParse({
Expand Down

0 comments on commit eb863f1

Please sign in to comment.