From 3827a3fb245cf248f8fea1dd1b72f38af3cc89b3 Mon Sep 17 00:00:00 2001 From: Sandrina Pereira Date: Sun, 4 May 2025 15:50:39 +0100 Subject: [PATCH 1/5] fix(next): Field - convert enum to options --- next/src/field/schema.ts | 10 +++++++++- next/test/fields.test.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/next/src/field/schema.ts b/next/src/field/schema.ts index 1d71ea916..c1bd1f6b1 100644 --- a/next/src/field/schema.ts +++ b/next/src/field/schema.ts @@ -160,7 +160,6 @@ function convertToOptions(nodeOptions: JsfSchema[]): Array { * Get field options from schema */ function getFieldOptions(schema: NonBooleanJsfSchema) { - // Handle oneOf or radio input type if (schema.oneOf) { return convertToOptions(schema.oneOf || []) } @@ -175,6 +174,15 @@ function getFieldOptions(schema: NonBooleanJsfSchema) { return convertToOptions(schema.anyOf) } + // Handle enum + if (schema.enum) { + const enumAsOneOf: JsfSchema['oneOf'] = schema.enum?.map(value => ({ + title: typeof value === 'string' ? value : JSON.stringify(value), + const: value, + })) || [] + return convertToOptions(enumAsOneOf) + } + return null } diff --git a/next/test/fields.test.ts b/next/test/fields.test.ts index 11f5b3b6d..0347db6ab 100644 --- a/next/test/fields.test.ts +++ b/next/test/fields.test.ts @@ -283,6 +283,40 @@ describe('fields', () => { ]) }) + it.only('build a radio field with enum', () => { + const schema: JsfSchema = { + type: 'object', + properties: { + status: { + 'enum': ['active', true, false, 1], + 'x-jsf-presentation': { + inputType: 'radio', + }, + }, + }, + } + + const fields = buildFieldSchema(schema, 'root', true)!.fields! + + expect(fields).toEqual([ + { + type: 'radio', + inputType: 'radio', + jsonType: 'text', + isVisible: true, + name: 'status', + required: false, + enum: ['active', true, false, 1], + options: [ + { label: 'active', value: 'active' }, + { label: 'true', value: true }, + { label: 'false', value: false }, + { label: '1', value: 1 }, + ], + }, + ]) + }) + it('skips options without a null const value', () => { const schema: JsfSchema = { type: 'object', From b194ad484f9d84a8ba707b036513249ebb27f70a Mon Sep 17 00:00:00 2001 From: Sandrina Pereira Date: Sun, 4 May 2025 15:54:28 +0100 Subject: [PATCH 2/5] annotate a bug in jsonType for later --- next/test/fields.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/next/test/fields.test.ts b/next/test/fields.test.ts index 0347db6ab..21b018496 100644 --- a/next/test/fields.test.ts +++ b/next/test/fields.test.ts @@ -302,7 +302,7 @@ describe('fields', () => { { type: 'radio', inputType: 'radio', - jsonType: 'text', + jsonType: 'text', // BUG: This is wrong, should be undefined. isVisible: true, name: 'status', required: false, From d92ce50e3c8f4aa08f7915d6e2812582a9cc6b8c Mon Sep 17 00:00:00 2001 From: Sandrina Pereira Date: Sun, 4 May 2025 16:00:35 +0100 Subject: [PATCH 3/5] type title and description --- next/src/types.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/next/src/types.ts b/next/src/types.ts index b081c5839..225dc42e2 100644 --- a/next/src/types.ts +++ b/next/src/types.ts @@ -52,6 +52,8 @@ export interface JsonLogicSchema extends JsonLogicRules, JsonLogicRootSchema {} * JSON Schema Form extending JSON Schema with additional JSON Schema Form properties. */ export type JsfSchema = JSONSchema & { + 'title'?: string + 'description'?: string 'properties'?: Record 'items'?: JsfSchema 'anyOf'?: JsfSchema[] From 149a65d0e52b1d78963295bee843d04974544bed Mon Sep 17 00:00:00 2001 From: Sandrina Pereira Date: Sun, 4 May 2025 16:05:28 +0100 Subject: [PATCH 4/5] revert type, it was already typed --- next/src/types.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/next/src/types.ts b/next/src/types.ts index 225dc42e2..b081c5839 100644 --- a/next/src/types.ts +++ b/next/src/types.ts @@ -52,8 +52,6 @@ export interface JsonLogicSchema extends JsonLogicRules, JsonLogicRootSchema {} * JSON Schema Form extending JSON Schema with additional JSON Schema Form properties. */ export type JsfSchema = JSONSchema & { - 'title'?: string - 'description'?: string 'properties'?: Record 'items'?: JsfSchema 'anyOf'?: JsfSchema[] From 5906a0ec0f58e765b8afd3a423bd7bf9c37daff2 Mon Sep 17 00:00:00 2001 From: Sandrina Pereira Date: Sun, 4 May 2025 16:08:50 +0100 Subject: [PATCH 5/5] remove it.only --- next/test/fields.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/next/test/fields.test.ts b/next/test/fields.test.ts index 21b018496..b899e985c 100644 --- a/next/test/fields.test.ts +++ b/next/test/fields.test.ts @@ -283,7 +283,7 @@ describe('fields', () => { ]) }) - it.only('build a radio field with enum', () => { + it('build a radio field with enum', () => { const schema: JsfSchema = { type: 'object', properties: {