-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathReferenceDataCodeValidator.cs
More file actions
27 lines (23 loc) · 1.11 KB
/
ReferenceDataCodeValidator.cs
File metadata and controls
27 lines (23 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.RefData;
using FluentValidation;
using FluentValidation.Validators;
using System;
namespace CoreEx.FluentValidation
{
/// <summary>
/// Represents a <see cref="IReferenceData.Code"/> validator.
/// </summary>
/// <typeparam name="T">The owning object <see cref="Type"/>.</typeparam>
/// <typeparam name="TRef">The <see cref="IReferenceData"/> <see cref="Type"/>.</typeparam>
public class ReferenceDataCodeValidator<T, TRef> : PropertyValidator<T, string?> where TRef : IReferenceData
{
/// <inheritdoc/>
public override string Name => nameof(ReferenceDataCodeValidator<T, TRef>);
/// <inheritdoc/>
public override bool IsValid(ValidationContext<T> context, string? value) =>
value == null || ReferenceDataOrchestrator.Current.GetByTypeRequired<TRef>().TryGetByCode(value, out var rd) && rd!.IsValid;
/// <inheritdoc/>
protected override string GetDefaultMessageTemplate(string errorCode) => "'{PropertyName}' is invalid.";
}
}