| | | 1 | | // Copyright (c) 2020 .NET Foundation and Contributors. All rights reserved. |
| | | 2 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 3 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 4 | | // See the LICENSE file in the project root for full license information. |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Linq.Expressions; |
| | | 10 | | using ReactiveUI.Validation.Components; |
| | | 11 | | using ReactiveUI.Validation.Components.Abstractions; |
| | | 12 | | using ReactiveUI.Validation.Contexts; |
| | | 13 | | |
| | | 14 | | namespace ReactiveUI.Validation.Extensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Extensions methods for <see cref="ValidationContext"/>. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class ValidationContextExtensions |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Resolves the property valuation for a specified property. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 25 | | /// <typeparam name="TViewModelProperty">ViewModel property type.</typeparam> |
| | | 26 | | /// <param name="context">ValidationContext instance.</param> |
| | | 27 | | /// <param name="viewModelProperty">ViewModel property.</param> |
| | | 28 | | /// <param name="strict">Indicates if the ViewModel property to find is unique.</param> |
| | | 29 | | /// <returns>Returns a collection of <see cref="BasePropertyValidation{TViewModel}"/> objects.</returns> |
| | | 30 | | public static IEnumerable<IPropertyValidationComponent<TViewModel>> ResolveFor<TViewModel, TViewModelProperty>( |
| | | 31 | | this ValidationContext context, |
| | | 32 | | Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty, |
| | | 33 | | bool strict = true) |
| | | 34 | | { |
| | 16 | 35 | | if (context is null) |
| | | 36 | | { |
| | 0 | 37 | | throw new ArgumentNullException(nameof(context)); |
| | | 38 | | } |
| | | 39 | | |
| | 16 | 40 | | if (viewModelProperty is null) |
| | | 41 | | { |
| | 0 | 42 | | throw new ArgumentNullException(nameof(viewModelProperty)); |
| | | 43 | | } |
| | | 44 | | |
| | 16 | 45 | | return context |
| | 16 | 46 | | .Validations |
| | 16 | 47 | | .OfType<IPropertyValidationComponent<TViewModel>>() |
| | 40 | 48 | | .Where(v => v.ContainsProperty(viewModelProperty, strict)); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Resolves the property valuation for two properties. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 55 | | /// <typeparam name="TProperty1">ViewModel first property type.</typeparam> |
| | | 56 | | /// <typeparam name="TProperty2">ViewModel second property type.</typeparam> |
| | | 57 | | /// <param name="context">ValidationContext instance.</param> |
| | | 58 | | /// <param name="viewModelProperty1">First ViewModel property.</param> |
| | | 59 | | /// <param name="viewModelProperty2">Second ViewModel property.</param> |
| | | 60 | | /// <returns>Returns a collection of <see cref="BasePropertyValidation{TViewModel}"/> objects.</returns> |
| | | 61 | | public static IEnumerable<IPropertyValidationComponent<TViewModel>> ResolveFor<TViewModel, TProperty1, |
| | | 62 | | TProperty2>( |
| | | 63 | | this ValidationContext context, |
| | | 64 | | Expression<Func<TViewModel, TProperty1>> viewModelProperty1, |
| | | 65 | | Expression<Func<TViewModel, TProperty2>> viewModelProperty2) |
| | | 66 | | { |
| | 0 | 67 | | if (context is null) |
| | | 68 | | { |
| | 0 | 69 | | throw new ArgumentNullException(nameof(context)); |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | if (viewModelProperty1 is null) |
| | | 73 | | { |
| | 0 | 74 | | throw new ArgumentNullException(nameof(viewModelProperty1)); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | if (viewModelProperty2 is null) |
| | | 78 | | { |
| | 0 | 79 | | throw new ArgumentNullException(nameof(viewModelProperty2)); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | return context |
| | 0 | 83 | | .Validations |
| | 0 | 84 | | .OfType<IPropertyValidationComponent<TViewModel>>() |
| | 0 | 85 | | .Where(v => v.ContainsProperty(viewModelProperty1) && |
| | 0 | 86 | | v.ContainsProperty(viewModelProperty2) && |
| | 0 | 87 | | v.PropertyCount == 2); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Resolves the property valuation for three properties. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 94 | | /// <typeparam name="TProperty1">ViewModel first property type.</typeparam> |
| | | 95 | | /// <typeparam name="TProperty2">ViewModel second property type.</typeparam> |
| | | 96 | | /// <typeparam name="TProperty3">ViewModel third property type.</typeparam> |
| | | 97 | | /// <param name="context">ValidationContext instance.</param> |
| | | 98 | | /// <param name="viewModelProperty1">First ViewModel property.</param> |
| | | 99 | | /// <param name="viewModelProperty2">Second ViewModel property.</param> |
| | | 100 | | /// <param name="viewModelProperty3">Third ViewModel property.</param> |
| | | 101 | | /// <returns>Returns a collection of <see cref="BasePropertyValidation{TViewModel}"/> objects.</returns> |
| | | 102 | | public static IEnumerable<IPropertyValidationComponent<TViewModel>> ResolveFor<TViewModel, |
| | | 103 | | TProperty1, TProperty2, TProperty3>( |
| | | 104 | | this ValidationContext context, |
| | | 105 | | Expression<Func<TViewModel, TProperty1>> viewModelProperty1, |
| | | 106 | | Expression<Func<TViewModel, TProperty2>> viewModelProperty2, |
| | | 107 | | Expression<Func<TViewModel, TProperty3>> viewModelProperty3) |
| | | 108 | | { |
| | 0 | 109 | | if (context is null) |
| | | 110 | | { |
| | 0 | 111 | | throw new ArgumentNullException(nameof(context)); |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | if (viewModelProperty1 is null) |
| | | 115 | | { |
| | 0 | 116 | | throw new ArgumentNullException(nameof(viewModelProperty1)); |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if (viewModelProperty2 is null) |
| | | 120 | | { |
| | 0 | 121 | | throw new ArgumentNullException(nameof(viewModelProperty2)); |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | if (viewModelProperty3 is null) |
| | | 125 | | { |
| | 0 | 126 | | throw new ArgumentNullException(nameof(viewModelProperty3)); |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | return context |
| | 0 | 130 | | .Validations |
| | 0 | 131 | | .OfType<IPropertyValidationComponent<TViewModel>>() |
| | 0 | 132 | | .Where(v => v.ContainsProperty(viewModelProperty1) && |
| | 0 | 133 | | v.ContainsProperty(viewModelProperty2) && |
| | 0 | 134 | | v.ContainsProperty(viewModelProperty3) && |
| | 0 | 135 | | v.PropertyCount == 3); |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | } |