-
Couldn't load subscription status.
- Fork 0
Return value validators with doc helpers #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: sshader-return-value-validators
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { Validator, v } from 'convex/values' | ||
| import { Doc, TableNames } from '../_generated/dataModel' | ||
| import schema from '../schema' | ||
|
|
||
| export const betterV = { | ||
| id: <T extends TableNames>(tableName: T) => { | ||
| return v.id(tableName) | ||
| }, | ||
| doc: <T extends TableNames>(tableName: T): Validator<Doc<T>, false, any> => { | ||
| const validator = v.object( | ||
| (schema.tables[tableName] as any).documentSchema | ||
| ) as Validator<any, any, any> | ||
| if (validator?.kind !== 'object') { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm realizing now that this isn't right -- it should account for a union of objects as well as any (and probably record, once that exists) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how does this pattern of being able to work directly with some validators if you know their |
||
| throw new Error(`Not an object validator`) | ||
| } | ||
| return v.object({ | ||
| ...validator.fields, | ||
| _id: v.id(tableName), | ||
| _creationTime: v.number(), | ||
| }) | ||
| }, | ||
| mergeObjects: < | ||
| O1 extends Record<string, any>, | ||
| O2 extends Record<string, any> | ||
| >( | ||
| validator1: Validator<O1, false, any>, | ||
| validator2: Validator<O2, false, any> | ||
| ): Validator<Omit<O1, keyof O2> & O2, false, any> => { | ||
| if (validator1.kind !== 'object' || validator2.kind !== 'object') { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted something like a spread. Zod seems to call this This also probably should allow for unions of objects, and maybe also any + record There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we expose all the validator types so you can use them here? |
||
| throw new Error('Not object validators') | ||
| } | ||
| return v.object({ | ||
| ...validator1.fields, | ||
| ...validator2.fields, | ||
| }) as any | ||
| }, | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made normal Convex schema expose the validator as
documentType. Ents have a privatedocumentSchema, but I think we should ideally make it behave likedocumentTypewith vanilla Convex