From 6bbe98cf1f110fa44b4eebf04038d9b7e0b0aba7 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Thu, 15 Dec 2022 12:02:08 +0100 Subject: [PATCH] update raven validator extensions --- zero.Raven/Extensions/ValidatorExtensions.cs | 45 ++++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/zero.Raven/Extensions/ValidatorExtensions.cs b/zero.Raven/Extensions/ValidatorExtensions.cs index edfdff7f..e1f75019 100644 --- a/zero.Raven/Extensions/ValidatorExtensions.cs +++ b/zero.Raven/Extensions/ValidatorExtensions.cs @@ -1,9 +1,5 @@ using FluentValidation; using Raven.Client.Documents; -using System.Globalization; -using zero.Extensions; -using zero.Models; -using zero.Raven; namespace zero.Raven; @@ -12,11 +8,12 @@ public static class ValidatorExtensions /// /// Check if this value is unique within a collection /// - public static IRuleBuilderOptions Unique(this IRuleBuilder ruleBuilder, IZeroStore store) where T : ZeroEntity + public static IRuleBuilderOptions Unique(this IRuleBuilder ruleBuilder, IRavenOperations ops) + where T : ZeroIdEntity { return ruleBuilder.MustAsync(async (entity, value, context, cancellation) => { - bool any = await store.Session().Advanced.AsyncDocumentQuery() + bool any = await ops.Session.Advanced.AsyncDocumentQuery() .WhereNotEquals(nameof(ZeroIdEntity.Id), entity.Id) .WhereEquals(context.PropertyName.ToPascalCaseId(), value) .AnyAsync(cancellation); @@ -26,14 +23,31 @@ public static class ValidatorExtensions } + /// + /// Check if this value is unique within a collection + /// + public static IRuleBuilderOptions Unique(this IRuleBuilder ruleBuilder, IRavenOperations ops) + { + return ruleBuilder.MustAsync(async (entity, value, context, cancellation) => + { + bool any = await ops.Session.Advanced.AsyncDocumentQuery() + .WhereEquals(context.PropertyName.ToPascalCaseId(), value) + .AnyAsync(cancellation); + + return !any; + }).WithMessage("@errors.forms.not_unique"); + } + + /// /// Check if this value is at least set once to the expected value within a collection /// - public static IRuleBuilderOptions ExpectAnyUnique(this IRuleBuilder ruleBuilder, IZeroStore store, TProperty expectedValue) where T : ZeroEntity + public static IRuleBuilderOptions ExpectAnyUnique(this IRuleBuilder ruleBuilder, IRavenOperations ops, TProperty expectedValue) + where T : ZeroIdEntity { return ruleBuilder.MustAsync(async (entity, value, context, cancellation) => { - return await store.Session().Advanced.AsyncDocumentQuery() + return await ops.Session.Advanced.AsyncDocumentQuery() .WhereNotEquals(nameof(ZeroIdEntity.Id), entity.Id) .WhereEquals(context.PropertyName.ToPascalCaseId(), expectedValue) .AnyAsync(cancellation); @@ -42,18 +56,21 @@ public static class ValidatorExtensions /// - /// Check if this reference exists and is an entity which can be referenced (appId = shared for shareable entities or appId = current) + /// Check if this reference exists and is an entity which can be referenced /// - public static IRuleBuilderOptions Exists(this IRuleBuilder ruleBuilder, IZeroStore store) where T : ZeroEntity + public static IRuleBuilderOptions Exists(this IRuleBuilder ruleBuilder, IRavenOperations ops) + where T : ZeroIdEntity { - return ruleBuilder.Exists(store); + return ruleBuilder.Exists(ops); } /// - /// Check if this reference exists and is an entity which can be referenced (appId = shared for shareable entities or appId = current) + /// Check if this reference exists and is an entity which can be referenced /// - public static IRuleBuilderOptions Exists(this IRuleBuilder ruleBuilder, IZeroStore store) where T : ZeroEntity where TReference : ZeroEntity + public static IRuleBuilderOptions Exists(this IRuleBuilder ruleBuilder, IRavenOperations ops) + where T : ZeroIdEntity + where TCollection : ZeroIdEntity { return ruleBuilder.MustAsync(async (entity, id, context, cancellation) => { @@ -62,7 +79,7 @@ public static class ValidatorExtensions return true; } - return await store.Session().Query().AnyAsync(x => x.Id == id); + return await ops.Session.Query().AnyAsync(x => x.Id == id); }).WithMessage("@errors.forms.reference_notfound"); } }