コード例 #1
0
        /// <summary>
        /// Runs the configured clauses and rules.
        /// </summary>
        /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        protected void Invoke(PropertyContext <TEntity, TProperty> context)
        {
            Check.NotNull(context, nameof(context));

            // Check all "this" clauses.
            foreach (var clause in _clauses)
            {
                if (!clause.Check(context))
                {
                    return;
                }
            }

            // Check and execute all rules/clauses within the rules stack.
            foreach (var rule in _rules)
            {
                if (rule.Check(context))
                {
                    rule.Validate(context);
                }

                // Stop validating after an error.
                if (context.HasError)
                {
                    break;
                }
            }
        }
コード例 #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);
        }
コード例 #3
0
        /// <summary>
        /// Validates the value.
        /// </summary>
        /// <param name="value">The value to validate.</param>
        /// <param name="name">The value name (defaults to <see cref="ValueValidator{T}.ValueNameDefault"/>).</param>
        /// <param name="text">The friendly text name used in validation messages (defaults to <paramref name="name"/> as sentence case where not specified).</param>
        /// <param name="throwOnError">Indicates to throw a <see cref="ValidationException"/> where an error was found.</param>
        /// <returns>A <see cref="ValueValidatorResult{TEntity, TProperty}"/>.</returns>
        public ValueValidatorResult <ValidationValue <T>, T> Validate(T value, string name = null, LText text = null, bool throwOnError = false)
        {
            var vv  = new ValidationValue <T>(null, value);
            var ctx = new PropertyContext <ValidationValue <T>, T>(new ValidationContext <ValidationValue <T> >(vv,
                                                                                                                new ValidationArgs()), value, name ?? ValueValidator <T> .ValueNameDefault, null, text);

            Invoke(ctx);
            var res = new ValueValidatorResult <ValidationValue <T>, T>(ctx);

            if (throwOnError)
            {
                res.ThrowOnError();
            }

            return(res);
        }
コード例 #4
0
        /// <summary>
        /// Perform more complex mandatory check based on the ReferenceData base ID type.
        /// </summary>
        private void ValidateId(PropertyContext <TEntity, object> context)
        {
            if (context.Value != null)
            {
                if (context.Parent.Value is ReferenceDataBaseInt && (int)context.Value != 0)
                {
                    return;
                }

                if (context.Parent.Value is ReferenceDataBaseGuid && (Guid)context.Value != Guid.Empty)
                {
                    return;
                }
            }

            context.CreateErrorMessage(ValidatorStrings.MandatoryFormat);
        }
コード例 #5
0
        /// <summary>
        /// Validates the value.
        /// </summary>
        /// <typeparam name="TEntity">The related entity <see cref="Type"/>.</typeparam>
        /// <param name="context">The related <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
        internal void Validate <TEntity>(PropertyContext <TEntity, T> context) where TEntity : class
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var vv = new ValidationValue <T>(context.Parent.Value, context.Value);
            var vc = new ValidationContext <ValidationValue <T> >(vv, new ValidationArgs
            {
                Config = context.Parent.Config,
                SelectedPropertyName         = context.Name,
                FullyQualifiedEntityName     = context.Parent.FullyQualifiedEntityName,
                FullyQualifiedJsonEntityName = context.Parent.FullyQualifiedJsonEntityName,
                UseJsonNames = context.UseJsonName
            });

            var ctx = new PropertyContext <ValidationValue <T>, T>(vc, context.Value, context.Name, context.JsonName, context.Text);

            Invoke(ctx);
            context.HasError = ctx.HasError;
            context.Parent.MergeResult(ctx.Parent.Messages);
        }
コード例 #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ValueValidatorResult{TEntity, TProperty}"/> class.
 /// </summary>
 /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
 public ValueValidatorResult(PropertyContext <TEntity, TProperty> context)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
 }
コード例 #7
0
 /// <summary>
 /// Validate the property value.
 /// </summary>
 /// <param name="context">The <see cref="PropertyContext{TEntity, TProperty}"/>.</param>
 void IValueRule <TEntity, TProperty> .Validate(PropertyContext <TEntity, TProperty> context)
 {
     // All good, nothing to see here ;-)
     throw new NotSupportedException("A property value validation should not occur directly on a PropertyRule.");
 }