Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): stop the schema editor re-presenting a typed field as enum (PRD-7649) (#3204)

serafin-garciad06f121e2990
diff --git a/.changeset/schema-editor-enum-no-type-conversion.md b/.changeset/schema-editor-enum-no-type-conversion.md
new file mode 100644
index 000000000..b975e83e6
--- /dev/null
+++ b/.changeset/schema-editor-enum-no-type-conversion.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+The frontmatter schema editor no longer re-presents a typed field as an enum when you give it allowed values. Typing into a `string` field's allowed-values box added the values correctly but flipped the field's Type select to `enum`, which read as a silent type change — and switching it back to `string` then deleted the values you had just entered. Allowed values are a constraint, not a type: a field keeps showing its declared type, and the `enum` option now shows only for a field the schema leaves untyped (a bare `{"enum": [...]}`), or while you are explicitly picking it. Picking `enum` no longer writes a `string` type in, and moving a field to `string` keeps its allowed values instead of clearing them. Any other target type still clears them, because it could never satisfy them — a string vocabulary left on a `number` field makes that field reject every possible value. The same rules apply to an array field's element type.
diff --git a/packages/app/src/components/settings/frontmatter-schema-field-editor.dom.test.tsx b/packages/app/src/components/settings/frontmatter-schema-field-editor.dom.test.tsx
index b0a87975c..fe3f0a484 100644
--- a/packages/app/src/components/settings/frontmatter-schema-field-editor.dom.test.tsx
+++ b/packages/app/src/components/settings/frontmatter-schema-field-editor.dom.test.tsx
@@ -6,8 +6,20 @@
*/
import { cleanup, fireEvent, render, screen, within } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
import { beforeEach, describe, expect, test, vi } from 'vitest';
+// Radix Select reaches for pointer-capture and scroll APIs the jsdom preload
+// doesn't expose; same shims the sibling DOM tests hoist.
+const ElementProto = Element.prototype as Element & {
+ hasPointerCapture?: () => boolean;
+ releasePointerCapture?: () => void;
+ scrollIntoView?: () => void;
+};
+ElementProto.hasPointerCapture ??= () => false;
+ElementProto.releasePointerCapture ??= () => {};
+ElementProto.scrollIntoView ??= () => {};
+
const globalWithDomShims = globalThis as { ResizeObserver?: unknown };
if (globalWithDomShims.ResizeObserver === undefined) {
class NoopResizeObserver {
@@ -191,12 +203,173 @@ describe('FrontmatterSchemaFieldEditor', () => {
expect(writes).toEqual([[FILE, 'status', { description: 'Lifecycle state' }, []]]);
});
- test('a field with enum values presents as the enum pseudo-type', () => {
+ test('an untyped field with enum values presents as the enum pseudo-type', () => {
render(<FrontmatterSchemaFieldEditor file={FILE} />);
const row = within(screen.getByTestId('frontmatter-field-row-status'));
expect(row.getByTestId('frontmatter-field-type-status').textContent).toContain('enum');
});
+ test('entering an allowed value on a typed field writes only that constraint', () => {
+ mockLintData = lintDataWithSchema({
+ type: 'object',
+ properties: { owner: { type: 'string' } },
+ });
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ const row = within(screen.getByTestId('frontmatter-field-row-owner'));
+ const input = row.getByLabelText('Allowed values (empty = any)');
+ fireEvent.change(input, { target: { value: 'ana' } });
+ fireEvent.keyDown(input, { key: 'Enter' });
+ expect(writes).toEqual([[FILE, 'owner', { enum: ['ana'] }, []]]);
+ });
+
+ test('a typed field carrying allowed values keeps presenting as its declared type', () => {
+ mockLintData = lintDataWithSchema({
+ type: 'object',
+ properties: {
+ owner: { type: 'string', enum: ['ana', 'bo'] },
+ tags: { type: 'array', items: { type: 'string', enum: ['a', 'b'] } },
+ },
+ });
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ const type = screen.getByTestId('frontmatter-field-type-owner');
+ expect(type.textContent).toContain('string');
+ expect(type.textContent).not.toContain('enum');
+ // The values are still shown — they just don't restate the field's type.
+ expect(screen.getByText('ana')).toBeTruthy();
+
+ const itemsType = screen.getByTestId('frontmatter-field-items-type-tags');
+ expect(itemsType.textContent).toContain('string');
+ expect(itemsType.textContent).not.toContain('enum');
+ expect(screen.getByText('a')).toBeTruthy();
+ });
+
+ test('picking the enum pseudo-type presents it without writing a type', async () => {
+ mockLintData = lintDataWithSchema({ type: 'object', properties: { owner: {} } });
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ const type = screen.getByTestId('frontmatter-field-type-owner');
+ await userEvent.click(type);
+ await userEvent.click(await screen.findByRole('option', { name: 'enum' }));
+ expect(writes).toEqual([]);
+ expect(type.textContent).toContain('enum');
+ });
+
+ test('picking the enum pseudo-type on a typed field presents it without writing', async () => {
+ mockLintData = lintDataWithSchema({
+ type: 'object',
+ properties: { owner: { type: 'string' } },
+ });
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ const type = screen.getByTestId('frontmatter-field-type-owner');
+ expect(type.textContent).toContain('string');
+ await userEvent.click(type);
+ await userEvent.click(await screen.findByRole('option', { name: 'enum' }));
+ // A declared type must not suppress the pick: the select follows the user
+ // here, and the schema keeps the type it already had.
+ expect(writes).toEqual([]);
+ expect(type.textContent).toContain('enum');
+ });
+
+ // The round trip, which neither leg pins alone: a pick that leaves the enum
+ // presentation must drop the intent as well. Without that, one enum pick
+ // would strand the row on `enum` for the rest of the session no matter what
+ // type the user chose next.
+ test('picking a scalar after enum drops the intent, not just the values', async () => {
+ mockLintData = lintDataWithSchema({
+ type: 'object',
+ properties: { owner: { type: 'string', enum: ['a'] } },
+ });
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ const type = screen.getByTestId('frontmatter-field-type-owner');
+ await userEvent.click(type);
+ await userEvent.click(await screen.findByRole('option', { name: 'enum' }));
+ expect(type.textContent).toContain('enum');
+
+ await userEvent.click(type);
+ await userEvent.click(await screen.findByRole('option', { name: 'number' }));
+ expect(writes).toEqual([[FILE, 'owner', { type: 'number', enum: null }, []]]);
+ // The write client is mocked, so the row still reads the schema's declared
+ // `string` — what matters is that it stopped reading `enum`.
+ expect(type.textContent).not.toContain('enum');
+ expect(type.textContent).toContain('string');
+ });
+
+ test('picking a scalar element type after enum drops the items intent', async () => {
+ mockLintData = lintDataWithSchema({
+ type: 'object',
+ properties: { tags: { type: 'array', items: { type: 'string', enum: ['a'] } } },
+ });
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ const itemsType = screen.getByTestId('frontmatter-field-items-type-tags');
+ await userEvent.click(itemsType);
+ await userEvent.click(await screen.findByRole('option', { name: 'enum' }));
+ expect(itemsType.textContent).toContain('enum');
+
+ await userEvent.click(itemsType);
+ await userEvent.click(await screen.findByRole('option', { name: 'number' }));
+ expect(writes).toEqual([[FILE, 'tags', { itemsType: 'number', itemsEnum: null }, []]]);
+ expect(itemsType.textContent).not.toContain('enum');
+ expect(itemsType.textContent).toContain('string');
+ });
+
+ test('leaving the enum presentation for a scalar type keeps the allowed values', async () => {
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ await userEvent.click(screen.getByTestId('frontmatter-field-type-status'));
+ await userEvent.click(await screen.findByRole('option', { name: 'string' }));
+ expect(writes).toEqual([[FILE, 'status', { type: 'string' }, []]]);
+ });
+
+ // `string` is the only target that can still hold string values. Every other
+ // type would keep a vocabulary it can never satisfy — the validator compiles
+ // `{type: 'number', enum: ['draft']}` and then rejects every value.
+ test.each([
+ 'number',
+ 'boolean',
+ 'array',
+ 'object',
+ ])('switching to %s clears values that type could never satisfy', async (target) => {
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ await userEvent.click(screen.getByTestId('frontmatter-field-type-status'));
+ await userEvent.click(await screen.findByRole('option', { name: target }));
+ expect(writes).toEqual([[FILE, 'status', { type: target, enum: null }, []]]);
+ });
+
+ test.each([
+ 'number',
+ 'boolean',
+ 'object',
+ ])('switching the element type to %s clears the element values', async (target) => {
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ await userEvent.click(screen.getByTestId('frontmatter-field-items-type-tags'));
+ await userEvent.click(await screen.findByRole('option', { name: target }));
+ expect(writes).toEqual([[FILE, 'tags', { itemsType: target, itemsEnum: null }, []]]);
+ });
+
+ test('entering an allowed element value on a typed array writes only that constraint', () => {
+ mockLintData = lintDataWithSchema({
+ type: 'object',
+ properties: { tags: { type: 'array', items: { type: 'string' } } },
+ });
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ const row = within(screen.getByTestId('frontmatter-field-row-tags'));
+ const input = row.getByLabelText('Allowed element values (empty = any)');
+ fireEvent.change(input, { target: { value: 'a' } });
+ fireEvent.keyDown(input, { key: 'Enter' });
+ expect(writes).toEqual([[FILE, 'tags', { itemsEnum: ['a'] }, []]]);
+ });
+
+ test('picking the enum element type presents it without writing a type', async () => {
+ mockLintData = lintDataWithSchema({
+ type: 'object',
+ properties: { tags: { type: 'array', items: {} } },
+ });
+ render(<FrontmatterSchemaFieldEditor file={FILE} />);
+ const itemsType = screen.getByTestId('frontmatter-field-items-type-tags');
+ await userEvent.click(itemsType);
+ await userEvent.click(await screen.findByRole('option', { name: 'enum' }));
+ expect(writes).toEqual([]);
+ expect(itemsType.textContent).toContain('enum');
+ });
+
test('object fields render nested child rows; nested edits carry the parent path', () => {
mockLintData = lintDataWithSchema({
type: 'object',
diff --git a/packages/app/src/components/settings/frontmatter-schema-field-editor.tsx b/packages/app/src/components/settings/frontmatter-schema-field-editor.tsx
index c3b9a37bd..3b3f4d04c 100644
--- a/packages/app/src/components/settings/frontmatter-schema-field-editor.tsx
+++ b/packages/app/src/components/settings/frontmatter-schema-field-editor.tsx
@@ -52,9 +52,11 @@ type FieldType = (typeof FIELD_TYPES)[number];
/**
* The type select adds `enum` as pure UI sugar for discoverability (matching
- * the agents-manage-ui builder): picking it defaults the field to `string` and
- * points the user at the allowed-values input. On disk `enum` stays what JSON
- * Schema says it is — a constraint orthogonal to `type`.
+ * the agents-manage-ui builder): picking it points the user at the
+ * allowed-values input without writing anything, since on disk `enum` stays
+ * what JSON Schema says it is — a constraint orthogonal to `type`. So the
+ * select reads back `enum` only for a field the schema leaves untyped;
+ * anything with a declared type presents as that type, allowed values or not.
*/
const TYPE_SELECT_OPTIONS = ['string', 'number', 'boolean', 'enum', 'array', 'object'] as const;
@@ -280,9 +282,9 @@ function FieldRow({
ops: FieldOps;
}) {
const { t } = useLingui();
- // The user picked the `enum` pseudo-type but hasn't entered values yet —
- // keeps the select from snapping back to the concrete type until the first
- // allowed value persists.
+ // The user picked the `enum` pseudo-type on a field the schema already types
+ // (or types nothing yet) — presentation only, since `enum` is a constraint
+ // rather than a type and picking it must not rewrite what was declared.
const [enumIntent, setEnumIntent] = useState(false);
// Same buffer for the items-type select's enum choice.
const [itemsEnumIntent, setItemsEnumIntent] = useState(false);
@@ -298,8 +300,16 @@ function FieldRow({
const pattern = typeof property.pattern === 'string' ? property.pattern : '';
const description = typeof property.description === 'string' ? property.description : '';
const preserved = hasUnmodeledKeywords(property);
+ // A declared type always wins: allowed values on a `string` field are a
+ // constraint on that string, so entering them must not re-present the field
+ // as an enum. The pseudo-type shows for a field the schema leaves untyped —
+ // a bare `{enum: [...]}`, which is what "an enum" actually is on disk — or
+ // while the user is picking it for one.
const showAsEnum =
- type !== 'array' && enumValues !== null && (enumValues.length > 0 || enumIntent);
+ type !== 'array' &&
+ type !== 'object' &&
+ enumValues !== null &&
+ (enumIntent || (type === undefined && enumValues.length > 0));
const showChildren = type === 'object' && depth < MAX_NESTING_DEPTH;
const itemsType =
typeof items.type === 'string' &&
@@ -309,7 +319,7 @@ function FieldRow({
const showItemsAsEnum =
itemsType !== 'object' &&
itemsEnumValues !== null &&
- (itemsEnumValues.length > 0 || itemsEnumIntent);
+ (itemsEnumIntent || (itemsType === undefined && itemsEnumValues.length > 0));
const showItemChildren = type === 'array' && itemsType === 'object' && depth < MAX_NESTING_DEPTH;
return (
@@ -383,16 +393,22 @@ function FieldRow({
value={showAsEnum ? 'enum' : (type ?? '')}
onValueChange={(next) => {
if (next === 'enum') {
+ // Nothing to write: `enum` is not a type, and defaulting one
+ // in is the conversion this select must not make.
setEnumIntent(true);
- if (type === undefined) ops.save(field, { type: 'string' }, parentPath);
return;
}
setEnumIntent(false);
- // Leaving the enum presentation for a concrete type also clears
- // the values — the constraint is what made the field an "enum",
- // so keeping it would snap the select right back.
+ // Allowed values survive only a move to `string` — the one
+ // target that can still hold them, and the one this editor
+ // exists to stop discarding. Every other type would keep a
+ // vocabulary it can never satisfy: `{type: 'number', enum:
+ // ['draft']}` compiles (the validator runs non-strict) and then
+ // rejects every possible value, while the property panel keeps
+ // offering those strings because it reads `enum` without
+ // consulting `type`.
const constraint: FrontmatterFieldConstraint =
- showAsEnum && (enumValues?.length ?? 0) > 0
+ next !== 'string' && (enumValues?.length ?? 0) > 0
? { type: next as FieldType, enum: null }
: { type: next as FieldType };
ops.save(field, constraint, parentPath);
@@ -452,12 +468,13 @@ function FieldRow({
onValueChange={(next) => {
if (next === 'enum') {
setItemsEnumIntent(true);
- if (itemsType === undefined) ops.save(field, { itemsType: 'string' }, parentPath);
return;
}
setItemsEnumIntent(false);
+ // Same rule one level down: only `string` elements can still
+ // hold the values, so every other element type clears them.
const constraint: FrontmatterFieldConstraint =
- showItemsAsEnum && (itemsEnumValues?.length ?? 0) > 0
+ next !== 'string' && (itemsEnumValues?.length ?? 0) > 0
? { itemsType: next as ItemsType, itemsEnum: null }
: { itemsType: next as ItemsType };
ops.save(field, constraint, parentPath);