Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

[Validator] Add newSchema 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
WedgeSama wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromWedgeSama:yaml-with-config-validation

Conversation

WedgeSama
Copy link
Contributor

@WedgeSamaWedgeSama commentedOct 14, 2024
edited
Loading

QA
Branch?7.2
Bug fix?no
New feature?yes
Deprecations?no
Issues
LicenseMIT
Doc PRsymfony/symfony-docs#20323

This constraint is like an extension ofJson andYaml constraints. It checks if given value is valid for the given format, then, if you declareconstraints option, it will try to apply those constraints to the deserialized data.

Usage examples:

// src/Entity/Report.phpnamespaceApp\Entity;useApp\Yaml\MyConfiguration;useSymfony\Component\Validator\ConstraintsasAssert;class Report{// Only validate format (like Json or Yaml constraints).    #[Assert\Schema(        format:'yaml',    )]privatestring$customConfiguration;// Same in short.    #[Assert\Schema('yaml')]privatestring$customConfiguration;// Validate on parsed data, with simple constraints.    #[Assert\Schema(        format:'yaml',        constraints:newAssert\NotNull(),    )]privatestring$customConfiguration;// Validate on parsed data, with composite constraints (example with `Collection`).    #[Assert\Schema(        format:'yaml',        constraints:newAssert\Collection(['foo' =>newAssert\NotNull(),        ]),    )]privatestring$customConfiguration;}

@carsonbotcarsonbot added this to the7.2 milestoneOct 14, 2024
@OskarStarkOskarStark changed the title[Validator] AddconfigClass option check to Yaml constraint usingsymfony/config.[Validator] AddconfigClass option check to Yaml constraint usingsymfony/configOct 14, 2024
@WedgeSamaWedgeSamaforce-pushed theyaml-with-config-validation branch 2 times, most recently from0f3a7cf to31aced8CompareOctober 14, 2024 10:29
@stof
Copy link
Member

Does this really belong in theYaml constraint ?

@xabbuh
Copy link
Member

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

@WedgeSama
Copy link
ContributorAuthor

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

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
ContributorAuthor

WedgeSama commentedOct 15, 2024
edited
Loading

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, likeHome 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

@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
ContributorAuthor

After a chat with@Neirda24 it may be more relevant to use encapsulation ofCollection 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 withoutsymfony/config (it will also solve the config class instance you mention).

I'll update the PR.

@WedgeSamaWedgeSamaforce-pushed theyaml-with-config-validation branch 2 times, most recently fromfcd9a0e to5932e71CompareOctober 15, 2024 12:20
@WedgeSamaWedgeSama changed the title[Validator] AddconfigClass option check to Yaml constraint usingsymfony/config[Validator] Add encapsulated Collection constraint in Yaml constraint.Oct 15, 2024
@WedgeSamaWedgeSamaforce-pushed theyaml-with-config-validation branch from5932e71 to2b2851cCompareOctober 15, 2024 14:37
@WedgeSamaWedgeSama changed the title[Validator] Add encapsulated Collection constraint in Yaml constraint.[Validator] Add newSchema 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.
@WedgeSamaWedgeSamaforce-pushed theyaml-with-config-validation branch from2b2851c to9da3762CompareOctober 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?

mdeboer reacted with thumbs up emoji

@OskarStarkOskarStark changed the title[Validator] Add newSchema validation constraint.[Validator] Add newSchema validation constraintOct 15, 2024
@valtzu
Copy link
Contributor

valtzu commentedOct 23, 2024
edited
Loading

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 tosymfony/serializer?

And then in the framework config wire@serializer to theSerializedValidator. 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.

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

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

@fabpotfabpot removed this from the7.3 milestoneMay 26, 2025
@fabpotfabpot added this to the7.4 milestoneMay 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@alexandre-dauboisalexandre-dauboisalexandre-daubois left review comments

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

8 participants
@WedgeSama@stof@xabbuh@alexandre-daubois@valtzu@wkania@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp