public InvalidMessageTransformer(ConstraintValidatorContext constraintContext, List<InvalidValue> results, System.Type @class, string propertyName /* nullable */, object value /* nullable */, object entity /* nullable */, IValidator validator, DefaultMessageInterpolatorAggregator defaultInterpolator, IMessageInterpolator userInterpolator /* nullable */) { if (constraintContext == null) throw new ArgumentNullException("constraintContext"); if (results == null) throw new ArgumentNullException("results"); if (@class == null) throw new ArgumentNullException("class"); if (validator == null) throw new ArgumentNullException("valitor"); if (defaultInterpolator == null) throw new ArgumentNullException("defaultInterpolator"); this.constraintContext = constraintContext; this.results = results; this.@class = @class; this.propertyName = propertyName; this.value = value; this.entity = entity; this.validator = validator; this.defaultInterpolator = defaultInterpolator; this.userInterpolator = userInterpolator; }
public InvalidMessageTransformer(ConstraintValidatorContext constraintContext, ICollection<object> activeTags, System.Type @class, string propertyName, object value, object entity, ValidatorDef validatorDef, DefaultMessageInterpolatorAggregator defaultInterpolator, IMessageInterpolator userInterpolator) { if (constraintContext == null) { throw new ArgumentNullException("constraintContext"); } if (@class == null) { throw new ArgumentNullException("class"); } if (validatorDef == null) { throw new ArgumentNullException("validatorDef"); } if (defaultInterpolator == null) { throw new ArgumentNullException("defaultInterpolator"); } this.constraintContext = constraintContext; this.activeTags = activeTags; this.@class = @class; this.propertyName = propertyName; this.value = value; this.entity = entity; this.validatorDef = validatorDef; this.defaultInterpolator = defaultInterpolator; this.userInterpolator = userInterpolator; }
public void OnSetup() { c = new ConstraintValidatorContext("SomeProperty", "This is Default Message"); }
private List<InvalidValue> MembersValidation(object entity, string memberName) { //Property & Field Validation List<InvalidValue> results = new List<InvalidValue>(); int getterFound = 0; for (int i = 0; i < memberValidators.Count; i++) { MemberInfo member = memberGetters[i]; if (memberName == null || member.Name.Equals(memberName)) { getterFound++; if (NHibernateUtil.IsPropertyInitialized(entity, member.Name)) { object value = TypeUtils.GetMemberValue(entity, member); /* The implementation of NHibernateUtil.IsPropertyInitialized is not enough for us * because NH call it only for some kind of propeties and especially only when NH need this check. * We need to check if is itilialized its value to prevent the initialization of * lazy-properties and lazy-collections. */ if (NHibernateUtil.IsInitialized(value)) { IValidator validator = memberValidators[i]; var constraintContext = new ConstraintValidatorContext(member.Name, defaultInterpolator.GetAttributeMessage(validator)); if (!validator.IsValid(value, constraintContext)) { new InvalidMessageTransformer(constraintContext, results, entityType, member.Name, value, entity, validator, defaultInterpolator,userInterpolator).Transform(); } } } } } if (memberName != null && getterFound == 0 && TypeUtils.GetPropertyOrField(entityType, memberName) == null) { throw new TargetException( string.Format("The property or field '{0}' was not found in class {1}", memberName, entityType.FullName)); } return results; }
private InvalidValue[] GetInvalidValues(object entity, ISet circularityState) { if (entity == null || circularityState.Contains(entity)) { return EMPTY_INVALID_VALUE_ARRAY; //Avoid circularity } else { circularityState.Add(entity); } if (!entityType.IsInstanceOfType(entity)) { throw new ArgumentException("not an instance of: " + entity.GetType()); } List<InvalidValue> results = new List<InvalidValue>(); //Entity Validation foreach (IValidator validator in entityValidators) { var constraintContext = new ConstraintValidatorContext(null,defaultInterpolator.GetAttributeMessage(validator)); if (!validator.IsValid(entity, constraintContext)) { new InvalidMessageTransformer(constraintContext, results, entityType, null, entity, entity, validator, defaultInterpolator, userInterpolator).Transform(); } } results.AddRange(MembersValidation(entity, null)); //Child validation for (int i = 0; i < childGetters.Count; i++) { MemberInfo member = childGetters[i]; if (NHibernateUtil.IsPropertyInitialized(entity, member.Name)) { object value = TypeUtils.GetMemberValue(entity, member); if (value != null && NHibernateUtil.IsInitialized(value)) { MakeChildValidation(value, entity, member, circularityState, results); } } } return results.ToArray(); }
/// <summary> /// Apply constraints of a particular property value of a entity type and return all the failures. /// The InvalidValue objects returns return null for InvalidValue#Entity and InvalidValue#RootEntity. /// Note: this is not recursive. /// </summary> /// <param name="propertyName">Name of the property or field to validate</param> /// <param name="value">Real value to validate. Is not an entity instance.</param> /// <returns></returns> public InvalidValue[] GetPotentialInvalidValues(string propertyName, object value) { List<InvalidValue> results = new List<InvalidValue>(); int getterFound = 0; for (int i = 0; i < memberValidators.Count; i++) { MemberInfo getter = memberGetters[i]; if (getter.Name.Equals(propertyName)) { getterFound++; IValidator validator = memberValidators[i]; var constraintContext = new ConstraintValidatorContext(propertyName, defaultInterpolator.GetAttributeMessage(validator)); if (!validator.IsValid(value, null)) new InvalidMessageTransformer(constraintContext, results, entityType, propertyName, value, null, validator, defaultInterpolator, userInterpolator).Transform(); } } if (getterFound == 0 && TypeUtils.GetPropertyOrField(entityType, propertyName) == null) { throw new TargetException( string.Format("The property or field '{0}' was not found in class {1}", propertyName, entityType.FullName)); } return results.ToArray(); }