Skip to content
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 #1

Conversation

fwoelffel
Copy link

Hi @joaoGabriel55 I had a look at colinhacks#2672 and came up with this PR which

  • simplifies the uniqueness implementation (less code to maintain)
  • provides typing for the identifier and message functions
  • adds documentation

Let me know what you think of this, and hopefully we'll see this merged into Zod.

const { unique, duplicateElements } = result;

if (!unique) {
const duplicates = (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIP: That part could be rewritten only for reading purposes, this way:

const output = (ctx.data as this["_output"][]);
const duplicates = (identifier ? output.map(identifier) : output).filter((item, idx, arr) => arr.indexOf(item) !== idx);

Copy link
Owner

@joaoGabriel55 joaoGabriel55 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@joaoGabriel55 joaoGabriel55 merged commit 7bfa15f into joaoGabriel55:validate-array-uniqueness Feb 19, 2024
@fwoelffel fwoelffel deleted the validate-array-uniqueness branch February 27, 2024 08:54
@fwoelffel fwoelffel restored the validate-array-uniqueness branch February 27, 2024 08:54
@fwoelffel fwoelffel deleted the validate-array-uniqueness branch February 27, 2024 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants