Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

fix(v1): generate unions of options list values #295

Open
wants to merge 2 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/core/src/transform-schema-to-structure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,46 @@ describe('transformSchemaToStructure', () => {
},
});
});

it('works with string with a predefined list of options', () => {
const normalizedSchema = schemaNormalizer({
name: 'default',
types: [
{
type: 'document',
name: 'book',
fields: [
{
name: 'genre',
type: 'string',
options: { list: ['fiction', 'thriller'] },
},
],
},
],
});

const structure = transformSchemaToStructure({ normalizedSchema });

expect(structure).toMatchObject({
type: 'Array',
of: {
type: 'Object',
properties: [
{ key: '_type', value: { type: 'String', value: 'book' } },
{ key: '_id', value: { type: 'String' } },
{
key: 'genre',
value: {
type: 'Or',
children: [
{ type: 'String', value: 'thriller' },
{ type: 'String', value: 'fiction' },
],
},
},
],
},
});
});
});
19 changes: 15 additions & 4 deletions packages/core/src/transform-schema-to-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,22 @@ export function transformSchemaNodeToStructure({
case 'String':
case 'Text':
case 'Url': {
const list = 'list' in node && node.list?.length ? node.list : null;
return createStructure({
type: 'String',
canBeNull: false,
canBeOptional: !node.codegen.required,
value: null,
type: 'Or',
children: (
list ??
// treat non list fields as lists of 1 item with a null value
// so it's handled aswell as a union of string
([{ value: null }] as const)
).map((item) =>
createStructure({
type: 'String',
canBeNull: false,
canBeOptional: !node.codegen.required,
value: item.value,
}),
),
});
}
case 'Object':
Expand Down