Exemplo n.º 1
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override Task ValidateAsync(PropertyContext <TEntity, TProperty> context)
        {
            // Do not validate where the compare to property has an error.
            var ctx = Beef.Check.NotNull(context, nameof(context));

            if (ctx.Parent.HasError(_compareTo))
            {
                return(Task.CompletedTask);
            }

            // Convert type and compare values.
            try
            {
                var compareToValue = (TProperty)(object)_compareTo.GetValue(context.Parent.Value) !;
                if (!Compare(ctx.Value !, compareToValue))
                {
                    CreateErrorMessage(context, _compareToText ?? _compareTo.Text);
                }

                return(Task.CompletedTask);
            }
            catch (InvalidCastException icex)
            {
                throw new InvalidCastException($"Property '{_compareTo.Name}' and '{ctx.Name}' are incompatible: {icex.Message}", icex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Validates an entity given a <see cref="ValidationContext{TEntity}"/>.
        /// </summary>
        /// <param name="context">The <see cref="ValidationContext{TEntity}"/>.</param>
        public void Validate(ValidationContext <TEntity> context)
        {
            Check.NotNull(context, nameof(context));

            if (context.Value == null)
            {
                return;
            }

            // Where validating a specific property then make sure the names match.
            if (context.SelectedPropertyName != null && context.SelectedPropertyName != Name)
            {
                return;
            }

            // Ensure that the property does not already have an error.
            if (context.HasError(_property))
            {
                return;
            }

            // Get the property value and create the property context.
            var value = _property.GetValue(context.Value);
            var ctx   = new PropertyContext <TEntity, TProperty>(context, value, this.Name, this.JsonName, this.Text);

            // Run the rules.
            Invoke(ctx);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, TProperty> context)
        {
            // Do not validate where the compare to property has an error.
            Beef.Check.NotNull(context, nameof(context));
            if (context.Parent.HasError(_compareTo))
            {
                return;
            }

            // Convert type and compare values.
            try
            {
                var compareToValue = (TProperty)(object)_compareTo.GetValue(context.Parent.Value);
#pragma warning disable CA1062 // Validate arguments of public methods; by-design, null check already performed.
                if (!Compare(context.Value, compareToValue))
                {
                    CreateErrorMessage(context, _compareToText ?? _compareTo.Text);
                }
#pragma warning restore CA1062
            }
            catch (InvalidCastException icex)
            {
                throw new InvalidCastException($"Property '{_compareTo.Name}' and '{context.Name}' are incompatible: {icex.Message}", icex);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Checks the clause.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        /// <returns><c>true</c> where validation is to continue; otherwise, <c>false</c> to stop.</returns>
        public bool Check(IPropertyContext context)
        {
            // Do not continue where the depends on property is in error.
            if (context.Parent.HasError(context.CreateFullyQualifiedPropertyName(_dependsOn.Name)))
            {
                return(false);
            }

            // Check depends on value to continue.
            object value = _dependsOn.GetValue((TEntity)context.Parent.Value);

            return(!(System.Collections.Comparer.Default.Compare(value, default(TProperty)) == 0));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Validate the property value.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        public override void Validate(PropertyContext <TEntity, TProperty> context)
        {
            // Do not validate where the compare to property has an error.
            if (context.Parent.HasError(_compareTo))
            {
                return;
            }

            // Convert type and compare values.
            try
            {
                var compareToValue = (TProperty)(object)_compareTo.GetValue(context.Parent.Value);
                if (!Compare(context.Value, compareToValue))
                {
                    CreateErrorMessage(context, _compareToText ?? _compareTo.Text);
                }
            }
            catch (InvalidCastException icex)
            {
                throw new InvalidCastException($"Property '{_compareTo.Name}' and '{context.Name}' are incompatible: {icex.Message}", icex);
            }
        }
Exemplo n.º 6
0
 public string this[string propertyName] =>
 (string)PropertyExpression <StyleMap> .GetValue(this,
                                                 propertyName.First().ToString().ToUpper() + propertyName.Substring(1).ToLower());
Exemplo n.º 7
0
 /// <summary>
 /// Gets the friendly text and value for a property expression.
 /// </summary>
 private object[] GetTextAndValue <TProperty>(PropertyExpression <TEntity, TProperty> propertyExpression)
 {
     return(new object[] { propertyExpression.Text, propertyExpression.GetValue(Value) });
 }