Example #1
0
 public static void PhoneNumber <T>(this IRuleBuilderInitial <T, string> field)
 {
     field
     .NormalizeWhitespace()
     .Apply(Rules.PhoneNumber)
     .WithMessageFromErrorCode("VENUE_TELEPHONE_FORMAT");
 }
Example #2
0
        public static void ProviderVenueRef <T>(
            this IRuleBuilderInitial <T, string> field,
            Func <T, Task <IEnumerable <string> > > getOtherVenueProviderVenueRefs)
        {
            field
            .NormalizeWhitespace()
            .NotEmpty()
            .WithMessageFromErrorCode("VENUE_PROVIDER_VENUE_REF_REQUIRED")
            .MaximumLength(Constants.ProviderVenueRefMaxLength)
            .WithMessageFromErrorCode("VENUE_PROVIDER_VENUE_REF_MAXLENGTH")
            .MustAsync(async(obj, providerVenueRef, _) =>
            {
                // Field is optional
                if (string.IsNullOrEmpty(providerVenueRef))
                {
                    return(true);
                }

                // Venue name must be distinct for this provider
                var otherVenueRefs = await getOtherVenueProviderVenueRefs(obj);

                var otherVenuesWithSameRef = otherVenueRefs
                                             .Where(v => (v ?? string.Empty).Equals(providerVenueRef, StringComparison.OrdinalIgnoreCase));

                return(!otherVenuesWithSameRef.Any());
            })
            .WithMessageFromErrorCode("VENUE_PROVIDER_VENUE_REF_UNIQUE");
        }
 public static void ContactTelephone <T>(this IRuleBuilderInitial <T, string> field) =>
 field
 .NormalizeWhitespace()
 .NotEmpty()
 .WithMessageFromErrorCode("APPRENTICESHIP_TELEPHONE_REQUIRED")
 .Apply(Rules.PhoneNumber)
 .WithMessageFromErrorCode("APPRENTICESHIP_TELEPHONE_FORMAT");
        public static void Venue <T>(
            this IRuleBuilderInitial <T, string> field,
            Func <T, ApprenticeshipLocationType?> getDeliveryMode)
        {
            field
            .NormalizeWhitespace()
            .Custom((v, ctx) =>
            {
                var obj = (T)ctx.InstanceToValidate;

                var deliveryMode = getDeliveryMode(obj);
                var isSpecified  = !string.IsNullOrEmpty(v);

                // Not allowed for delivery modes other than classroom based
                if (isSpecified && deliveryMode != ApprenticeshipLocationType.ClassroomBased)
                {
                    ctx.AddFailure(CreateFailure("APPRENTICESHIP_VENUE_NOT_ALLOWED"));
                    return;
                }

                if (deliveryMode != ApprenticeshipLocationType.ClassroomBased)
                {
                    return;
                }

                ValidationFailure CreateFailure(string errorCode) =>
                ValidationFailureEx.CreateFromErrorCode(ctx.PropertyName, errorCode);
            });
        }
Example #5
0
 public static void ProviderCourseRef <T>(this IRuleBuilderInitial <T, string> field)
 {
     field
     .NormalizeWhitespace()
     .MaximumLength(Constants.ProviderCourseRefMaxLength)
     .WithMessageFromErrorCode("COURSERUN_PROVIDER_COURSE_REF_MAXLENGTH")
     .Matches(@"^[a-zA-Z0-9/\n/\r/\\u/\¬\!\£\$\%\^\&\*\\é\\è\\fi\(\)_\+\-\=\{\}\[\]\;\:\@\'\#\~\,\<\>\.\?\/\|\`\•\·\●\\’\‘\“\”\—\-\–\‐\‐\…\:/\°\®\\â\\ç\\ñ\\ü\\ø\♦\™\\t/\s\¼\¾\½\" + "\"" + "\\\\]+$")
     .WithMessageFromErrorCode("COURSERUN_PROVIDER_COURSE_REF_FORMAT");
 }
Example #6
0
        public static void Email <T>(this IRuleBuilderInitial <T, string> field)
        {
            const string emailRegex = @"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-||_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+([a-z]+|\d|-|\.{0,1}|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])?([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$";

            field
            .NormalizeWhitespace()
            .Matches(emailRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture)
            .WithMessageFromErrorCode("VENUE_EMAIL_FORMAT");
        }
Example #7
0
 public static void County <T>(this IRuleBuilderInitial <T, string> field)
 {
     field
     .NormalizeWhitespace()
     .MaximumLength(Constants.CountyMaxLength)
     .WithMessageFromErrorCode("VENUE_COUNTY_MAXLENGTH")
     .Matches(_countyPattern)
     .WithMessageFromErrorCode("VENUE_COUNTY_FORMAT");
 }
Example #8
0
 public static void AddressLine2 <T>(this IRuleBuilderInitial <T, string> field)
 {
     field
     .NormalizeWhitespace()
     .MaximumLength(Constants.AddressLine2MaxLength)
     .WithMessageFromErrorCode("VENUE_ADDRESS_LINE2_MAXLENGTH")
     .Matches(AddressLinePattern)
     .WithMessageFromErrorCode("VENUE_ADDRESS_LINE2_FORMAT");
 }
Example #9
0
 public static void Postcode <T>(this IRuleBuilderInitial <T, string> field, Func <Postcode, PostcodeInfo> getPostcodeInfo)
 {
     field
     .NormalizeWhitespace()
     .NotEmpty()
     .WithMessageFromErrorCode("VENUE_POSTCODE_REQUIRED")
     // i.e. Postcode must exist in our Postcode info table (so we can resolve a lat/lng)
     .Must(postcode => postcode == null || (Models.Postcode.TryParse(postcode, out var pc) && getPostcodeInfo(pc) != null))
     .WithMessageFromErrorCode("VENUE_POSTCODE_FORMAT");
 }
Example #10
0
 public static void Town <T>(this IRuleBuilderInitial <T, string> field)
 {
     field
     .NormalizeWhitespace()
     .NotEmpty()
     .WithMessageFromErrorCode("VENUE_TOWN_REQUIRED")
     .MaximumLength(Constants.TownMaxLength)
     .WithMessageFromErrorCode("VENUE_TOWN_MAXLENGTH")
     .Matches(_townPattern)
     .WithMessageFromErrorCode("VENUE_TOWN_FORMAT");
 }
        public static void VenueName <T>(
            this IRuleBuilderInitial <T, string> field,
            Func <T, ApprenticeshipLocationType?> getDeliveryMethod,
            Func <T, string> getProviderVenueRef,
            Guid?matchedVenueId)
        {
            field
            .NormalizeWhitespace()
            .Custom((v, ctx) =>
            {
                var obj = (T)ctx.InstanceToValidate;

                var deliveryMode = getDeliveryMethod(obj);
                var isSpecified  = !string.IsNullOrEmpty(v);

                // Not allowed for delivery modes other than classroom based
                if (isSpecified &&
                    deliveryMode != ApprenticeshipLocationType.ClassroomBased &&
                    deliveryMode != ApprenticeshipLocationType.ClassroomBasedAndEmployerBased)
                {
                    ctx.AddFailure(CreateFailure("APPRENTICESHIP_VENUE_NAME_NOT_ALLOWED"));
                    return;
                }

                if (deliveryMode != ApprenticeshipLocationType.ClassroomBased &&
                    deliveryMode != ApprenticeshipLocationType.ClassroomBasedAndEmployerBased)
                {
                    return;
                }

                if (isSpecified && !matchedVenueId.HasValue)
                {
                    // We don't want both a ref and a name but if the ref resolves a venue and that venue's name
                    // matches this name then we let it go. If it doesn't match then yield an error.
                    if (!string.IsNullOrEmpty(getProviderVenueRef(obj)))
                    {
                        ctx.AddFailure(CreateFailure("APPRENTICESHIP_VENUE_NAME_NOT_ALLOWED_WITH_REF"));
                        return;
                    }

                    // Couldn't find a match from name
                    ctx.AddFailure(CreateFailure("APPRENTICESHIP_VENUE_NAME_INVALID"));
                    return;
                }

                ValidationFailure CreateFailure(string errorCode) =>
                ValidationFailureEx.CreateFromErrorCode(ctx.PropertyName, errorCode);
            });
        }
Example #12
0
        public static IRuleBuilderOptions <T, string> VenueName <T>(
            this IRuleBuilderInitial <T, string> field,
            Func <T, Task <IEnumerable <string> > > getOtherVenueNames)
        {
            return(field
                   .NormalizeWhitespace()
                   .NotEmpty()
                   .WithMessageFromErrorCode("VENUE_NAME_REQUIRED")
                   .MaximumLength(Constants.NameMaxLength)
                   .WithMessageFromErrorCode("VENUE_NAME_MAXLENGTH")
                   .MustAsync(async(obj, name, _) =>
            {
                // Venue name must be distinct for this provider
                var otherVenueNames = await getOtherVenueNames(obj);

                var otherVenuesWithSameName = otherVenueNames
                                              .Where(v => (v ?? string.Empty).Equals(name, StringComparison.OrdinalIgnoreCase));

                return !otherVenuesWithSameName.Any();
            })
                   .WithMessageFromErrorCode("VENUE_NAME_UNIQUE"));
        }