-
Notifications
You must be signed in to change notification settings - Fork 2.9k
V15: Serverside Media Picker Validation #18429
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
Merged
nikolajlauridsen
merged 15 commits into
v15/dev
from
v15/feature/serverside-media-validation
Feb 25, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
27ef071
Add TypedJsonValidator to avoid duplicate serialization
nikolajlauridsen dc2cd96
Add allowed type validator
nikolajlauridsen 815a091
Validate multiple media toggle
nikolajlauridsen 8420014
Add startnode validator
nikolajlauridsen 8b90038
Fix tests
nikolajlauridsen 7d49b48
Add validation tests
nikolajlauridsen 1f547df
Apply suggestions from code review
nikolajlauridsen 7f27bce
Add XML docs
nikolajlauridsen 9fcb69b
Remove unnecessary obsolete constructor
nikolajlauridsen 13e2204
Avoid multiple checks
nikolajlauridsen ec53b71
Use value instead of specific member names
nikolajlauridsen 1d1852e
Remove test
nikolajlauridsen f2ff509
Optimize StartNodeValidator
nikolajlauridsen 83667b0
Clarify Validates_Allowed_Type
nikolajlauridsen 97dda23
Merge branch 'v15/dev' into v15/feature/serverside-media-validation
nikolajlauridsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Umbraco.Core/PropertyEditors/Validation/ITypedJsonValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
| using Umbraco.Cms.Core.Models.Validation; | ||
|
|
||
| namespace Umbraco.Cms.Core.PropertyEditors.Validation; | ||
|
|
||
| /// <summary> | ||
| /// A specific validator used for JSON based value editors, to avoid doing multiple deserialization. | ||
| /// <remarks>Is used together with <see cref="TypedJsonValidatorRunner{TValue,TConfiguration}"/></remarks> | ||
| /// </summary> | ||
| /// <typeparam name="TValue">The type of the value consumed by the validator.</typeparam> | ||
| /// <typeparam name="TConfiguration">The type of the configuration consumed by validator.</typeparam> | ||
| public interface ITypedJsonValidator<TValue, TConfiguration> | ||
| { | ||
| public abstract IEnumerable<ValidationResult> Validate( | ||
| TValue? value, | ||
| TConfiguration? configuration, | ||
| string? valueType, | ||
| PropertyValidationContext validationContext); | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
src/Umbraco.Core/PropertyEditors/Validation/TypedJsonValidatorRunner.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
| using Umbraco.Cms.Core.Models.Validation; | ||
| using Umbraco.Cms.Core.Serialization; | ||
|
|
||
| namespace Umbraco.Cms.Core.PropertyEditors.Validation; | ||
|
|
||
| /// <summary> | ||
| /// <para> | ||
| /// An aggregate validator for JSON based value editors, to avoid doing multiple deserialization. | ||
| /// </para> | ||
| /// <para> | ||
| /// Will deserialize once, and cast the configuration once, and pass those values to each <see cref="ITypedJsonValidator{TValue,TConfiguration}"/>, aggregating the results. | ||
| /// </para> | ||
| /// </summary> | ||
| /// <typeparam name="TValue">The type of the expected value.</typeparam> | ||
| /// <typeparam name="TConfiguration">The type of the expected configuration</typeparam> | ||
| public class TypedJsonValidatorRunner<TValue, TConfiguration> : IValueValidator | ||
| where TValue : class | ||
| { | ||
| private readonly IJsonSerializer _jsonSerializer; | ||
| private readonly ITypedJsonValidator<TValue, TConfiguration>[] _validators; | ||
|
|
||
| public TypedJsonValidatorRunner(IJsonSerializer jsonSerializer, params ITypedJsonValidator<TValue, TConfiguration>[] validators) | ||
| { | ||
| _jsonSerializer = jsonSerializer; | ||
| _validators = validators; | ||
| } | ||
|
|
||
| public IEnumerable<ValidationResult> Validate(object? value, string? valueType, object? dataTypeConfiguration, | ||
| PropertyValidationContext validationContext) | ||
| { | ||
| var validationResults = new List<ValidationResult>(); | ||
|
|
||
| if (dataTypeConfiguration is not TConfiguration configuration) | ||
| { | ||
| return validationResults; | ||
| } | ||
|
|
||
| if (value is null || _jsonSerializer.TryDeserialize(value, out TValue? deserializedValue) is false) | ||
| { | ||
| return validationResults; | ||
| } | ||
|
|
||
| foreach (ITypedJsonValidator<TValue, TConfiguration> validator in _validators) | ||
| { | ||
| validationResults.AddRange(validator.Validate(deserializedValue, configuration, valueType, validationContext)); | ||
| } | ||
|
|
||
| return validationResults; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.