private static Validator WrapAndInstrumentValidator(Validator validator, IConfigurationSource configurationSource) { ValidatorWrapper validatorWrapper = new ValidatorWrapper(validator); AttachInstrumentationListener(validatorWrapper, configurationSource); return(validatorWrapper); }
/// <summary> /// Adds a validator to handle a specific subclass. /// </summary> /// <param name="validatorFactory">The derived validator</param> /// <typeparam name="TDerived"></typeparam> /// <param name="ruleSets">Optionally specify rulesets to execute. If set, rules not in these rulesets will not be run</param> /// <returns></returns> public PolymorphicValidator <T, TProperty> Add <TDerived>(Func <T, TDerived, IValidator <TDerived> > validatorFactory, params string[] ruleSets) where TDerived : TProperty { if (validatorFactory == null) { throw new ArgumentNullException(nameof(validatorFactory)); } _derivedValidators[typeof(TDerived)] = new ValidatorWrapper(context => validatorFactory((T)context.ParentContext.InstanceToValidate, (TDerived)context.PropertyValue), ruleSets); return(this); }
/// <summary> /// Adds a validator to handle a specific subclass. /// </summary> /// <param name="derivedValidator">The derived validator</param> /// <param name="ruleSets">Optionally specify rulesets to execute. If set, rules not in these rulesets will not be run</param> /// <typeparam name="TDerived"></typeparam> /// <returns></returns> public PolymorphicValidator <T, TProperty> Add <TDerived>(IValidator <TDerived> derivedValidator, params string[] ruleSets) where TDerived : TProperty { if (derivedValidator == null) { throw new ArgumentNullException(nameof(derivedValidator)); } _derivedValidators[typeof(TDerived)] = new ValidatorWrapper(derivedValidator, ruleSets); return(this); }
public void FailedValidateFiresWmiEvent() { ValidatorWrapper validator = new ValidatorWrapper(new MockValidator(true)); ValidationInstrumentationListener instrumentationListener = new ValidationInstrumentationListener(false, false, true); new ReflectionInstrumentationBinder().Bind(validator.GetInstrumentationEventProvider(), instrumentationListener); using (WmiEventWatcher eventWatcher = new WmiEventWatcher(1)) { validator.Validate(this); eventWatcher.WaitForEvents(); Assert.AreEqual(1, eventWatcher.EventsReceived.Count); Assert.AreEqual("ValidationFailedEvent", eventWatcher.EventsReceived[0].ClassPath.ClassName); } }