public virtual Task <IEnumerable <ValidationError> > Validate( IPropertyValidatorDefinition validatorDefinition, object value, CancellationToken ct) { return(Task.FromResult(ValidateSync(validatorDefinition, value))); }
protected override IEnumerable <ValidationError> ValidateSync(IPropertyValidatorDefinition validatorDefinition, object value) { if (value == null) { yield break; } var regex = (Regex)validatorDefinition.AdditionalProperties[PropertyRegex]; if (!regex.IsMatch(value.ToString())) { yield return(CreateError(validatorDefinition, $"{validatorDefinition.PropertyDefinition.Name} does not match the pattern ({validatorDefinition.AdditionalProperties[PropertyPattern]}).")); } }
protected override IEnumerable <ValidationError> ValidateSync(IPropertyValidatorDefinition validatorDefinition, object value) { if (value == null) { yield return(CreateError(validatorDefinition, $"{validatorDefinition.PropertyDefinition.Name} is required.")); } if (value is string) { if (value.Equals(string.Empty)) { yield return(CreateError(validatorDefinition, $"{validatorDefinition.PropertyDefinition.Name} is required.")); } } }
public override void ModelLoaded(IPropertyValidatorDefinition validatorDefinition) { var patternProperty = validatorDefinition.PropertyBag[PropertyPattern] as string; if (patternProperty == null) { throw new ModelLoadingException( $"When using regex validator, the {PropertyPattern} property is mandatory (on {validatorDefinition})."); } try { validatorDefinition.AdditionalProperties[PropertyPattern] = patternProperty; validatorDefinition.AdditionalProperties[PropertyRegex] = new Regex(patternProperty, RegexOptions.Compiled); } catch (ArgumentException ex) { throw new ModelLoadingException($"Error while creating regular expression: {ex.Message}", ex); } }
protected virtual ValidationError CreateError(IPropertyValidatorDefinition validatorDefinition, string defaultMessage) { return(new ValidationError(validatorDefinition.PropertyDefinition.Name, validatorDefinition.Message ?? defaultMessage)); }
protected abstract IEnumerable <ValidationError> ValidateSync(IPropertyValidatorDefinition validatorDefinition, object value);
public virtual void ModelLoaded(IPropertyValidatorDefinition validatorDefinition) { }