コード例 #1
0
        /// <summary>
        /// Source: https://stackoverflow.com/questions/2167811/unit-testing-asp-net-dataannotations-validation
        /// Source2: https://github.com/ovation22/DataAnnotationsValidatorRecursive
        /// Validates an object based on the data annotations set on the object's properties and returns the validation result.
        /// </summary>
        /// <param name="model">The object to validate</param>
        /// <returns>The validation result for the object.</returns>
        public static IList <ValidationResult> ValidateModel(object model)
        {
            var validator         = new DataAnnotationsValidator.DataAnnotationsValidator();
            var validationResults = new List <ValidationResult>();

            validator.TryValidateObjectRecursive(model, validationResults);


            return(validationResults);
        }
コード例 #2
0
        private static IEnumerable <string> ValidationErrors(object obj)
        {
            var results   = new List <ValidationResult>();
            var validator = new DataAnnotationsValidator.DataAnnotationsValidator();

            if (!validator.TryValidateObjectRecursive(obj, results) && !results.Any())
            {
                yield return("Object validation failed");
            }

            foreach (var validationResult in results)
            {
                yield return(validationResult.ErrorMessage);
            }
        }
コード例 #3
0
ファイル: ToscaValidator.cs プロジェクト: noamwegner/Toscana
        public bool TryValidateRecursively(T toscaObject, out List <ValidationResult> validationResults)
        {
            validationResults = new List <ValidationResult>();
            var dataAnnotationsValidator = new DataAnnotationsValidator.DataAnnotationsValidator();

            try
            {
                dataAnnotationsValidator.TryValidateObjectRecursive(toscaObject, validationResults);
                validationResults = validationResults.Distinct(new ValidationResultEqualityComparer()).ToList();
            }
            catch (TargetInvocationException targetInvocationException)
            {
                validationResults.Add(new ValidationResult(targetInvocationException.InnerException.Message));
            }
            return(!validationResults.Any());
        }
コード例 #4
0
        private void ValidateRequest(HttpListenerContext context)
        {
            var clientVersion = context.Request.Headers[GigyaHttpHeaders.ProtocolVersion];

            if (clientVersion != null && clientVersion != HttpServiceRequest.ProtocolVersion)
            {
                _failureCounter.Increment("ProtocolVersionMismatch");
                throw new RequestException($"Client protocol version {clientVersion} is not supported by the server protocol version {HttpServiceRequest.ProtocolVersion}.");
            }

            if (context.Request.HttpMethod != "POST")
            {
                context.Response.Headers.Add("Allow", "POST");
                _failureCounter.Increment("NonPostRequest");
                throw new RequestException("Only POST calls are allowed.");
            }

            if (context.Request.ContentType == null || context.Request.ContentType.StartsWith("application/json") == false)
            {
                context.Response.Headers.Add("Accept", "application/json");
                _failureCounter.Increment("NonJsonRequest");
                throw new RequestException("Only requests with content type 'application/json' are supported.");
            }

            if (context.Request.ContentLength64 == 0)
            {
                _failureCounter.Increment("EmptyRequest");
                throw new RequestException("Only requests with content are supported.");
            }

            var errors = new List <ValidationResult>();

            if (!_validator.TryValidateObjectRecursive(context.Request, errors))
            {
                _failureCounter.Increment("InvalidRequestFormat");
                throw new RequestException("Invalid request format: " + string.Join("\n", errors.Select(a => a.MemberNames + ": " + a.ErrorMessage)));
            }
        }