Exemplo n.º 1
0
        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="field">The field.</param>
        /// <param name="summary">The summary.</param>
        /// <returns></returns>
        public override bool IsValid(object value, string field, ref ValidationSummary summary)
        {
            if (value == null)
            {
                return(false);
            }

            String toValidate;

            if (!(value is String))
            {
                toValidate = value.ToString();
            }
            else
            {
                toValidate = (String)value;
            }

            bool result = true;

            Regex expression = new Regex(_regex, RegexOptions.None);

            if (!expression.IsMatch(toValidate))
            {
                ValidationEntry ve = new ValidationEntry(false, String.Format(_message, new object[] { field }), field, value);
                summary.Add(ve);
                result = false;
            }

            return(result);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Sets validator for upcoming side-effect
 /// </summary>
 /// <param name="assertions">Assertion delegate</param>
 /// <returns>Fluent</returns>
 public void Then <TCommand>(Action <TCommand> assertions = null) where TCommand : CommandBase
 {
     _current.CommandType = typeof(TCommand);
     _current.Assertion   = assertions;
     _validationEntries.Enqueue(_current);
     _current = new ValidationEntry(null, null);
 }
        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="field">The field.</param>
        /// <param name="summary">The summary.</param>
        /// <returns></returns>
        public override bool IsValid(object value, string field, ref ValidationSummary summary)
        {
            if (value == null)
            {
                return(false);
            }

            if (!(value is String))
            {
                return(false);
            }

            String s      = (String)value;
            bool   result = true;

            if (s.Length < _minLen)
            {
                ValidationEntry ve = new ValidationEntry(false, String.Format(Resources.MinLengthFallenBelow, new object[] { _minLen, field }), field, value);
                summary.Add(ve);
                result = false;
            }

            if (s.Length > _maxLen)
            {
                ValidationEntry ve = new ValidationEntry(false, String.Format(Resources.MaxLengthExceeded, new object[] { _maxLen, field }), field, value);
                summary.Add(ve);
                result = false;
            }

            return(result);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Sets validator for upcoming side-effect
 /// </summary>
 /// <param name="assertions">Set of assertions that must take place for upcoming side-effect</param>
 /// <returns>Fluent</returns>
 public void Then <TCommand>(params ICommandCheck <TCommand>[] assertions) where TCommand : CommandBase
 {
     _current.CommandType = typeof(TCommand);
     _current.Assertions  = assertions;
     _validationEntries.Enqueue(_current);
     _current = new ValidationEntry(null, null);
 }
        /// <inheritdoc />
        public bool ShouldValidateEntry(ValidationEntry entry, ValidationEntry parentEntry)
        {
            var request = _serviceProvider.GetRequiredService <IJsonApiRequest>();

            if (IsId(entry.Key))
            {
                return(true);
            }

            bool isTopResourceInPrimaryRequest = string.IsNullOrEmpty(parentEntry.Key) && IsAtPrimaryEndpoint(request);

            if (!isTopResourceInPrimaryRequest)
            {
                return(false);
            }

            var httpContextAccessor = _serviceProvider.GetRequiredService <IHttpContextAccessor>();

            if (httpContextAccessor.HttpContext.Request.Method == HttpMethods.Patch || request.OperationKind == OperationKind.UpdateResource)
            {
                var targetedFields = _serviceProvider.GetRequiredService <ITargetedFields>();
                return(IsFieldTargeted(entry, targetedFields));
            }

            return(true);
        }
Exemplo n.º 6
0
        private static void DoPart2(Validation validation, Ticket myTicket, List <Ticket> nearbyTickets, List <Ticket> allValidTickets)
        {
            var ticketColumns = GetTicketColumns(allValidTickets);

            //Find which condition satisfies each column:
            var validationsInOrder = new ValidationEntry[ticketColumns.Count];

            for (int ii = 0; ii < ticketColumns.Count; ii++)
            {
                if (validationsInOrder[ii] != null)
                {
                    continue;
                }
                List <int> column          = ticketColumns[ii];
                var        validationEntry = validation.validationEntries[0];
                var        numValidEntries = 0;
                foreach (var entry in validation.validationEntries)
                {
                    if (entry.AreValid(column))
                    {
                        validationEntry = entry;
                        numValidEntries++;
                    }
                }
                if (numValidEntries == 1)
                {
                    validationsInOrder[ii] = validationEntry;
                    ii = 0;
                }
            }

            Console.WriteLine(string.Join(",", validationsInOrder.Select(entry => entry.Name)));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="field">The field.</param>
        /// <param name="summary">The summary.</param>
        /// <returns></returns>
        public override bool IsValid(object value, string field, ref ValidationSummary summary)
        {
            if (value == null)
            {
                ValidationEntry ve = new ValidationEntry(false, String.Format(_message, new object[] { Alias ?? field }), field, null);
                summary.Add(ve);
                return(false);
            }

            return(true);
        }
Exemplo n.º 8
0
        private static void DoPart2_2(Validation validation, Ticket myTicket, List <Ticket> nearbyTickets, List <Ticket> allValidTickets)
        {
            var ticketColumns = GetTicketColumns(allValidTickets);

            //Find which condition fails to satisfy each column:
            var columnsToValidationEntries = new Dictionary <int, List <ValidationEntry> >();

            for (int ii = 0; ii < ticketColumns.Count; ii++)
            {
                List <int> column = ticketColumns[ii];
                foreach (var entry in validation.validationEntries)
                {
                    if (entry.AreValid(column))
                    {
                        if (!columnsToValidationEntries.ContainsKey(ii))
                        {
                            columnsToValidationEntries[ii] = new List <ValidationEntry>();
                        }
                        columnsToValidationEntries[ii].Add(entry);
                    }
                }
            }

            var validationsInOrder = new ValidationEntry[ticketColumns.Count];

            for (int ii = 0; ii < ticketColumns.Count; ii++)
            {
                var validationEntries = columnsToValidationEntries[ii];
                if (validationEntries.Count == 1)
                {
                    //Found something:
                    validationsInOrder[ii] = validationEntries[0];
                    //Remove every one of the same validator:
                    foreach (var keyPairs in columnsToValidationEntries)
                    {
                        keyPairs.Value.Remove(validationsInOrder[ii]);
                    }
                }
            }


            for (int ii = 0; ii < validationsInOrder.Length; ii++)
            {
                var validationEntry = validationsInOrder[ii];
                if (validationEntry == null)
                {
                    Console.WriteLine("NULL!");
                    continue;
                }
                Console.WriteLine($"{validationEntry.Name}");
            }
        }
        public bool ShouldValidateEntry(ValidationEntry entry, ValidationEntry parentEntry)
        {
            if (string.IsNullOrWhiteSpace(OtherProperty))
            {
                return(true);
            }

            var prop =
                parentEntry
                .Metadata
                .Properties[OtherProperty]?
                .PropertyGetter?
                .Invoke(parentEntry.Model);

            return(prop == OtherValue);
        }
        private bool VisitChildren(IValidationStrategy strategy)
        {
            var isValid     = true;
            var enumerator  = strategy.GetChildren(_metadata, _key, _model);
            var parentEntry = new ValidationEntry(_metadata, _key, _model);

            while (enumerator.MoveNext())
            {
                var entry    = enumerator.Current;
                var metadata = entry.Metadata;
                var key      = entry.Key;

                isValid &= Visit(metadata, key, entry.Model);
            }

            return(isValid);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="field">The field.</param>
        /// <param name="summary">The summary.</param>
        public override bool IsValid(object value, string field, ref ValidationSummary summary)
        {
            string   dayText = value as string;
            DateTime day     = DateTime.Now;

            if (DateTime.TryParse(dayText, out day))
            {
                int result = DateTime.Compare(day, DateTime.Now);
                if (result < 0)
                {
                    ValidationEntry ve = new ValidationEntry(false, String.Format(Resources.NoValidDate, new[] { field }), field, value);
                    summary.Add(ve);

                    return(false);
                }
                return(true);
            }

            return(false);
        }
Exemplo n.º 12
0
        protected virtual bool VisitChildren(IValidationStrategy strategy)
        {
            var isValid     = true;
            var enumerator  = strategy.GetChildren(Metadata, Key, Model);
            var parentEntry = new ValidationEntry(Metadata, Key, Model);

            while (enumerator.MoveNext())
            {
                var entry    = enumerator.Current;
                var metadata = entry.Metadata;
                var key      = entry.Key;
                if (metadata.PropertyValidationFilter?.ShouldValidateEntry(entry, parentEntry) == false)
                {
                    SuppressValidation(key);
                    continue;
                }

                isValid &= Visit(metadata, key, entry.Model);
            }

            return(isValid);
        }
Exemplo n.º 13
0
        private bool VisitChildren(IValidationStrategy strategy)
        {
            var isValid     = true;
            var enumerator  = strategy.GetChildren(_metadata, _key, _model);
            var parentEntry = new ValidationEntry(_metadata, _key, _model);

            while (enumerator.MoveNext())
            {
                var entry    = enumerator.Current;
                var metadata = entry.Metadata;
                var key      = entry.Key;
                //TODO: Re-introduce this after next .net fwk update.
//				if (metadata.PropertyValidationFilter?.ShouldValidateEntry(entry, parentEntry) == false) {
//					SuppressValidation(key);
//					continue;
//				}

                isValid &= Visit(metadata, key, entry.Model);
            }

            return(isValid);
        }
Exemplo n.º 14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="parentEntry"></param>
 /// <returns></returns>
 public bool ShouldValidateEntry(ValidationEntry entry, ValidationEntry parentEntry)
 {
     return(false);
 }
 private static bool IsFieldTargeted(ValidationEntry entry, ITargetedFields targetedFields)
 {
     return(targetedFields.Attributes.Any(attribute => attribute.Property.Name == entry.Key));
 }
Exemplo n.º 16
0
        /// <summary>
        /// Determines whether the specified value is valid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="field">The field.</param>
        /// <param name="summary">The summary.</param>
        /// <returns></returns>
        public override bool IsValid(object value, string field, ref ValidationSummary summary)
        {
            if (value == null)
            {
                return(false);
            }

            if (!(value is IComparable))
            {
                return(false);
            }

            IComparable toValidate = (IComparable)value;

            bool   isValid = true;
            string message = string.Empty;

            switch (_restriction)
            {
            case Restriction.Less:
                if (toValidate.CompareTo(_value) >= 0)
                {
                    isValid = false;
                    message = Resources.NumberValidatorLess;
                }
                break;

            case Restriction.LessOrEqual:
                if (toValidate.CompareTo(_value) > 0)
                {
                    isValid = false;
                    message = Resources.NumberValidatorLessEqual;
                }
                break;

            case Restriction.Equal:
                if (toValidate.CompareTo(_value) != 0)
                {
                    isValid = false;
                    message = Resources.NumberValidatorEqual;
                }
                break;

            case Restriction.Greater:
                if (toValidate.CompareTo(_value) <= 0)
                {
                    isValid = false;
                    message = Resources.NumberValidatorGreater;
                }
                break;

            case Restriction.GreaterOrEqual:
                if (toValidate.CompareTo(_value) < 0)
                {
                    isValid = false;
                    message = Resources.NumberValidatorGreaterEqual;
                }
                break;
            }

            if (isValid == false)
            {
                ValidationEntry ve = new ValidationEntry(false, String.Format(message, new[] { field, _value }), field, _value);
                summary.Add(ve);
            }
            return(isValid);
        }
Exemplo n.º 17
0
        private static void DoPart2_3(Validation validation, Ticket myTicket, List <Ticket> nearbyTickets, List <Ticket> allValidTickets)
        {
            var ticketColumns = GetTicketColumns(allValidTickets);
            //Prepopulate
            var columnsToValidationEntries = new Dictionary <int, List <ValidationEntry> >();

            for (int ii = 0; ii < ticketColumns.Count; ii++)
            {
                columnsToValidationEntries[ii] = new List <ValidationEntry>();
                columnsToValidationEntries[ii].AddRange(validation.validationEntries);
            }

            //Find which condition fails to satisfy each column:
            for (int ii = 0; ii < ticketColumns.Count; ii++)
            {
                List <int> column = ticketColumns[ii];
                var        validValidationEntries = new List <ValidationEntry>();
                for (int jj = 0; jj < columnsToValidationEntries[ii].Count; jj++)
                {
                    var entry = columnsToValidationEntries[ii][jj];
                    if (entry.AreValid(column))
                    {
                        validValidationEntries.Add(entry);
                    }
                }
                columnsToValidationEntries[ii] = validValidationEntries;
            }

            var orderedColumnsToValidationEntries = columnsToValidationEntries.OrderBy(kvp => kvp.Value.Count);
            var validationsInOrder = new ValidationEntry[ticketColumns.Count];

            while (orderedColumnsToValidationEntries.Count() > 0)
            {
                var lowestValidationEntry = orderedColumnsToValidationEntries.First(); //There should be more checking that there's only one item, but i'm tired.
                validationsInOrder[lowestValidationEntry.Key] = lowestValidationEntry.Value.First(entry => !validationsInOrder.Contains(entry));

                columnsToValidationEntries.Remove(lowestValidationEntry.Key);
                foreach (var item in columnsToValidationEntries)
                {
                    item.Value.Remove(validationsInOrder[lowestValidationEntry.Key]);
                }
                orderedColumnsToValidationEntries = columnsToValidationEntries.OrderBy(kvp => kvp.Value.Count);
            }


            long departureProduct = 1;

            for (int ii = 0; ii < validationsInOrder.Length; ii++)
            {
                var validationEntry = validationsInOrder[ii];
                if (validationEntry == null)
                {
                    Console.WriteLine("NULL!");
                    continue;
                }
                Console.WriteLine($"{validationEntry.Name}: {myTicket.entries[ii]}");
                if (validationEntry.Name.StartsWith("departure"))
                {
                    departureProduct *= (long)myTicket.entries[ii];
                }
            }
            Console.WriteLine($"Product: {departureProduct}");
        }