| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | using System.Data; |
| | | 5 | | using System.Linq.Expressions; |
| | | 6 | | using Api.Database.Utils; |
| | | 7 | | using FluentValidation; |
| | | 8 | | using HotChocolate; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | |
| | | 11 | | namespace Api.Utils; |
| | | 12 | | |
| | | 13 | | public static class ValidatorUtils |
| | | 14 | | { |
| | | 15 | | public static IRuleBuilderOptions<T, TInner> WhenOptionalSet<T, TInner>( |
| | | 16 | | this IRuleBuilderOptions<T, TInner> rule, |
| | | 17 | | Func<T, IOptional> expr |
| | 1 | 18 | | ) => rule.When((t) => expr(t).HasValue && expr(t).Value is not null); |
| | | 19 | | |
| | | 20 | | public static IRuleBuilderOptions<T, IEnumerable<TInner>> MustBeDistinct<T, TInner>( |
| | | 21 | | this IRuleBuilder<T, IEnumerable<TInner>> rule, |
| | | 22 | | string propName, |
| | | 23 | | IEqualityComparer<TInner>? comparer = null |
| | | 24 | | ) |
| | 1 | 25 | | { |
| | 1 | 26 | | return rule.Must(t => t.Distinct().SequenceEqual(t, comparer)) |
| | 1 | 27 | | .WithMessage($"{propName} must be distinct") |
| | 1 | 28 | | .WithErrorCode(ErrorCodes.IS_NOT_DISTINCT); |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | public static void BeginTransaction<T, TProp>( |
| | | 32 | | this IRuleBuilderInitial<T, TProp> rule, |
| | | 33 | | IEFTransactionDIAccessorService tx |
| | | 34 | | ) |
| | 1 | 35 | | { |
| | 1 | 36 | | rule.MustAsync( |
| | 1 | 37 | | async (_, ct) => |
| | 1 | 38 | | { |
| | 1 | 39 | | await tx.BeginOrGetTransactionAsync(); |
| | 1 | 40 | | return true; |
| | 1 | 41 | | } |
| | 1 | 42 | | ); |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | public static IRuleBuilderOptions<T, TProp> IdsMustExist<T, TProp, TTarget>( |
| | | 46 | | this IRuleBuilder<T, TProp> rule, |
| | | 47 | | DbSet<TTarget> target, |
| | | 48 | | Guid? ownerId |
| | | 49 | | ) |
| | | 50 | | where TProp : IEnumerable<Guid> |
| | | 51 | | where TTarget : class, IDbEntityMetadata |
| | 1 | 52 | | { |
| | 1 | 53 | | return rule.MustAsync( |
| | 1 | 54 | | async (t, ids, ct) => |
| | 1 | 55 | | { |
| | 1 | 56 | | var q = target.Where(t => ids.Contains(t.Id)); |
| | 1 | 57 | | |
| | 1 | 58 | | if (ownerId is not null) |
| | 1 | 59 | | { |
| | 1 | 60 | | q = q.Where(t => t.OwnerId == ownerId); |
| | 1 | 61 | | } |
| | 1 | 62 | | |
| | 1 | 63 | | return ( |
| | 1 | 64 | | await q.Select(t => t.Id).Distinct().Order().ToArrayAsync(ct) |
| | 1 | 65 | | ).SequenceEqual(ids.Distinct().Order()); |
| | 1 | 66 | | } |
| | 1 | 67 | | ) |
| | 1 | 68 | | .WithMessage($"{nameof(TTarget)} ids must exist") |
| | 1 | 69 | | .WithErrorCode(ErrorCodes.IDS_DOES_NOT_EXIST); |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | public static IRuleBuilderOptions<T, Guid> IdMustExist<T, TTarget>( |
| | | 73 | | this IRuleBuilder<T, Guid> rule, |
| | | 74 | | DbSet<TTarget> target, |
| | | 75 | | Guid? ownerId |
| | | 76 | | ) |
| | | 77 | | where TTarget : class, IDbEntityMetadata |
| | 1 | 78 | | { |
| | 1 | 79 | | return rule.MustAsync( |
| | 1 | 80 | | async (id, ct) => |
| | 1 | 81 | | { |
| | 1 | 82 | | var q = target.Where(t => t.Id == id); |
| | 1 | 83 | | if (ownerId is not null) |
| | 1 | 84 | | { |
| | 1 | 85 | | q = q.Where(t => t.OwnerId == ownerId); |
| | 1 | 86 | | } |
| | 1 | 87 | | return await q.AnyAsync(ct); |
| | 1 | 88 | | } |
| | 1 | 89 | | ) |
| | 1 | 90 | | .WithMessage($"{nameof(TTarget)} id must exist") |
| | 1 | 91 | | .WithErrorCode(ErrorCodes.ID_DOES_NOT_EXIST); |
| | 1 | 92 | | } |
| | | 93 | | |
| | | 94 | | public static IRuleBuilderOptions<T, int> RowVersionMustMatch<T, TTarget>( |
| | | 95 | | this IRuleBuilder<T, int> rule, |
| | | 96 | | DbSet<TTarget> target |
| | | 97 | | ) |
| | | 98 | | where T : class, IBasicEntityMetadata |
| | | 99 | | where TTarget : class, IDbEntityMetadata |
| | 1 | 100 | | { |
| | 1 | 101 | | return rule.MustAsync( |
| | 1 | 102 | | async (t, rowVer, ct) => |
| | 1 | 103 | | await target |
| | 1 | 104 | | .Where(tt => tt.Id == t.Id && tt.RowVersion == rowVer) |
| | 1 | 105 | | .AnyAsync(cancellationToken: ct) |
| | 1 | 106 | | ) |
| | 1 | 107 | | .WithMessage( |
| | 1 | 108 | | $"Row version for `{nameof(TTarget)}` does not match with provided row version" |
| | 1 | 109 | | ) |
| | 1 | 110 | | .WithErrorCode(ErrorCodes.ROW_VERSION_MISMATCH); |
| | 1 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Validates <paramref name="input"/>; throws an <see cref="DomainException"/> per failure |
| | | 115 | | /// (as an <see cref="AggregateException"/> when there are several) so the mutation's |
| | | 116 | | /// <c>[Error]</c> middleware reports them in the payload's <c>errors</c> list. |
| | | 117 | | /// </summary> |
| | | 118 | | public static async Task ValidateOrThrowInputAsync<T>( |
| | | 119 | | this IValidator<T> validator, |
| | | 120 | | T input, |
| | | 121 | | CancellationToken ct = default |
| | | 122 | | ) |
| | 1 | 123 | | { |
| | 1 | 124 | | var result = await validator.ValidateAsync(input, ct); |
| | | 125 | | |
| | 1 | 126 | | if (result.IsValid) |
| | 1 | 127 | | { |
| | 1 | 128 | | return; |
| | | 129 | | } |
| | | 130 | | |
| | 1 | 131 | | var failures = result |
| | 1 | 132 | | .Errors.Select(failure => new DomainException( |
| | 1 | 133 | | failure.ErrorCode, |
| | 1 | 134 | | failure.ErrorMessage |
| | 1 | 135 | | )) |
| | 1 | 136 | | .ToArray(); |
| | | 137 | | |
| | 1 | 138 | | throw failures.Length is 1 ? failures[0] : new AggregateException(failures); |
| | 1 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Validates <paramref name="input"/>; throws a <see cref="GraphQLException"/> carrying the |
| | | 143 | | /// failures when the input is invalid. Use for fields that have no payload to put errors in. |
| | | 144 | | /// </summary> |
| | | 145 | | public static async Task ValidateOrThrowAsync<T>( |
| | | 146 | | this IValidator<T> validator, |
| | | 147 | | T input, |
| | | 148 | | CancellationToken ct = default |
| | | 149 | | ) |
| | 1 | 150 | | { |
| | 1 | 151 | | var result = await validator.ValidateAsync(input, ct); |
| | | 152 | | |
| | 1 | 153 | | if (!result.IsValid) |
| | 1 | 154 | | { |
| | 1 | 155 | | throw new GraphQLException([ |
| | 1 | 156 | | .. result.Errors.Select(failure => |
| | 1 | 157 | | ErrorBuilder |
| | 1 | 158 | | .New() |
| | 1 | 159 | | .SetMessage(failure.ErrorMessage) |
| | 1 | 160 | | .SetCode(failure.ErrorCode) |
| | 1 | 161 | | .Build() |
| | 1 | 162 | | ), |
| | 1 | 163 | | ]); |
| | | 164 | | } |
| | 1 | 165 | | } |
| | | 166 | | } |