示例#1
0
        public override IEnumerable <ValidationMessage> GetValidationMessages(Dictionary <string, Dictionary <string, Operation> > entity, RuleContext context)
        {
            // get all operation objects that are either of get or post type
            var serviceDefinition = (ServiceDefinition)context.Root;
            var listOperations    = entity.Values.SelectMany(opDict => opDict.Where(pair => pair.Key.ToLower().Equals("get") || pair.Key.ToLower().Equals("post")));

            foreach (var opPair in listOperations)
            {
                // if the operation id is already of the type _list we can skip this check
                if (ListRegex.IsMatch(opPair.Value.OperationId))
                {
                    continue;
                }

                var violatingPath = ValidationUtilities.GetOperationIdPath(opPair.Value.OperationId, entity).Key;
                if (ValidationUtilities.IsXmsPageableResponseOperation(opPair.Value))
                {
                    yield return(new ValidationMessage(new FileObjectPath(context.File, context.Path.AppendProperty(violatingPath).AppendProperty(opPair.Key).AppendProperty("operationId")), this, opPair.Value.OperationId, XmsPageableViolation));
                }
                else if (ValidationUtilities.IsArrayTypeResponseOperation(opPair.Value, serviceDefinition))
                {
                    yield return(new ValidationMessage(new FileObjectPath(context.File, context.Path.AppendProperty(violatingPath).AppendProperty(opPair.Key).AppendProperty("operationId")), this, opPair.Value.OperationId, ArrayTypeViolation));
                }
            }
        }
        public override IEnumerable <ValidationMessage> GetValidationMessages(Dictionary <string, Dictionary <string, Operation> > entity, RuleContext context)
        {
            // get all operation objects that are either of get or post type
            var serviceDefinition = (ServiceDefinition)context.Root;

            foreach (var pathObj in entity)
            {
                var listOpIds = pathObj.Value
                                // operation should be a get or post
                                .Where(pair => (pair.Key.ToLower().Equals("get") || pair.Key.ToLower().Equals("post")))
                                // operation id should be of the form *_list(by)
                                .Where(pair => (ListByRegex.IsMatch(pair.Value.OperationId) || pair.Value.OperationId.ToLower().EndsWith("_list")))
                                // operation is xmspageable or returns an array
                                .Where(pair => (ValidationUtilities.IsArrayTypeResponseOperation(pair.Value, serviceDefinition)) ||
                                       (ValidationUtilities.IsXmsPageableResponseOperation(pair.Value)))
                                // select the operation id
                                .Select(pair => pair.Value.OperationId);

                // if there are no operations matching our conditions, skip
                if (IsNullOrEmpty(listOpIds))
                {
                    continue;
                }

                // populate valid list operation names for given path
                List <string> opNames = GetValidListOpNames(pathObj.Key);

                // if url does not match any of the predefined regexes, skip
                if (opNames == null || opNames.Count == 0)
                {
                    continue;
                }

                // find if there are any operations that violate the rule
                var errOpIds = listOpIds.Where(opId => !opNames.Contains(opId.ToLower()));

                // no violations found, skip
                if (IsNullOrEmpty(errOpIds))
                {
                    continue;
                }
                // aggregate suggested op names in a single readable string for the formatter
                var suggestedNames = string.Join(", ", opNames);
                foreach (var errOpId in errOpIds)
                {
                    yield return(new ValidationMessage(new FileObjectPath(context.File, context.Path), this, errOpId, suggestedNames));
                }
            }
        }