You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to migrate to ArkType from Zod, but I have these very nice generic function for creating schemas that allow me to dynamically create schemas based on keys of objects. I want to convert these to ArkType but I'm having some trouble converting the generics, since I need to add validations like min length/defaults to the generics and it doesn't seem that simple. Would be great if anyone could help out!
exportinterfaceCursorPaginationParams<TSortKeyextendsstring>extendsBasePaginationParams<TSortKey>{// This will be a serialised string of all the cursors based on sortingcursor?: string;}exportconstcreateCursorPaginationParamsSchema=<TSortKeySchemaextendsz.ZodType<string>>(sortKeySchema: TSortKeySchema,defaultSortBy: SortItem<TSortKeySchema["_type"]>[],)=>// We need at least one sort item so we can derive a primary cursor for paginationcreateBasePaginationParamsSchema(sortKeySchema,1,defaultSortBy).merge(z.object({cursor: z.string().optional(),}),);
Then finally example usage where I have full type-safety over the keys, and it knows to autocomplete to choose one of the keys for the argument in the function:
I'm not sure how to work with these more complex nested type functions in ArkType, and would be great if someone could help me out here, thanks!
PS: Some other questions I have whilst I'm migrating...
How do I have generics that can let me merge schemas? export const createAzureEntitySchema = <TEntity extends CompositeKeyEntity>(schema: type.Any<TEntity>) => schema.merge(itemMetadataSchema) the merge function doesn't exist unless it's explicitly "object" type which loses the generic
An extension of 1 is if it's possible to extend an existing schema's property with more restrictions, perhaps using pipe..?
I've tried super hard here with the default, but for some reason, it doesn't really let me do any defaults at all which is annoying and also defaults don't return just the type for me to infer unlike zod, so I'm also not sure what the best practice is for inferring things with defaults..
The error I get is roughly
Argument of type '() => SortItem<TSortKey>[]' is not assignable to parameter of type 'defaultFor<finalizeDistillation<{ key: TSortKey; order: SortOrder; }[], { [k in keyof ({ [k in keyof { key: TSortKey; order: SortOrder; } as k extends (unwrapInput<TSortKey> extends Default<...> ? [...] extends [...] ? never : "key" : never) ? never : k]: _distill<...>; } & { [k in unwrapInput<...> extends Default<....'.
This is also an issue with compatibility for TRPC, since I want to be able to omit certain parameters and provide defaults for them, so I don't need to pass them when calling TRPC, and zod allows this, but the types returned by arktype when using .default() is completely incompatible
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys!
I'm trying to migrate to ArkType from Zod, but I have these very nice generic function for creating schemas that allow me to dynamically create schemas based on keys of objects. I want to convert these to ArkType but I'm having some trouble converting the generics, since I need to add validations like min length/defaults to the generics and it doesn't seem that simple. Would be great if anyone could help out!
Example:
Then finally example usage where I have full type-safety over the keys, and it knows to autocomplete to choose one of the keys for the argument in the function:
I'm not sure how to work with these more complex nested type functions in ArkType, and would be great if someone could help me out here, thanks!
PS: Some other questions I have whilst I'm migrating...
How do I have generics that can let me merge schemas?
export const createAzureEntitySchema = <TEntity extends CompositeKeyEntity>(schema: type.Any<TEntity>) => schema.merge(itemMetadataSchema)
the merge function doesn't exist unless it's explicitly "object" type which loses the genericAn extension of 1 is if it's possible to extend an existing schema's property with more restrictions, perhaps using pipe..?
or
I've tried super hard here with the default, but for some reason, it doesn't really let me do any defaults at all which is annoying and also defaults don't return just the type for me to infer unlike zod, so I'm also not sure what the best practice is for inferring things with defaults..
The error I get is roughly
This is also an issue with compatibility for TRPC, since I want to be able to omit certain parameters and provide defaults for them, so I don't need to pass them when calling TRPC, and zod allows this, but the types returned by arktype when using .default() is completely incompatible
Beta Was this translation helpful? Give feedback.
All reactions