Coder Social home page Coder Social logo

Comments (2)

sinclairzx81 avatar sinclairzx81 commented on June 12, 2024 3

@ethndotsh Hi, sorry, I had removed the previous snippet but meant to re-post after I had a deeper look at how Elysia was handling type registration. Here is the snippet again (slightly modified to support deriving the type keyword)

TypeScript Link Here

import { Kind, Static, TSchema, SchemaOptions, TypeRegistry } from '@sinclair/typebox'

// -------------------------------------------------------------------------------------
// TypeRegistry
// -------------------------------------------------------------------------------------
TypeRegistry.Set<TUnionEnum>('UnionEnum', (schema, value) => {
  return (typeof value === 'string' || typeof value === 'number') && schema.enum.includes(value as never)
})
// -------------------------------------------------------------------------------------
// TUnionEnum
// -------------------------------------------------------------------------------------
export type TUnionValue = string | number

export interface TUnionEnum<T extends TUnionValue[] = []> extends TSchema {
  [Kind]: 'UnionEnum'
  static: T[number]
  enum: T
}
// -------------------------------------------------------------------------------------
// UnionEnum
// -------------------------------------------------------------------------------------
/** `[Elysia]` Creates a Union type with a enum array schema representation  */
export function UnionEnum<T extends TUnionValue[]>(values: [...T], options: SchemaOptions = {}) {
  const type = (
    values.every(value => typeof value === 'string') ? { type: 'string' } :
    values.every(value => typeof value === 'number') ? { type: 'number' } :
    {} // mixed string | number
  )
  return { ...options, [Kind]: 'UnionEnum', ...type, enum: values } as TUnionEnum<T>
}

// -------------------------------------------------------------------------------------
// Usage
// -------------------------------------------------------------------------------------

const T = UnionEnum(['A', 'B', 'C']) // const T = { type: 'string', enum: ['A', 'B', 'C'] }

type T = Static<typeof T>            // type T = 'A' | 'B' | 'C'

It should be possible to include the above as a module in your project; with Elysia able to use it so long as the type is registered. I had noted that there is potential here to include the type on Elysia t for the benefit of documentation (as quite a few OpenAPI / Swagger users ask for this enum representation specifically for documentation generation)

The above should provide a good reference point :)
Hope this helps!
S

from elysia.

ethndotsh avatar ethndotsh commented on June 12, 2024

Could've sworn there was a solution here from the author of Typebox. Am I losing it or was it deleted?

from elysia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.