-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Validate array uniqueness #2672
Validate array uniqueness #2672
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
What suggestions do you have for array of complex objects uniqueness? I have one that would be make the objects elements from array flatten to simplifies the comparison between. |
Class-validator seems to be doing a simple comparison |
@colinhacks could you take a look, when you have a free time? |
@joaoGabriel55 As discussed here, #2316 (reply in thread), would it be possible to add an options which would display duplicate items? |
yeah! |
@colinhacks any thoughts about this? |
deno/lib/types.ts
Outdated
@@ -1975,6 +1975,11 @@ export interface ZodArrayDef<T extends ZodTypeAny = ZodTypeAny> | |||
exactLength: { value: number; message?: string } | null; | |||
minLength: { value: number; message?: string } | null; | |||
maxLength: { value: number; message?: string } | null; | |||
uniqueness: { | |||
identifier?: ArrayUniqueIdentifier; | |||
message?: string | ((duplicateElements: any) => string); |
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.
message?: string | ((duplicateElements: any) => string); | |
message?: string | ((duplicateElements: T[]) => string); |
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 tried this, but some types of errors like this:
Type '{ [k in keyof T]: ZodOptional<T[k]>; }' does not satisfy the constraint 'ZodRawShape'.
Type 'ZodOptional<T[k]>' is not assignable to type 'ZodTypeAny'.
The types returned by 'refine(...).refine(...).optional()' are incompatible between these types.
Type 'ZodOptional<ZodEffects<ZodEffects<ZodOptional<T[k]>, any, T[k]["_input"] | undefined>, any, T[k]["_input"] | undefined>>' is not assignable to type 'ZodOptional<ZodEffects<ZodEffects<ZodType<any, any, any>, any, any>, any, any>>'.
Type 'ZodEffects<ZodType<any, any, any>, any, any>' is not assignable to type 'ZodEffects<ZodOptional<T[k]>, any, T[k]["_input"] | undefined>'.
Type 'Options[number]' does not satisfy the constraint 'ZodType<any, any, any>'.ts(2344)
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'll try to have a look at this during the weekend ;)
? params.message | ||
: errorUtil.toString(params.message); | ||
: errorUtil.toString(params?.message); |
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.
: errorUtil.toString(params?.message); | |
: errorUtil.toString(params.message); |
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 need to put this ?
because the params can be not passed (undefined
) on the .unique
method.
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.
My bad
Thanks! I like this a lot, especially |
148dfbd
to
68d83b7
Compare
* feat: implement primitive array uniqueness * chore: update yarn.lock * feat: implement array of complex objects uniqueness * chore: rollback yarn.lock * refactor: minor update _arrayUnique * chore: minor change yarn.lock * feat: implement custom message support * feat: add showDuplicates param * chore: TS types update * chore: TS types II * refactor: ArrayMessageFunction * refactor: simplify array uniqueness implementation * docs: add documentation for array uniqueness * Regen lock * Fix readme --------- Co-authored-by: Frederic Woelffel <[email protected]> Co-authored-by: Colin McDonnell <[email protected]>
Cool, but I do not get why the most idiomatic approach, z.array(z.string()).unique(), is not supported ? |
Maybe I'm mistaken, but |
From discussion: #2316
TODO
Thanks for the help: @ramos-ph and @fwoelffel