/// <summary> /// Validates if the value is present in the provided data by key. /// </summary> /// <typeparam name="T">The source type of the item being validated.</typeparam> /// <typeparam name="TProperty">The source type of the property being validated.</typeparam> /// <typeparam name="TData">The type of the data in the keyed value provider.</typeparam> /// <param name="ruleBuilder">The rule builder to add the rule to.</param> /// <param name="keyedDataProvider">The data provider for the rule.</param> /// <returns><see cref="IRuleBuilderOptions{T, TProperty}"/> for the new rule.</returns> public static IRuleBuilderOptions <T, TProperty> ValidateKey <T, TProperty, TData, TKey, TValue> (this IRuleBuilder <T, TProperty> rule, IProvideKeyValueData <TData, TKey, TValue> keyedDataProvider) where TData : IHaveKeyValue <TKey, TValue> where TProperty : TKey => rule.MustAsync(async(source, cancellationToken) => source == null ? false : (await keyedDataProvider.GetTypedReadOnlyDictionaryAsync()).ContainsKey(source)) .CreateMessageCode($"{nameof(ValidateKey)}.{typeof(TData).Name}");
/// <summary> /// Validates if the value is present in the provided data by value. /// </summary> /// <typeparam name="T">The source type of the item being validated.</typeparam> /// <typeparam name="TProperty">The source type of the property being validated.</typeparam> /// <typeparam name="TData">The type of the data in the keyed value provider.</typeparam> /// <param name="ruleBuilder">The rule builder to add the rule to.</param> /// <param name="keyedDataProvider">The data provider for the rule.</param> /// <returns><see cref="IRuleBuilderOptions{T, TProperty}"/> for the new rule.</returns> public static IRuleBuilderOptions <T, TProperty> ValidateValue <T, TProperty, TData, TKey, TValue> (this IRuleBuilder <T, TProperty> ruleBuilder, IProvideKeyValueData <TData, TKey, TValue> keyedDataProvider) where TData : IHaveKeyValue <TKey, TValue> where TProperty : TValue => ruleBuilder.MustAsync(async(source, cancellationToken) => (await keyedDataProvider.GetTypedReadOnlyDictionaryAsync()).Values.Any(value => value.Equals(source))) .CreateMessageCode($"{nameof(ValidateValue)}.{typeof(TData).Name}");
/// <summary> /// Initializes a new instance of the <see cref="SourceExampleValidator" /> class. /// </summary> /// <param name="yesNoKeyedDataValidationRule"> /// The <see cref="IKeyedDataValidationRule{TKey, TValue}"/> for <see cref="YesNoLookupData"/>. /// </param> public SourceExampleValidator(IProvideKeyValueData <YesNoLookupData, string, string> yesNoKeyedData, IValidator <Address> addressValidator) : base(addressValidator) { // let's save this for later in case something else is overriden that needs it. YesNoKeyedData = yesNoKeyedData; // add validation rules RuleFor(source => source.TestString).Required().ValidateKey(YesNoKeyedData); }
public DestExampleValidator(IProvideKeyValueData <YesNoLookupData, string, string> yesNoKeyedData) { // let's save this for later in case something else is overriden that needs it. YesNoKeyedData = yesNoKeyedData; // add validation rules RuleFor(source => source.TestBool).Required(); RuleFor(source => source.TestByte).Required(); RuleFor(source => source.TestChar).Required(); RuleFor(source => source.TestDateTime).Required(); RuleFor(source => source.TestDateTimeOffset).Required(); RuleFor(source => source.TestDecimal).Required(); RuleFor(source => source.TestDouble).Required(); RuleFor(source => source.TestFloat).Required(); RuleFor(source => source.TestGuid).Required(); RuleFor(source => source.TestInt16).Required(); RuleFor(source => source.TestInt32).Required(); RuleFor(source => source.TestInt64).Required(); RuleFor(source => source.TestSbyte).Required(); RuleFor(source => source.TestSingle).Required(); RuleFor(source => source.TestTimeSpan).Required(); RuleFor(source => source.TestUint16).Required(); RuleFor(source => source.TestUint32).Required(); RuleFor(source => source.TestUint64).Required(); RuleFor(source => source.TestDeriveStringToBool).Required() .ValidateValue(YesNoKeyedData); RuleFor(source => source.TestDeriveAddedValue).Required(); }
public SourceExampleToDestExampleDerivation(IProvideKeyValueData <YesNoLookupData, string, string> keyedDataProvider) => KeyedDataProvider = keyedDataProvider;