Skip to content

feat(cli): Improve generated schema definitions - #2783

Merged
daffl merged 2 commits into
dovefrom
cli-schema-tweaks
Oct 8, 2022
Merged

feat(cli): Improve generated schema definitions#2783
daffl merged 2 commits into
dovefrom
cli-schema-tweaks

Conversation

@daffl

@daffl daffl commented Oct 7, 2022

Copy link
Copy Markdown
Member

This pull request improves a few things on top of #2772 - specifically it first creates the main schema with all properties and then derives the data schema from the properties that we want the user to be able to submit. We also do the same thing for query schemas (picking the properties you want to be able to query for). A generated TypeBox schema now looks like this:

import { resolve } from '@feathersjs/schema'
import { Type, querySyntax, getDataValidator, getValidator } from '@feathersjs/typebox'
import type { Static } from '@feathersjs/typebox'

import type { HookContext } from '../../declarations'
import { dataValidator, queryValidator } from '../../schemas/validators'

// Main data model schema
export const messageSchema = Type.Object(
  {
    _id: Type.String(),
    text: Type.String()
  },
  { $id: 'Message', additionalProperties: false }
)
export type Message = Static<typeof messageSchema>
export const messageResolver = resolve<Message, HookContext>({
  properties: {}
})

export const messageExternalResolver = resolve<Message, HookContext>({
  properties: {}
})

// Schema for creating new entries
export const messageDataSchema = Type.Pick(messageSchema, ['text'], {
  $id: 'MessageData',
  additionalProperties: false
})
export type MessageData = Static<typeof messageDataSchema>
export const messageDataValidator = getDataValidator(messageDataSchema, dataValidator)
export const messageDataResolver = resolve<Message, HookContext>({
  properties: {}
})

// Schema for allowed query properties
export const messageQueryProperties = Type.Pick(messageSchema, ['_id', 'text'], {
  additionalProperties: false
})
export const messageQuerySchema = querySyntax(messageQueryProperties)
export type MessageQuery = Static<typeof messageQuerySchema>
export const messageQueryValidator = getValidator(messageQuerySchema, queryValidator)
export const messageQueryResolver = resolve<MessageQuery, HookContext>({
  properties: {}
})

Also fixes the TypeBox dependency separation by providing a TypeBox specific getDataValidator and getValidator.

@daffl
daffl merged commit 474a9fd into dove Oct 8, 2022
@daffl
daffl deleted the cli-schema-tweaks branch October 8, 2022 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant