Exemple #1
0
 public virtual Task <IEnumerable <ValidationError> > Validate(
     IPropertyValidatorDefinition validatorDefinition,
     object value,
     CancellationToken ct)
 {
     return(Task.FromResult(ValidateSync(validatorDefinition, value)));
 }
Exemple #2
0
        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]})."));
            }
        }
Exemple #3
0
        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."));
                }
            }
        }
Exemple #4
0
        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);
            }
        }
Exemple #5
0
 protected virtual ValidationError CreateError(IPropertyValidatorDefinition validatorDefinition, string defaultMessage)
 {
     return(new ValidationError(validatorDefinition.PropertyDefinition.Name, validatorDefinition.Message ?? defaultMessage));
 }
Exemple #6
0
 protected abstract IEnumerable <ValidationError> ValidateSync(IPropertyValidatorDefinition validatorDefinition, object value);
Exemple #7
0
 public virtual void ModelLoaded(IPropertyValidatorDefinition validatorDefinition)
 {
 }