Comments (2)
@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)
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.
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)
- `decorate` unexpectedly overwrites decorators everywhere HOT 1
- Eden Treat types break when using absolute import paths
- Eden treaty swallows errors HOT 1
- CORS not working correctly when aot is false
- Typing error when using `onError` with `derive` HOT 3
- requestIP return null when using sign cookie
- `ctx.query` doesn't work in some cases with `aot: true` HOT 2
- `aot` does not recognize the use of `ctx.body` within a `try catch` HOT 1
- rate limit feature or plugin !? HOT 1
- no `base64` in `t.String()` `format` option. Also no type support for the `byte` option.
- t.Literal type is ignored in favor of string during validation
- Eden Treaty ignores t.Transform()
- "default" option is not being applied on validation HOT 4
- Ability to create and remove websocket endpoints at runtime.
- Macro modify schema
- Rxjs pipe syntax
- Nullable field for types as documented in OpenAPI 3.0
- Support for classes and getter fields
- Transform type (include Date) won't transform in Array schema
- Decorates are replacing each other HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elysia.