Replies: 1 comment 1 reply
-
If I understand you correctly, you want to attach some custom metadata to a property? If so, I've had success with a setup something like this: import t from 'typebox'
type PropMetadata = {
/** Declares whether this property is unique across all other records. */
unique?: boolean
}
/** A helper for creating `PropMetadata` in a type-safe way. */
const propMetadata = (metadata: PropMetadata): { 'x-metadata': PropMetadata } => {
return { 'x-metadata': metadata }
}
const hasPropMetadata = (
obj: Record<string, any>,
): obj is { [key: PropertyKey]: unknown; 'x-metadata': PropMetadata } => obj['x-metadata'] != null
// Example of writing custom properties to schema
export const MY_SCHEMA = t.Object({
name: t.String({ ...propMetadata({ unique: true }) })
})
// Example of reading custom properties off of a schema
const getPropMetadata = (obj: Record<string, any>): PropMetadata | undefined =>
hasPropMetadata(obj) ? obj['x-metadata'] : undefined |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Looking for a suggestion, creating a minimalist lib for Mongodb's native driver, looking for better types as to the why.
There are some cases where we'd like to pass along some custom properties that aren't really part of the validation Schema but required for example to map a relationship.
Looking for any suggestions anyone might have.
Beta Was this translation helpful? Give feedback.
All reactions