Skip to content

The open alternative to Salesforce, designed for AI.

fix(cli): surface metadata validation errors in command output (#29078)

Etienne07b30a2fb273
diff --git a/packages/twenty-cli/src/output/__tests__/create-output.spec.ts b/packages/twenty-cli/src/output/__tests__/create-output.spec.ts
index 0a1986b378d..a1e48e5bcbd 100644
--- a/packages/twenty-cli/src/output/__tests__/create-output.spec.ts
+++ b/packages/twenty-cli/src/output/__tests__/create-output.spec.ts
@@ -222,6 +222,72 @@ describe('terminal progress', () => {
expect(process.exitCode).toBe(error.exitCode);
});
+ it('lists metadata validation errors under the failure line in human mode', () => {
+ const output = createOutput({ mode: 'human', command: 'app apply' });
+
+ output.fail(
+ new CliError({
+ code: 'GRAPHQL_ERROR',
+ message: 'Validation failed for 2 fields',
+ details: {
+ validationErrors: [
+ {
+ metadataName: 'fieldMetadata',
+ entityLabel: 'invalidField',
+ message: 'Field name "123" is invalid',
+ },
+ {
+ metadataName: 'fieldMetadata',
+ message: 'A field with this name already exists',
+ },
+ ],
+ },
+ }),
+ );
+
+ expect(stderr).toContain('Validation failed for 2 fields');
+ expect(stderr).toContain(
+ '• fieldMetadata invalidField: Field name "123" is invalid',
+ );
+ expect(stderr).toContain(
+ '• fieldMetadata: A field with this name already exists',
+ );
+ });
+
+ it('includes the validation details in the JSON error envelope', () => {
+ const output = createOutput({ mode: 'json', command: 'app apply' });
+
+ output.fail(
+ new CliError({
+ code: 'GRAPHQL_ERROR',
+ message: 'Validation failed for 1 field',
+ details: {
+ validationErrors: [
+ {
+ metadataName: 'fieldMetadata',
+ message: 'Field name "123" is invalid',
+ },
+ ],
+ },
+ }),
+ );
+
+ expect(JSON.parse(stdout)).toMatchObject({
+ ok: false,
+ error: {
+ code: 'GRAPHQL_ERROR',
+ details: {
+ validationErrors: [
+ {
+ metadataName: 'fieldMetadata',
+ message: 'Field name "123" is invalid',
+ },
+ ],
+ },
+ },
+ });
+ });
+
it('keeps JSON diagnostics plain and emits one result envelope', () => {
const output = createOutput({ mode: 'json', command: 'data export' });
diff --git a/packages/twenty-cli/src/output/create-output.ts b/packages/twenty-cli/src/output/create-output.ts
index 333440d029f..ad453750a4a 100644
--- a/packages/twenty-cli/src/output/create-output.ts
+++ b/packages/twenty-cli/src/output/create-output.ts
@@ -3,6 +3,8 @@ import { isDefined } from 'twenty-shared/utils';
import { createProgressOutput } from '@/output/create-progress-output';
import { JSON_SCHEMA_VERSION } from '@/output/constants/json-schema-version.constant';
+import { formatValidationErrorLines } from '@/output/format-validation-error-lines';
+import { readValidationErrorDetails } from '@/output/read-validation-error-details';
import { formatFailureLine, formatWarningLine } from '@/output/style';
import { type CliWarning } from '@/output/types/cli-warning.type';
import { type OutputMode } from '@/output/types/output-mode.type';
@@ -159,6 +161,15 @@ export const createOutput = ({
text: formatFailureLine(error.message),
});
+ const validationErrors = readValidationErrorDetails(error.details);
+
+ if (validationErrors.length > 0) {
+ writeLine({
+ stream: process.stderr,
+ text: indentLines(formatValidationErrorLines(validationErrors)),
+ });
+ }
+
if (isDefined(error.hint)) {
writeLine({ stream: process.stderr, text: indentLines(error.hint) });
}
diff --git a/packages/twenty-cli/src/output/format-validation-error-lines.ts b/packages/twenty-cli/src/output/format-validation-error-lines.ts
new file mode 100644
index 00000000000..f07d437d43b
--- /dev/null
+++ b/packages/twenty-cli/src/output/format-validation-error-lines.ts
@@ -0,0 +1,13 @@
+import { isDefined } from 'twenty-shared/utils';
+
+import { type ValidationErrorDetail } from '@/output/types/validation-error-detail.type';
+
+export const formatValidationErrorLines = (
+ details: ValidationErrorDetail[],
+): string =>
+ details
+ .map(
+ ({ metadataName, entityLabel, message }) =>
+ `• ${metadataName}${isDefined(entityLabel) ? ` ${entityLabel}` : ''}: ${message}`,
+ )
+ .join('\n');
diff --git a/packages/twenty-cli/src/output/read-validation-error-details.ts b/packages/twenty-cli/src/output/read-validation-error-details.ts
new file mode 100644
index 00000000000..a7361883e45
--- /dev/null
+++ b/packages/twenty-cli/src/output/read-validation-error-details.ts
@@ -0,0 +1,19 @@
+import { isArray, isString } from '@sniptt/guards';
+import { isDefined, isPlainObject } from 'twenty-shared/utils';
+
+import { type ValidationErrorDetail } from '@/output/types/validation-error-detail.type';
+
+export const readValidationErrorDetails = (
+ details: Record<string, unknown> | undefined,
+): ValidationErrorDetail[] => {
+ if (!isDefined(details) || !isArray(details.validationErrors)) {
+ return [];
+ }
+
+ return details.validationErrors.filter(
+ (detail): detail is ValidationErrorDetail =>
+ isPlainObject(detail) &&
+ isString(detail.metadataName) &&
+ isString(detail.message),
+ );
+};
diff --git a/packages/twenty-cli/src/output/types/validation-error-detail.type.ts b/packages/twenty-cli/src/output/types/validation-error-detail.type.ts
new file mode 100644
index 00000000000..0cb51addf23
--- /dev/null
+++ b/packages/twenty-cli/src/output/types/validation-error-detail.type.ts
@@ -0,0 +1,5 @@
+export type ValidationErrorDetail = {
+ metadataName: string;
+ entityLabel?: string;
+ message: string;
+};
diff --git a/packages/twenty-cli/src/transport/graphql/__tests__/create-graphql-error.spec.ts b/packages/twenty-cli/src/transport/graphql/__tests__/create-graphql-error.spec.ts
new file mode 100644
index 00000000000..c8d12184916
--- /dev/null
+++ b/packages/twenty-cli/src/transport/graphql/__tests__/create-graphql-error.spec.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it } from 'vitest';
+
+import { createGraphqlError } from '@/transport/graphql/create-graphql-error';
+import { type ResolvedTarget } from '@/target/types/resolved-target.type';
+
+const target: ResolvedTarget = {
+ apiUrl: 'https://app.twenty.com',
+ bearerToken: 'test-token',
+ credentialKind: 'apiKey',
+ source: 'environment',
+};
+
+const createError = (errors: unknown[], status = 200) =>
+ createGraphqlError({
+ errors,
+ data: null,
+ status,
+ headers: new Headers(),
+ target,
+ });
+
+describe('createGraphqlError', () => {
+ it('surfaces metadata validation detail and the generic summary', () => {
+ const error = createError([
+ {
+ message: 'Workspace migration builder failed',
+ extensions: {
+ code: 'METADATA_VALIDATION_FAILED',
+ message: 'Validation failed for 1 field',
+ errors: {
+ fieldMetadata: [
+ {
+ metadataName: 'fieldMetadata',
+ errors: [
+ {
+ code: 'INVALID_NAME',
+ message: 'Field name "123" is invalid',
+ },
+ ],
+ flatEntityMinimalInformation: { nameSingular: 'invalidField' },
+ },
+ ],
+ },
+ },
+ },
+ ]);
+
+ expect(error.code).toBe('GRAPHQL_ERROR');
+ expect(error.message).toBe('Validation failed for 1 field');
+ expect(error.details?.validationErrors).toEqual([
+ {
+ metadataName: 'fieldMetadata',
+ entityLabel: 'invalidField',
+ message: 'Field name "123" is invalid',
+ },
+ ]);
+ });
+
+ it('keeps the first message and omits validationErrors for a plain error', () => {
+ const error = createError([{ message: 'Something broke' }]);
+
+ expect(error.message).toBe('Something broke');
+ expect(error.details?.validationErrors).toBeUndefined();
+ });
+});
diff --git a/packages/twenty-cli/src/transport/graphql/__tests__/extract-metadata-validation.spec.ts b/packages/twenty-cli/src/transport/graphql/__tests__/extract-metadata-validation.spec.ts
new file mode 100644
index 00000000000..f74bac741b6
--- /dev/null
+++ b/packages/twenty-cli/src/transport/graphql/__tests__/extract-metadata-validation.spec.ts
@@ -0,0 +1,134 @@
+import { describe, expect, it } from 'vitest';
+
+import { extractMetadataValidation } from '@/transport/graphql/extract-metadata-validation';
+
+const metadataValidationError = {
+ message: 'Workspace migration builder failed',
+ extensions: {
+ code: 'METADATA_VALIDATION_FAILED',
+ message: 'Validation failed for 2 fields',
+ summary: { totalErrors: 2, fieldMetadata: 2 },
+ errors: {
+ fieldMetadata: [
+ {
+ type: 'create_field',
+ metadataName: 'fieldMetadata',
+ errors: [
+ { code: 'INVALID_NAME', message: 'Field name "123" is invalid' },
+ ],
+ flatEntityMinimalInformation: {
+ name: '123',
+ nameSingular: 'invalidField',
+ },
+ },
+ {
+ type: 'create_field',
+ metadataName: 'fieldMetadata',
+ errors: [
+ {
+ code: 'DUPLICATE',
+ message: 'A field with this name already exists',
+ },
+ ],
+ flatEntityMinimalInformation: { name: 'status' },
+ },
+ ],
+ },
+ },
+};
+
+describe('extractMetadataValidation', () => {
+ it('flattens the per-entity breakdown and keeps the generic summary', () => {
+ expect(extractMetadataValidation([metadataValidationError])).toEqual({
+ summary: 'Validation failed for 2 fields',
+ errors: [
+ {
+ metadataName: 'fieldMetadata',
+ entityLabel: 'invalidField',
+ message: 'Field name "123" is invalid',
+ },
+ {
+ metadataName: 'fieldMetadata',
+ entityLabel: 'status',
+ message: 'A field with this name already exists',
+ },
+ ],
+ });
+ });
+
+ it('returns no errors for a plain GraphQL error', () => {
+ expect(
+ extractMetadataValidation([
+ { message: 'Boom', extensions: { code: 'GRAPHQL_ERROR' } },
+ ]),
+ ).toEqual({ errors: [] });
+ });
+
+ it('ignores malformed entries without throwing', () => {
+ expect(
+ extractMetadataValidation([
+ null,
+ 'nope',
+ {
+ extensions: {
+ code: 'METADATA_VALIDATION_FAILED',
+ errors: { fieldMetadata: 'not-an-array' },
+ },
+ },
+ {
+ extensions: {
+ code: 'METADATA_VALIDATION_FAILED',
+ errors: {
+ fieldMetadata: [
+ {
+ errors: [{ message: 42 }],
+ flatEntityMinimalInformation: { name: 'status' },
+ },
+ ],
+ },
+ },
+ },
+ ]),
+ ).toEqual({ errors: [] });
+ });
+
+ it('ignores a breakdown without the validation-failed code', () => {
+ expect(
+ extractMetadataValidation([
+ {
+ extensions: {
+ code: 'GRAPHQL_ERROR',
+ message: 'Something else',
+ errors: {
+ fieldMetadata: [
+ {
+ errors: [{ code: 'INVALID_NAME', message: 'nope' }],
+ flatEntityMinimalInformation: { name: 'status' },
+ },
+ ],
+ },
+ },
+ },
+ ]),
+ ).toEqual({ errors: [] });
+ });
+
+ it('caps the breakdown at 100 entries', () => {
+ const failures = Array.from({ length: 60 }, (_, index) => ({
+ metadataName: 'fieldMetadata',
+ errors: [{ code: 'INVALID', message: `error ${index}` }],
+ flatEntityMinimalInformation: { name: `field-${index}` },
+ }));
+
+ const { errors } = extractMetadataValidation([
+ {
+ extensions: {
+ code: 'METADATA_VALIDATION_FAILED',
+ errors: { fieldMetadata: failures, objectMetadata: failures },
+ },
+ },
+ ]);
+
+ expect(errors).toHaveLength(100);
+ });
+});
diff --git a/packages/twenty-cli/src/transport/graphql/create-graphql-error.ts b/packages/twenty-cli/src/transport/graphql/create-graphql-error.ts
index 4ad280c381b..c424a1ba30b 100644
--- a/packages/twenty-cli/src/transport/graphql/create-graphql-error.ts
+++ b/packages/twenty-cli/src/transport/graphql/create-graphql-error.ts
@@ -5,6 +5,7 @@ import { CliError } from '@/output/cli-error';
import { EXIT_CODE } from '@/output/constants/exit-code.constant';
import { getAuthenticationHint } from '@/target/get-authentication-hint';
import { type ResolvedTarget } from '@/target/types/resolved-target.type';
+import { extractMetadataValidation } from '@/transport/graphql/extract-metadata-validation';
import { pickSafeResponseHeaders } from '@/transport/pick-safe-response-headers';
const toPublicGraphqlError = (error: unknown) => {
@@ -43,11 +44,15 @@ export const createGraphqlError = ({
}) => {
const publicErrors = errors.map(toPublicGraphqlError);
const errorCodes = publicErrors.map((error) => error.code);
+ const validation = extractMetadataValidation(errors);
const details = {
status,
headers: pickSafeResponseHeaders(headers),
errors: publicErrors,
data: data ?? null,
+ ...(validation.errors.length > 0
+ ? { validationErrors: validation.errors }
+ : {}),
};
if (status === 401 || errorCodes.includes('UNAUTHENTICATED')) {
@@ -71,7 +76,7 @@ export const createGraphqlError = ({
return new CliError({
code: 'GRAPHQL_ERROR',
- message: summarizeMessages(publicErrors),
+ message: validation.summary ?? summarizeMessages(publicErrors),
details,
});
};
diff --git a/packages/twenty-cli/src/transport/graphql/extract-metadata-validation.ts b/packages/twenty-cli/src/transport/graphql/extract-metadata-validation.ts
new file mode 100644
index 00000000000..97398d7667e
--- /dev/null
+++ b/packages/twenty-cli/src/transport/graphql/extract-metadata-validation.ts
@@ -0,0 +1,122 @@
+import { isArray, isNonEmptyString, isString } from '@sniptt/guards';
+import { type FailedMetadataValidation } from 'twenty-shared/metadata';
+import { isDefined, isPlainObject } from 'twenty-shared/utils';
+
+import { type ValidationErrorDetail } from '@/output/types/validation-error-detail.type';
+
+const MAX_VALIDATION_ERRORS = 100;
+const METADATA_VALIDATION_FAILED_CODE = 'METADATA_VALIDATION_FAILED';
+
+const ENTITY_LABEL_KEYS = [
+ 'nameSingular',
+ 'namePlural',
+ 'name',
+ 'label',
+ 'universalIdentifier',
+ 'id',
+] as const;
+
+const readEntityLabel = (
+ entityInformation: FailedMetadataValidation['flatEntityMinimalInformation'],
+): string | undefined => {
+ for (const key of ENTITY_LABEL_KEYS) {
+ const value = entityInformation[key];
+
+ if (isNonEmptyString(value)) {
+ return value;
+ }
+ }
+
+ return undefined;
+};
+
+const isFailedMetadataValidation = (
+ value: unknown,
+): value is FailedMetadataValidation =>
+ isPlainObject(value) &&
+ isArray(value.errors) &&
+ isPlainObject(value.flatEntityMinimalInformation);
+
+const collectDetails = ({
+ errorsByMetadata,
+ limit,
+}: {
+ errorsByMetadata: Record<string, unknown>;
+ limit: number;
+}): ValidationErrorDetail[] => {
+ const details: ValidationErrorDetail[] = [];
+
+ for (const [metadataName, failures] of Object.entries(errorsByMetadata)) {
+ if (!isArray(failures)) {
+ continue;
+ }
+
+ for (const failure of failures) {
+ if (!isFailedMetadataValidation(failure)) {
+ continue;
+ }
+
+ const entityLabel = readEntityLabel(failure.flatEntityMinimalInformation);
+
+ for (const error of failure.errors) {
+ if (!isPlainObject(error) || !isString(error.message)) {
+ continue;
+ }
+
+ details.push({
+ metadataName,
+ ...(isDefined(entityLabel) ? { entityLabel } : {}),
+ message: error.message,
+ });
+
+ if (details.length >= limit) {
+ return details;
+ }
+ }
+ }
+ }
+
+ return details;
+};
+
+export const extractMetadataValidation = (
+ graphqlErrors: unknown[],
+): { summary?: string; errors: ValidationErrorDetail[] } => {
+ const errors: ValidationErrorDetail[] = [];
+ let summary: string | undefined;
+
+ for (const graphqlError of graphqlErrors) {
+ if (
+ !isPlainObject(graphqlError) ||
+ !isPlainObject(graphqlError.extensions) ||
+ graphqlError.extensions.code !== METADATA_VALIDATION_FAILED_CODE ||
+ !isPlainObject(graphqlError.extensions.errors)
+ ) {
+ continue;
+ }
+
+ const extracted = collectDetails({
+ errorsByMetadata: graphqlError.extensions.errors,
+ limit: MAX_VALIDATION_ERRORS - errors.length,
+ });
+
+ if (extracted.length === 0) {
+ continue;
+ }
+
+ if (!isDefined(summary) && isString(graphqlError.extensions.message)) {
+ summary = graphqlError.extensions.message;
+ }
+
+ errors.push(...extracted);
+
+ if (errors.length >= MAX_VALIDATION_ERRORS) {
+ break;
+ }
+ }
+
+ return {
+ ...(isDefined(summary) ? { summary } : {}),
+ errors,
+ };
+};