Beispiel #1
0
        public void Transform(TransformContext context)
        {
            if (!(context.Attribute is RequiredAttribute required))
            {
                return;
            }
            context.Handled = true;
            var memberType = context.DataTransferObjectMember.GetMemberType();

            context.Metadata = new ValidationMetadata((descriptor, _, rule) =>
            {
                var result = TransformHelper.ApplyNotNullRule(context.DataTransferObjectType, memberType, rule);
                if (!required.AllowEmptyStrings && memberType == typeof(string))
                {
                    result = TransformHelper.ApplyNotEmptyRule(context.DataTransferObjectType, memberType, result);
                }

                if (!string.IsNullOrEmpty(required.ErrorMessage))
                {
                    result = TransformHelper.ApplyMessage(context.DataTransferObjectType, memberType, result, required.ErrorMessage);
                }

                result = TransformHelper.ApplyErrorCode(context.DataTransferObjectType, memberType, result, "require");

                return(result);
            }, (descriptor, _) => new MetadataInfo("require", !required.AllowEmptyStrings ? new Dictionary <string, object> {
                ["notEmpty"] = true
            } : null));
        }
Beispiel #2
0
        public void Transform(TransformContext context)
        {
            if (!(context.Attribute is StringLengthAttribute stringLength))
            {
                return;
            }
            var memberType = context.DataTransferObjectMember.GetMemberType();

            if (memberType != typeof(string))
            {
                return;
            }
            context.Handled = true;

            context.Metadata = new ValidationMetadata((descriptor, _, rule) =>
            {
                var result = TransformHelper.ApplyLengthRule(context.DataTransferObjectType, rule, stringLength.MinimumLength,
                                                             stringLength.MaximumLength);

                if (!string.IsNullOrEmpty(stringLength.ErrorMessage))
                {
                    result = TransformHelper.ApplyMessage(context.DataTransferObjectType, memberType, result, stringLength.ErrorMessage);
                }

                result = TransformHelper.ApplyErrorCode(context.DataTransferObjectType, memberType, result, "string_length");

                return(result);
            }, (descriptor, _) => new MetadataInfo("string_length", new Dictionary <string, object> {
                ["maxLength"] = stringLength.MaximumLength, ["minLength"] = stringLength.MinimumLength
            }));
        }
Beispiel #3
0
        public void Transform(TransformContext context)
        {
            if (!(context.Attribute is EmailAddressAttribute emailAddress))
            {
                return;
            }
            var memberType = context.DataTransferObjectMember.GetMemberType();

            if (memberType != typeof(string))
            {
                return;
            }
            context.Handled = true;

            context.Metadata = new ValidationMetadata((descriptor, _, rule) =>
            {
                var result = TransformHelper.ApplyEmailAddressRule(context.DataTransferObjectType, rule);

                if (!string.IsNullOrEmpty(emailAddress.ErrorMessage))
                {
                    result = TransformHelper.ApplyMessage(context.DataTransferObjectType, memberType, result, emailAddress.ErrorMessage);
                }

                result = TransformHelper.ApplyErrorCode(context.DataTransferObjectType, memberType, result, "email_address");

                return(result);
            }, (descriptor, _) => new MetadataInfo("email_address", null));
        }
Beispiel #4
0
        public void Transform(TransformContext context)
        {
            if (!(context.Attribute is CreditCardAttribute creditCard))
            {
                return;
            }
            var memberType = context.DataTransferObjectMember.GetMemberType();

            if (memberType != typeof(string))
            {
                return;
            }
            context.Handled = true;

            context.Metadata = new ValidationMetadata((descriptor, _, rule) =>
            {
                var result = TransformHelper.ApplyCreditCardRule(context.DataTransferObjectType, rule);

                if (!string.IsNullOrEmpty(creditCard.ErrorMessage))
                {
                    result = TransformHelper.ApplyMessage(context.DataTransferObjectType, memberType, result, creditCard.ErrorMessage);
                }

                result = TransformHelper.ApplyErrorCode(context.DataTransferObjectType, memberType, result, "credit_card");

                return(result);
            }, (descriptor, _) => new MetadataInfo("credit_card", null));
        }
Beispiel #5
0
        public void Transform(TransformContext context)
        {
            if (!(context.Attribute is RangeAttribute range))
            {
                return;
            }
            context.Handled = true;
            var memberType      = context.DataTransferObjectMember.GetMemberType();
            var notNullableType = Nullable.GetUnderlyingType(memberType) ?? memberType;
            var min             = Convert.ChangeType(range.Minimum, notNullableType);
            var max             = Convert.ChangeType(range.Maximum, notNullableType);

            context.Metadata = new ValidationMetadata((descriptor, _, rule) =>
            {
                var result = TransformHelper.ApplyInclusiveBetweenRule(context.DataTransferObjectType, notNullableType, rule, min, max);

                if (!string.IsNullOrEmpty(range.ErrorMessage))
                {
                    result = TransformHelper.ApplyMessage(context.DataTransferObjectType, memberType, result, range.ErrorMessage);
                }

                result = TransformHelper.ApplyErrorCode(context.DataTransferObjectType, memberType, result, "range");

                return(result);
            }, (descriptor, _) => new MetadataInfo("range", new Dictionary <string, object> {
                ["min"] = range.Minimum, ["max"] = range.Maximum
            }));
        }
Beispiel #6
0
        public void Transform(TransformContext context)
        {
            if (!(context.Attribute is CompareAttribute compare))
            {
                return;
            }
            context.Handled = true;
            var fromSource = context.DataTransferObjectMember.GetCustomAttributesData()
                             .Any(p => p.AttributeType == typeof(CompareAttribute));
            var memberType = fromSource ? context.SourceMember.GetMemberType() : context.DataTransferObjectMember.GetMemberType();

            context.Metadata = new ValidationMetadata((descriptor, _, rule) =>
            {
                var expression = context.DataTransferObjectType.CreatePropertyLambda(fromSource ? compare.OtherProperty : descriptor.SourceMap[compare.OtherProperty]);
                var result     = TransformHelper.ApplyEqualRule(context.DataTransferObjectType, memberType, rule, expression);

                if (!string.IsNullOrEmpty(compare.ErrorMessage))
                {
                    result = TransformHelper.ApplyMessage(context.DataTransferObjectType, memberType, result, compare.ErrorMessage);
                }

                result = TransformHelper.ApplyErrorCode(context.DataTransferObjectType, memberType, result, "compare_to");

                return(result);
            }, (descriptor, _) =>
            {
                var prop = fromSource? compare.OtherProperty: descriptor.SourceMap[compare.OtherProperty];
                return(new MetadataInfo("compare_to", new Dictionary <string, object> {
                    ["otherProperty"] = prop
                }));
            });
        }
        public void Transform(TransformContext context)
        {
            if (!(context.Attribute is RegularExpressionAttribute regex))
            {
                return;
            }
            var memberType = context.DataTransferObjectMember.GetMemberType();

            if (memberType != typeof(string))
            {
                return;
            }
            context.Handled = true;

            context.Metadata = new ValidationMetadata((descriptor, _, rule) =>
            {
                var result = TransformHelper.ApplyMatchesRule(context.DataTransferObjectType, rule,
                                                              new Regex(regex.Pattern, RegexOptions.None, TimeSpan.FromMilliseconds(regex.MatchTimeoutInMilliseconds)));

                if (!string.IsNullOrEmpty(regex.ErrorMessage))
                {
                    result = TransformHelper.ApplyMessage(context.DataTransferObjectType, memberType, result, regex.ErrorMessage);
                }

                result = TransformHelper.ApplyErrorCode(context.DataTransferObjectType, memberType, result, "regex");

                return(result);
            }, (descriptor, _) => new MetadataInfo("regex", new Dictionary <string, object> {
                ["pattern"] = regex.Pattern
            }));
        }
Beispiel #8
0
        public void Transform(TransformContext context)
        {
            if (!(context.Attribute is CustomValidationAttribute customValidation))
            {
                return;
            }
            var memberType = context.DataTransferObjectMember.GetMemberType();
            var methodInfo = string.IsNullOrEmpty(customValidation.Method)
                                ? customValidation.ValidatorType.GetMethod(customValidation.Method)
                                : null;

            methodInfo = methodInfo ??
                         customValidation.ValidatorType.GetMethods(BindingFlags.Instance | BindingFlags.Public).FirstOrDefault();
            if (methodInfo == null || methodInfo.ReturnType != typeof(bool))
            {
                return;
            }
            context.Handled = true;

            context.Metadata = new ValidationMetadata((descriptor, services, rule) =>
            {
                var validator = services?.GetService(customValidation.ValidatorType) ??
                                Activator.CreateInstance(customValidation.ValidatorType);
                var wrapper = Activator.CreateInstance(typeof(Wrapper <,>).MakeGenericType(context.DataTransferObjectType, memberType), methodInfo,
                                                       validator, services);

                var result = TransformHelper.ApplyMustRule(context.DataTransferObjectType, memberType, rule, Delegate.CreateDelegate(typeof(Func <, ,>).MakeGenericType(context.DataTransferObjectType, memberType, typeof(bool)), wrapper, nameof(Wrapper <object, object> .Validate)));

                if (!string.IsNullOrEmpty(customValidation.ErrorMessage))
                {
                    result = TransformHelper.ApplyMessage(context.DataTransferObjectType, memberType, result, customValidation.ErrorMessage);
                }

                result = TransformHelper.ApplyErrorCode(context.DataTransferObjectType, memberType, result, "server_side");

                return(result);
            }, (descriptor, _) => new MetadataInfo("server_side", new Dictionary <string, object> {
                ["type"] = customValidation.ValidatorType.Name
            }));
        }