Skip to content

feature(symfony): Add CSS Color Schema Restriction for Property Validation #7215

Merged
soyuka merged 6 commits into
api-platform:mainfrom
SebLevDev:feature/property-schema-css-color-restriction
Jun 27, 2025
Merged

feature(symfony): Add CSS Color Schema Restriction for Property Validation #7215
soyuka merged 6 commits into
api-platform:mainfrom
SebLevDev:feature/property-schema-css-color-restriction

Conversation

@SebLevDev

@SebLevDev SebLevDev commented Jun 13, 2025

Copy link
Copy Markdown
Contributor

Description

This PR introduces the PropertySchemaCssColorRestriction class, which generates a regular expression (pattern) for validating CSS color values in API Platform property schemas, based on the Symfony CssColor constraint.

Technical Details

Supports multiple CSS color formats (hex, rgb, rgba, hsl, hsla, named colors, etc.).
Dynamically builds a single regex pattern combining all allowed formats, suitable for OpenAPI/JSON Schema documentation.

Example

With a given resource Theme

#[ApiResource]
class Theme
{
    private ?int $id = null;

    #[Assert\NotNull]
    #[Assert\CssColor(
        formats: Assert\CssColor::HEX_LONG,
    )]
    public ?string $backgroundColor = null;
}

The impact in the OpenApi Schema provide

{
  "Theme": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer",
        "readOnly": true
      },
      "backgroundColor": {
        "type": "string",
        "pattern": "^#(?:[0-9a-f]{6})$",
      }
    },
    "required": ["backgroundColor"]
  }
}

@SebLevDev
SebLevDev force-pushed the feature/property-schema-css-color-restriction branch from 7581d75 to 89a2157 Compare June 14, 2025 09:00
@soyuka

soyuka commented Jun 26, 2025

Copy link
Copy Markdown
Member

looks nice, could you maybe explain how this is used? (or open a doc PR)

It'd be nice to add some tests (can also be functional testing)

@SebLevDev

Copy link
Copy Markdown
Contributor Author

I've updated the PR description to include an example of how to use the constraint in an entity.
I've also added a unit test to cover the PropertySchemaCssColorRestriction logic.
Let me know if anything else is needed!

@soyuka
soyuka merged commit 4abf17b into api-platform:main Jun 27, 2025
110 of 114 checks passed
@soyuka

soyuka commented Jun 27, 2025

Copy link
Copy Markdown
Member

awesome thanks!

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