Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Validator] Add new Schema validation constraint #58560

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from

Conversation

WedgeSama
Copy link
Contributor

@WedgeSama WedgeSama commented Oct 14, 2024

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT
Doc PR symfony/symfony-docs#20323

This constraint is like an extension of Json and Yaml constraints. It checks if given value is valid for the given format, then, if you declare constraints option, it will try to apply those constraints to the deserialized data.

Usage examples:

// src/Entity/Report.php
namespace App\Entity;

use App\Yaml\MyConfiguration;
use Symfony\Component\Validator\Constraints as Assert;

class Report
{
    // Only validate format (like Json or Yaml constraints).
    #[Assert\Schema(
        format: 'yaml',
    )]
    private string $customConfiguration;

    // Same in short.
    #[Assert\Schema('yaml')]
    private string $customConfiguration;

    // Validate on parsed data, with simple constraints.
    #[Assert\Schema(
        format: 'yaml',
        constraints: new Assert\NotNull(),
    )]
    private string $customConfiguration;

    // Validate on parsed data, with composite constraints (example with `Collection`).
    #[Assert\Schema(
        format: 'yaml',
        constraints: new Assert\Collection([
            'foo' => new Assert\NotNull(),
        ]),
    )]
    private string $customConfiguration;
}

@carsonbot carsonbot added this to the 7.2 milestone Oct 14, 2024
@OskarStark OskarStark changed the title [Validator] Add configClass option check to Yaml constraint using symfony/config. [Validator] Add configClass option check to Yaml constraint using symfony/config Oct 14, 2024
@WedgeSama WedgeSama force-pushed the yaml-with-config-validation branch 2 times, most recently from 0f3a7cf to 31aced8 Compare October 14, 2024 10:29
@stof
Copy link
Member

stof commented Oct 14, 2024

Does this really belong in the Yaml constraint ?

@xabbuh
Copy link
Member

xabbuh commented Oct 15, 2024

I doubt that this is a common enough use case that justifies to add this feature to the Symfony core.

@WedgeSama
Copy link
Contributor Author

IMO, now that there is a Yaml constraint, I think config validation will be common.

It allow to validate your Yaml tree directly (e.g. in form submit, or API call) without more action but to create your Configuration class.

That way, you ensure the Yaml can be validate soon in your process without to much code to do it.

IMO, it can be good to also add this to Json constraint too.

@xabbuh
Copy link
Member

xabbuh commented Oct 15, 2024

I have never see an application where the config for a bundle is managed by a user of the application. Can you explain with more details how your actual use case looks like please?

@WedgeSama
Copy link
Contributor Author

WedgeSama commented Oct 15, 2024

The case I see is not about bundle's configuration (and you are right, bundle's config must not be managed by your app interface).

It is about configuration in general. In some app, you can find/manage config for other app or parts of the app itself, like Home Assistant for example (just the principle).

In the case of Home Assistant, Yaml is used to configure and customize users' dashboard, directly from the web interface, by each user. And the problem for this example is the lack of validation on submit. With this PR, problem solve.

I know some app I worked on where this capability will have been good.

@stof
Copy link
Member

stof commented Oct 15, 2024

@WedgeSama I would suggest you to create a custom constraint in your project for that case (or in a third-party bundle if you want to reuse it). I don't think this use case is common enough to justify adding it in the core.

Btw, your proposal requires that the provided config class can be instantiated with no argument, which is not guaranteed at all by the interface.

@WedgeSama
Copy link
Contributor Author

After a chat with @Neirda24 it may be more relevant to use encapsulation of Collection constraint to do only validation. It will match more the logic of the Validator.

It will do something similar to what I wanted to do, but without symfony/config (it will also solve the config class instance you mention).

I'll update the PR.

@WedgeSama WedgeSama force-pushed the yaml-with-config-validation branch 2 times, most recently from fcd9a0e to 5932e71 Compare October 15, 2024 12:20
@WedgeSama WedgeSama changed the title [Validator] Add configClass option check to Yaml constraint using symfony/config [Validator] Add encapsulated Collection constraint in Yaml constraint. Oct 15, 2024
@WedgeSama WedgeSama force-pushed the yaml-with-config-validation branch from 5932e71 to 2b2851c Compare October 15, 2024 14:37
@WedgeSama WedgeSama changed the title [Validator] Add encapsulated Collection constraint in Yaml constraint. [Validator] Add new Schema validation constraint. Oct 15, 2024
This constraint is like an extension of `Json` and `Yaml` constraints. It checks if given value is valid for the given format, then, if you declare `constraints` option, it will try to apply those constraints to the deserialized data.
@WedgeSama WedgeSama force-pushed the yaml-with-config-validation branch from 2b2851c to 9da3762 Compare October 15, 2024 14:46
@alexandre-daubois
Copy link
Member

If I want to validate a JSON against an actual schema, I would create a new constraint using, for example, https://packagist.org/packages/justinrainbow/json-schema. I'm not sure I would like to rewrite it with constraints by hand.

As far as I can tell, for the Yaml part, the PR only validates that the Yaml is valid, no schema is actually checked? Also, you're restoring an error handler in the validator but you didn't set one, what's the reason?

@OskarStark OskarStark changed the title [Validator] Add new Schema validation constraint. [Validator] Add new Schema validation constraint Oct 15, 2024
@valtzu
Copy link
Contributor

valtzu commented Oct 23, 2024

I recall having a similar need in the past, also related to config management via UI – so +1 for the idea from me.


Instead of putting serializer logic into validator component, how about adding a #[Assert\Serialized(format: '...', constraints: [...])] constraint to symfony/serializer?

And then in the framework config wire @serializer to the SerializedValidator. This way we'd support all the formats that serializer supports. And of course some defaults would need to be configured in case you use the components without the framework.

@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@wkania
Copy link
Contributor

wkania commented Apr 30, 2025

Perhaps it would be better if this was an optional schema definition within the JSON constraint?
Similar to how the egulias/email-validator is used for email validation, in this case we could use justinrainbow/json-schema.

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.

8 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.