private PropertyValidator <T, TProperty> registerValidator <TProperty>(Expression <Func <T, TProperty> > expression) { var propertyValidator = new PropertyValidator <T, TProperty>(expression); PropertyValidators.Add(propertyValidator); return(propertyValidator); }
/// <summary> /// Ensures that when validating an instance of T, that another specification also be enforced. /// </summary> /// <typeparam name="U">The type of the object the specification to enforce is validating.</typeparam> /// <typeparam name="TSpecType">The type of the specification to enforce.</typeparam> public void Using <U, TSpecType>() where TSpecType : Validates <U>, new() { //Get the PropertyValidators from the Using Spec and add it to this specification var usingSpec = ValidationCatalog.SpecificationContainer.GetAllSpecifications().First(x => x.GetType() == typeof(TSpecType)); PropertyValidators.AddRange(usingSpec.PropertyValidators); }
/// <summary> /// Validates an instance of T against the rules defined in the specification and a SpecificationContainer. /// </summary> /// <remarks> /// This is useful when using a specification leverages other specifications for referenced types. The referenced /// types will be validated against their specifications contained in the SpecificationContainer. /// </remarks> /// <param name="instance">Instance of T to validate.</param> /// <param name="specificationContainer">The <see cref="SpecificationContainer"/></param> /// <returns><see cref="ValidationNotification"/></returns> public ValidationNotification Validate(T instance, SpecificationContainer specificationContainer) { lock (this) { var notification = new ValidationNotification(); PropertyValidators.Select(x => x.Validate(instance, specificationContainer, notification)); return(notification); } }
public AsyncPropertyValidator <TProp> SetupAsync <TProp>(Expression <Func <TObject, TProp> > expression) { var memberInfo = PropertyExtractor.Extract(expression); if (PropertyValidators.TryGetValue(memberInfo.Name, out var propertyValidator)) { if (propertyValidator is AsyncPropertyValidator <TProp> asyncPropertyValidator) { return(asyncPropertyValidator); } throw new InvalidOperationException("Member already has assigned synchronous validator"); } var newPropertyValidator = new AsyncPropertyValidator <TProp>(memberInfo); PropertyValidators.Add(memberInfo.Name, newPropertyValidator); return(newPropertyValidator); }
public void Setup() { PropertyValidators.Clear(); }