Ejemplo n.º 1
0
        private ValidationResults ValidateCreateRequest(int year, int month, CreateMutationRequest request)
        {
            ValidationResults results = new ValidationResults();
            ObjectId          parsedCategoryId;

            if (year <= 0)
            {
                results.AddError("year", "The year parameter must be greater or equal to zero.");
            }

            if (month < 1 || month > 12)
            {
                results.AddError("month", "The month parameter must be between 1 and 12.");
            }

            if (string.IsNullOrWhiteSpace(request.Description))
            {
                results.AddError("description", "Please provide a description for your mutation");
            }

            if (!ObjectId.TryParse(request.Category, out parsedCategoryId))
            {
                results.AddError("category", "The provided category ID is invalid. Please provide a valid Object ID");
            }

            return(results);
        }
Ejemplo n.º 2
0
        private ValidationResults ValidateCreateRequest(CreateCategoryRequest request)
        {
            ValidationResults results = new ValidationResults();

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                results.AddError("description", "The provided description is empty." +
                                 "Please provide a valid description");
            }

            if (request.Max < 0)
            {
                results.AddError("max", "The maximum for a category must be greater or equal to zero");
            }

            return(results);
        }
Ejemplo n.º 3
0
        private ValidationResults ValidateFindByIdRequest(string id)
        {
            ValidationResults results = new ValidationResults();
            ObjectId          parsedId;

            if (!ObjectId.TryParse(id, out parsedId))
            {
                results.AddError("id", "ID could not be parsed as a " +
                                 "valid ObjectID. Please supply a valid ID");
            }

            return(results);
        }
Ejemplo n.º 4
0
        public void AddError_BlankInstance_StoresMessageAndSetsSeverityToError()
        {
            // arrange
            var result  = new ValidationResults();
            var message = "someMessage";

            // act
            result.AddError(message);

            // assert
            result.Severity.Should().Be(Severity.Error);
            result.Errors.Should().ContainSingle(i => i == message);
            result.Warnings.Should().BeEmpty();
            result.Information.Should().BeEmpty();
        }
Ejemplo n.º 5
0
        private ValidationResults ValidateUpdateRequest(string id, UpdateCategoryRequest request)
        {
            ValidationResults results = new ValidationResults();
            ObjectId          parsedId;

            if (!ObjectId.TryParse(id, out parsedId))
            {
                results.AddError("id", "ID could not be parsed as a " +
                                 "valid ObjectID. Please supply a valid ID");
            }

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                results.AddError("description", "The provided description is empty." +
                                 "Please provide a valid description");
            }

            if (request.Max < 0)
            {
                results.AddError("max", "The maximum for a category must be greater or equal to zero");
            }

            return(results);
        }
Ejemplo n.º 6
0
        public void AddInformation_InstanceWithError_StoresMessageButLeavesSeverity()
        {
            // arrange
            var result  = new ValidationResults();
            var message = "someMessage";

            result.AddError("someErrorMessage");

            // act
            result.AddInformation(message);

            // assert
            result.Severity.Should().Be(Severity.Error);
            result.Errors.Should().ContainSingle();
            result.Warnings.Should().BeEmpty();
            result.Information.Should().ContainSingle(i => i == message);
        }
        Boolean IsCommandValid(HttpRequestMessage request, Object command, out HttpResponseMessage result)
        {
            logger.Debug("Loading command validators.");
            var validators        = this.validatorsProvider.GetValidators(command);
            var validationResults = new ValidationResults();

            try
            {
                foreach (var item in validators)
                {
                    logger.Debug("Querying validation interceptor: {0}.", item);

                    var validationResult = item.Validate(command);
                    if (!validationResult.IsValid)
                    {
                        logger.Debug("Validation failed.");

                        validationResult.Errors.ForEach(e => validationResults.AddError(e));
                    }
                    else
                    {
                        logger.Debug("Validation successful.");
                    }
                }
            }
            finally
            {
                logger.Debug("Releasing command validators.");
                this.validatorsProvider.Release(validators);
                logger.Debug("Validation handling completed.");
            }

            if (!validationResults.IsValid)
            {
                var data = new ModelStateDictionary();
                validationResults.Errors.ForEach(e => data.AddModelError(e.Key, e.ToString()));

                result = request.CreateErrorResponse(HttpStatusCode.BadRequest, data);
                return(false);
            }

            result = null;
            return(true);
        }
Ejemplo n.º 8
0
        Boolean IsCommandValid( HttpRequestMessage request, Object command, out HttpResponseMessage result )
        {
            logger.Debug( "Loading command validators." );
            var validators = this.validatorsProvider.GetValidators( command );
            var validationResults = new ValidationResults();
            try
            {
                foreach ( var item in validators )
                {
                    logger.Debug( "Querying validation interceptor: {0}.", item );

                    var validationResult = item.Validate( command );
                    if ( !validationResult.IsValid )
                    {
                        logger.Debug( "Validation failed." );

                        validationResult.Errors.ForEach( e => validationResults.AddError( e ) );
                    }
                    else
                    {
                        logger.Debug( "Validation successful." );
                    }
                }
            }
            finally
            {
                logger.Debug( "Releasing command validators." );
                this.validatorsProvider.Release( validators );
                logger.Debug( "Validation handling completed." );
            }

            if ( !validationResults.IsValid )
            {
                var data = new ModelStateDictionary();
                validationResults.Errors.ForEach( e => data.AddModelError( e.Key, e.ToString() ) );

                result = request.CreateErrorResponse( HttpStatusCode.BadRequest, data );
                return false;
            }

            result = null;
            return true;
        }