Skip to content

Commit f21d4dc

Browse files
committedNov 9, 2023
feat(schema): add group helper to build schema
1 parent 8d3ac2d commit f21d4dc

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed
 

Diff for: ‎playground/nuxt.schema.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { field } from '../src/theme'
1+
import { field, group } from '../src/theme'
22

33
export default defineNuxtSchema({
44
appConfig: {
5-
someConfig: field({ type: 'string', default: 'schema default' }),
6-
configFromNuxtSchema: field('boolean', true)
5+
parent: group({
6+
title: 'Parent',
7+
description: 'Parent description',
8+
fields: {
9+
someConfig: field({ type: 'string', default: 'schema default' }),
10+
configFromNuxtSchema: field({ type: 'boolean' })
11+
}
12+
})
713
}
814
})

Diff for: ‎src/theme.ts

+33-18
Original file line numberDiff line numberDiff line change
@@ -148,39 +148,54 @@ const supportedFields: { [key in InputsTypes]: Schema } = {
148148
export type StudioFieldData =
149149
PartialSchema &
150150
{
151-
type: keyof typeof supportedFields
151+
type?: keyof typeof supportedFields
152152
icon?: string
153+
fields?: { [key: string]: InputValue }
153154
}
154155

155156
/**
156-
* Declares a Nuxt Studio compatible configuration field.
157+
* Helper to build aNuxt Studio compatible configuration schema.
157158
* Supports all type of fields provided by Nuxt Studio and all fields supported from Untyped Schema interface.
158159
*/
159-
export function field (
160-
type: keyof typeof supportedFields | StudioFieldData,
161-
defaultValue?: any
162-
): InputValue {
163-
// Custom `type` field should get overwritten by native Schema ones at this stage
164-
const result = defu(
165-
supportedFields[typeof type === 'string' ? type : type.type],
166-
type
167-
) as StudioFieldData
160+
export function field (schema: StudioFieldData): InputValue {
161+
if (!schema.type) {
162+
throw new Error(`Missing type in schema ${JSON.stringify(schema)}`)
163+
}
168164

169-
// Init tags
170-
if (!result.tags) { result.tags = [] }
165+
// copy of supportedFields
166+
const base = JSON.parse(JSON.stringify(supportedFields[schema.type]))
167+
const result = defu(base, schema)
171168

172-
// Cast `icon` into its tag
169+
if (!result.tags) {
170+
result.tags = []
171+
}
173172
if (result.icon) {
174173
result.tags.push(`@studioIcon ${result.icon}`)
175174
delete result.icon
176175
}
176+
return {
177+
$schema: result
178+
}
179+
}
180+
181+
export function group (schema: StudioFieldData): InputValue {
182+
const result = { ...schema }
183+
184+
if (result.icon) {
185+
result.tags = [`@studioIcon ${result.icon}`]
186+
delete result.icon
187+
}
177188

178-
// Cast default value passed as 2nd parameter
179-
if (defaultValue) {
180-
result.default = defaultValue
189+
const fields: Record<string, InputValue> = {}
190+
if (result.fields) {
191+
for (const key of Object.keys(result.fields)) {
192+
fields[key] = result.fields[key]
193+
}
194+
delete result.fields
181195
}
182196

183197
return {
184-
$schema: result
198+
$schema: result,
199+
...fields
185200
}
186201
}

0 commit comments

Comments
 (0)
Please sign in to comment.