コード例 #1
0
        public ContactFormRequestModelValidator()
        {
            RuleFor(x => x.FirstName)
            .NotEmpty().WithMessage(ValidationMessageHelper.ForNotEmpty(nameof(ContactFormRequestModel.FirstName)))
            .MinimumLength(2).WithMessage("'{PropertyName}' must have a minimum of 2 charaters")
            .MaximumLength(20).WithMessage("'{PropertyName}' must have a maximum of 20 charaters")
            .Matches(_alphaNumericPattern).WithMessage("'{PropertyName}' must only contain alphanumeric charaters (a-z, A-Z, 0-9)");

            RuleFor(x => x.LastName)
            .NotEmpty().WithMessage(ValidationMessageHelper.ForNotEmpty(nameof(ContactFormRequestModel.LastName)))
            .MinimumLength(2).WithMessage("'{PropertyName}' must have a minimum of 2 charaters")
            .MaximumLength(20).WithMessage("'{PropertyName}' must have a maximum of 20 charaters")
            .Matches(_alphaNumericPattern).WithMessage("'{PropertyName}' must only contain alphanumeric charaters (a-z, A-Z, 0-9)");

            RuleFor(x => x.Email)
            .NotEmpty().WithMessage(ValidationMessageHelper.ForNotEmpty(nameof(ContactFormRequestModel.Email)))
            .MaximumLength(320).WithMessage("'{PropertyName}' must have a maximum of 320 charaters")
            .Matches(_emailPattern).WithMessage("'{PropertyName}' must only contain a valid email pattern");

            RuleFor(x => x.Subject)
            .NotEmpty().WithMessage(ValidationMessageHelper.ForNotEmpty(nameof(ContactFormRequestModel.Subject)))
            .MinimumLength(2).WithMessage("'{PropertyName}' must have a minimum of 2 charaters")
            .MaximumLength(100).WithMessage("'{PropertyName}' must have a maximum of 100 charaters")
            .Matches(_alphaNumericWithSpecialCharPattern).WithMessage("'{PropertyName}' must only contain alphanumeric charaters and a limited set of special charaters");

            RuleFor(x => x.Message)
            .NotEmpty().WithMessage(ValidationMessageHelper.ForNotEmpty(nameof(ContactFormRequestModel.Message)))
            .MinimumLength(10).WithMessage("'{PropertyName}' must have a minimum of 2 charaters")
            .MaximumLength(1000).WithMessage("'{PropertyName}' must have a maximum of 20 charaters")
            .Matches(_alphaNumericWithSpecialCharPattern).WithMessage("'{PropertyName}' must only contain alphanumeric charaters and a limited set of special charaters");
        }
        public LocationDetailsRequestModelValidator()
        {
            RuleFor(x => x.Lat)
            .NotNull().WithMessage(ValidationMessageHelper.ForNotNull(nameof(LocationDetailsRequestModel.Lat)));

            RuleFor(x => x.Lng)
            .NotNull().WithMessage(ValidationMessageHelper.ForNotNull(nameof(LocationDetailsRequestModel.Lng)));

            RuleFor(x => x.Address)
            .NotNull().WithMessage(ValidationMessageHelper.ForNotNull(nameof(LocationDetailsRequestModel.Address)));

            RuleFor(x => x.Address.LineOne)
            .NotEmpty().WithMessage(ValidationMessageHelper.ForNotEmpty(nameof(LocationDetailsRequestModel.Address.LineOne)))
            .MinimumLength(2).WithMessage("'{PropertyName}' must have a minimum of 2 charaters")
            .MaximumLength(100).WithMessage("'{PropertyName}' must have a maximum of 100 charaters")
            .Matches("[a-zA-Z0-9_.+-]").WithMessage("'{ PropertyName}' must only contain alphanumeric charaters (a-z, A-Z, 0-9)");

            RuleFor(x => x.Address.Country)
            .NotEmpty().WithMessage(ValidationMessageHelper.ForNotEmpty(nameof(LocationDetailsRequestModel.Address.Country)));

            RuleFor(x => x.AssignedGamesList)
            .NotNull().WithMessage(ValidationMessageHelper.ForNotNull(nameof(LocationDetailsRequestModel.AssignedGamesList)));
        }