Skip to content

Commit

Permalink
fix: render enum as string from schema ref form
Browse files Browse the repository at this point in the history
* fix: render enum as string from schema ref form
  • Loading branch information
blackmann authored Dec 21, 2023
1 parent dfd781d commit 30e5e16
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 52 deletions.
37 changes: 3 additions & 34 deletions admin/src/components/collection-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import Select from './select'
import app from '../mangobase-app'
import { appendIndexFields } from '@/lib/append-index-fields'
import appendSchemaFields from '@/lib/append-schema-fields'
import { definitionFromField } from '@/lib/definition-from-field'
import getNewFieldName from '@/lib/get-new-field-name'
import { loadCollections } from '@/data/collections'
import { schemaFromFields } from '@/lib/schema-from-fields'

type ValidatedIndex = Index & { existing?: boolean; removed?: boolean }

Expand Down Expand Up @@ -215,7 +217,7 @@ function CollectionForm({ collection, onHide }: Props) {
.map(({ existing, removed, ...index }) => index), // existing and removed don't go to backend
migrationSteps,
name,
schema: schemaFromForm(fields),
schema: schemaFromFields(fields),
template: options.includes('is-template'),
}

Expand Down Expand Up @@ -592,38 +594,5 @@ function prepareMigrations(
return migrationSteps
}

function schemaFromForm(fields: FieldProps[]) {
const schema: Record<string, any> = {}
for (const field of fields) {
if (field.removed) {
continue
}

const [name, definition] = definitionFromField(field)

if (definition.type === 'string') {
if (definition.enum) {
if (definition.enum.length === 0) {
// remove empty enum list
definition.enum = undefined
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
definition.enum = definition.enum.map((it) => it.text)
}
}
}

schema[name] = definition
}

return schema
}

function definitionFromField(field: FieldProps) {
const { name, existing, removed, ...definition } = field
return [name, definition] as const
}

export default CollectionForm
export type { FieldProps }
8 changes: 8 additions & 0 deletions admin/src/lib/definition-from-field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FieldProps } from '@/components/collection-form'

function definitionFromField(field: FieldProps) {
const { name, existing, removed, ...definition } = field
return [name, definition] as const
}

export { definitionFromField }
32 changes: 32 additions & 0 deletions admin/src/lib/schema-from-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { FieldProps } from '@/components/collection-form'
import { definitionFromField } from './definition-from-field'

function schemaFromFields(fields: FieldProps[]) {
const schema: Record<string, any> = {}
for (const field of fields) {
if (field.removed) {
continue
}

const [name, definition] = definitionFromField(field)

if (definition.type === 'string') {
if (definition.enum) {
if (definition.enum.length === 0) {
// remove empty enum list
definition.enum = undefined
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
definition.enum = definition.enum.map((it) => it.text)
}
}
}

schema[name] = definition
}

return schema
}

export { schemaFromFields }
11 changes: 3 additions & 8 deletions admin/src/pages/settings/schemas/[name].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import {
useNavigate,
useParams,
} from 'react-router-dom'
import { Ref, SchemaDefinitions } from 'mangobase'
import Button from '@/components/button'
import Chip from '@/components/chip'
import Field from '@/components/field'
import { FieldProps } from '@/components/collection-form'
import Input from '@/components/input'
import React from 'preact/compat'
import { Ref } from 'mangobase'
import app from '../../../mangobase-app'
import appendSchemaFields from '@/lib/append-schema-fields'
import getNewFieldName from '@/lib/get-new-field-name'
import { getSchema } from '@/lib/get-schema'
import { loadSchemaRefs } from '../../../data/schema-refs'
import removeFieldsItem from '@/lib/remove-fields-item'
import { schemaFromFields } from '@/lib/schema-from-fields'

function Component() {
const formMethods = useForm()
Expand All @@ -48,15 +49,9 @@ function Component() {
// [ ] schema name shouldn't start with 'collections/'. This is reserved
// for schema related to template collections
async function submit(data: FieldValues) {
const schema: SchemaDefinitions = {}

for (const { name, ...definition } of data.fields) {
schema[name] = definition
}

const refData = {
name: data.name,
schema,
schema: schemaFromFields(data.fields),
}

isNew
Expand Down
6 changes: 1 addition & 5 deletions base/src/lib/export-schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
Definition,
SchemaDefinitions,
DefinitionType,
} from '../schema.js'
import type { Definition, SchemaDefinitions } from '../schema.js'

interface TypescriptOptions {
language: 'typescript'
Expand Down
19 changes: 14 additions & 5 deletions base/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,11 +782,20 @@ class Schema {
)
}

if (definition.enum && definition.enum.length === 0) {
throw new ValidationError(
fieldPath,
'`enum` should have at least one value'
)
if (definition.enum) {
if (definition.enum.length === 0) {
throw new ValidationError(
fieldPath,
'`enum` should have at least one value'
)
}

if (!definition.enum.every((item) => typeof item === 'string')) {
throw new ValidationError(
fieldPath,
'`enum` should be an array of strings'
)
}
}

break
Expand Down

0 comments on commit 30e5e16

Please sign in to comment.