Skip to content

Color Picker: Validate uniqueness of selected colors#20431

Merged
AndyButland merged 8 commits intoumbraco:mainfrom
Programeerik:v16/bugfix/20359-color-picker-validate-unique
Oct 9, 2025
Merged

Color Picker: Validate uniqueness of selected colors#20431
AndyButland merged 8 commits intoumbraco:mainfrom
Programeerik:v16/bugfix/20359-color-picker-validate-unique

Conversation

@Programeerik
Copy link
Contributor

Prerequisites

  • I have added steps to test this contribution in the description below

If there's an existing issue for this PR then this fixes #20359

Description

I've added an check on duplicates after the valid color check, so it's only checked when it's an valid color.
I've added an function normalize to the internal ColorListValidator class so the logic is much better readable.
The normalize function is for removing the # before checking for duplicate values.
When the count of duplicates is higher than 0 it's not possible to save the data.

image

I've also added an unittest which check if the test returns the duplicate value

@github-actions
Copy link

github-actions bot commented Oct 9, 2025

Hi there @Programeerik, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@AndyButland AndyButland changed the base branch from contrib to main October 9, 2025 04:48
Copilot AI review requested due to automatic review settings October 9, 2025 05:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds validation to prevent duplicate color values in the Color Picker property editor configuration. The validation normalizes color values (converting 3-digit hex to 6-digit format and ensuring case-insensitive comparison) before checking for duplicates.

Key changes:

  • Added duplicate color validation logic with normalization for proper comparison
  • Modernized the validator implementation using source-generated regex and collection expressions
  • Added comprehensive unit tests to verify the duplicate validation functionality

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Umbraco.Infrastructure/PropertyEditors/ColorPickerConfigurationEditor.cs Added duplicate validation logic, normalization method, and modernized code with source-generated regex
tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/ColorListValidatorTest.cs Added unit test to verify duplicate color validation works correctly

Copy link
Contributor

@AndyButland AndyButland left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution @Programeerik. I tested out and found an issue via the unit test (so that was very valuable to add), which I've fixed up and just done a little tidy up of the file since we are editing it.

Please let me know if all looks good to you and we can merge this in.

Moving forward I'd like to see better surfacing of this validation in the backoffice itself (which works well for content, but less so for data type configuration), but having this improved check on the server-side is worth having anyway.

@AndyButland AndyButland changed the title V16/bugfix/20359 color picker validate unique Color Picker: Validate uniqueness of selected colors Oct 9, 2025
@Programeerik
Copy link
Contributor Author

@AndyButland Great point about the continue addition. When you talk about the validation in the backoffice, do you mean so that the user get realtime feedback?

The serverside code check can be merged. Thanks for the additions and help.

@AndyButland
Copy link
Contributor

When you talk about the validation in the backoffice, do you mean so that the user get realtime feedback?

Could be real-time, but more that we better surface the server-side validation when managing data type configurations.

@AndyButland AndyButland merged commit 767894b into umbraco:main Oct 9, 2025
25 checks passed
@bjarnef
Copy link
Contributor

bjarnef commented Oct 9, 2025

@AndyButland should we consider some of these changes in ValueListUniqueValueValidator (of course in a new PR):

var duplicateValues = items
.Select(item => item)
.GroupBy(v => v)
.Where(group => group.Count() > 1)
.Select(group => group.First())
.ToArray();

It seems .Select(item => item) is redundant - and I think HashSet may perform better.

@AndyButland
Copy link
Contributor

Looks like it, certainly the Select is redundant but can't see it does any harm. As you say, I don't think it would be part of this PR (which I've merged now anyway!),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Color picker doesn't validate for unique color codes

3 participants