Пример #1
0
        /// <summary>
        /// Checks if electronic channel model is valid or not.
        /// </summary>
        /// <param name="modelState"></param>
        public override void Validate(ModelStateDictionary modelState)
        {
            // Validate names against newly added languages (required languages)
            var names = new LanguageItemListValidator(Model.ServiceChannelNames, "ServiceChannelNames", requiredLanguages);

            names.Validate(modelState);
            // Validate descriptions against newly added languages (required languages), all the available languages does not need to be validated.
            var descriptions = new LocalizedListValidator(Model.ServiceChannelDescriptions, "ServiceChannelDescriptions", requiredLanguages,
                                                          new List <string>()
            {
                DescriptionTypeEnum.ShortDescription.ToString(), DescriptionTypeEnum.Description.ToString()
            });

            descriptions.Validate(modelState);

            hours.Validate(modelState);
            organizationId.Validate(modelState);
            languages.Validate(modelState);
            phones.Validate(modelState);

            // Validate publishing status
            var status = new PublishingStatusValidator(Model.PublishingStatus, currentPublishingStatus);

            status.Validate(modelState);

            areas.Validate(modelState);
            services.Validate(modelState);
        }
Пример #2
0
        /// <summary>
        /// Validates phone channel model.
        /// </summary>
        /// <param name="modelState"></param>
        public override void Validate(ModelStateDictionary modelState)
        {
            base.Validate(modelState);

            // Validate phone numbers
            var phones = new PhoneNumberListValidator <V4VmOpenApiPhoneWithType>(Model.PhoneNumbers, codeService, requiredLanguages: RequiredLanguages, availableLanguages: AvailableLanguages);

            phones.Validate(modelState);
        }
Пример #3
0
        /// <summary>
        /// Validate organization model.
        /// </summary>
        public override void Validate(ModelStateDictionary modelState)
        {
            OrganizationTypeEnum?organizationType = null;

            if (!Model.OrganizationType.IsNullOrEmpty())
            {
                organizationType = Model.OrganizationType.Parse <OrganizationTypeEnum>();
            }

            name.Validate(modelState);

            // Validate municipality
            if (organizationType != OrganizationTypeEnum.Municipality && !Model.Municipality.IsNullOrEmpty())
            {
                modelState.AddModelError("Municipality", $"No Municipality accepted when OrganizationType has value {Model.OrganizationType}.");
            }
            else
            {
                municipality.Validate(modelState);
            }

            addresses.Validate(modelState);
            organizationId.Validate(modelState);
            oid.Validate(modelState);
            phones.Validate(modelState);
            status.Validate(modelState);

            // Validate organization descriptions: ShortDescription on is mandatory on versions 7+
            if (versionNumber >= 7)
            {
                description.Validate(modelState);
            }

            // Validate area data according to organization type
            switch (organizationType)
            {
            case OrganizationTypeEnum.Municipality:
            case OrganizationTypeEnum.TT1:
            case OrganizationTypeEnum.TT2:
                // No area info accepted if organization type is Municipality, TT1 or TT2 - except default value areaType => WholeCountry!
                if (!Model.AreaType.IsNullOrEmpty() && Model.AreaType != AreaInformationTypeEnum.WholeCountry.ToString())
                {
                    modelState.AddModelError("AreaType", $"Value '{ Model.AreaType}' not accepted when OrganizationType has value {Model.OrganizationType}.");
                }
                if (!Model.SubAreaType.IsNullOrEmpty())
                {
                    modelState.AddModelError("SubAreaType", $"No SubAreaType accepted when OrganizationType has value {Model.OrganizationType}.");
                }
                if (Model.Areas?.Count > 0)
                {
                    modelState.AddModelError("Areas", $"No Areas accepted when OrganizationType has value {Model.OrganizationType}.");
                }
                break;

            case OrganizationTypeEnum.RegionalOrganization:
                // Only AreaType is accepted if organization type is RegionalOrganization.
                if (Model.AreaType != AreaInformationTypeEnum.AreaType.ToString())
                {
                    modelState.AddModelError("AreaType", $"Value {AreaInformationTypeEnum.AreaType.ToString()} required when OrganizationType has value {Model.OrganizationType}.");
                }

                // Validate the area codes
                ValidateAreas(modelState);

                break;

            case null:
                if (!Model.AreaType.IsNullOrEmpty() || !Model.SubAreaType.IsNullOrEmpty() || Model.Areas?.Count > 0)
                {
                    modelState.AddModelError("OrganizationType", $"OrganizationType field is required when AreaType, SubAreaType or Areas has values.");
                }
                break;

            default:
                if (Model.AreaType != AreaInformationTypeEnum.AreaType.ToString() && !Model.SubAreaType.IsNullOrEmpty())
                {
                    modelState.AddModelError("SubAreaType", $"No SubAreaType accepted when AreaType has value {Model.AreaType}.");
                }
                if (Model.AreaType != AreaInformationTypeEnum.AreaType.ToString() && Model.Areas?.Count > 0)
                {
                    modelState.AddModelError("Areas", $"No Areas accepted when AreaType has value {Model.AreaType}.");
                }
                // For State, Organization and Company types we are validating the area codes
                ValidateAreas(modelState);
                break;
            }
        }
        /// <summary>
        /// Validates service location channel model
        /// </summary>
        /// <param name="modelState"></param>
        public override void Validate(ModelStateDictionary modelState)
        {
            base.Validate(modelState);

            phones.Validate(modelState);
            faxNumbers.Validate(modelState);

            // Validate addresses
            // validation rules, see PTV-2910
            var i = 0;

            Model.Addresses.ForEach(a =>
            {
                if ((a.Type == AddressConsts.LOCATION && (a.SubType == AddressTypeEnum.Street.ToString() || a.SubType == AddressTypeEnum.PostOfficeBox.ToString())) ||
                    (a.Type == AddressCharacterEnum.Postal.ToString() && (a.SubType == AddressConsts.SINGLE || a.SubType == AddressConsts.MULTIPOINT)))
                {
                    modelState.AddModelError($"Addresses[{i}].SubType", $"'{a.SubType}' is not allowed value of 'SubType' when 'Type' has value '{ a.Type}'.");
                }
                i++;
            });

            // if subType is Single only one other address (with same type) can be added - PTV-2910
            var singleAddresses = Model.Addresses.Where(a => a.Type == AddressConsts.LOCATION && a.SubType == AddressConsts.SINGLE).ToList();

            if (singleAddresses?.Count > 0)
            {
                if (Model.Addresses.Any(a => a.Type == AddressConsts.LOCATION && a.SubType != AddressConsts.SINGLE))
                {
                    modelState.AddModelError($"Addresses", $"The field is invalid. When 'SubType' has value '{ AddressConsts.SINGLE }' no other types of { AddressConsts.LOCATION } addresses can be defined.");
                }

                if (singleAddresses.Count > 2)
                {
                    modelState.AddModelError($"Addresses", $"The field is invalid. You can only add two addresses with 'SubType' as '{ AddressConsts.SINGLE }'.");
                }
            }

            // if address list includes foreign addresses no other address types are allowed - PTV-2910
            var foreignAddresses = Model.Addresses.Where(a => a.SubType == AddressConsts.ABROAD).ToList();

            foreignAddresses.GroupBy(a => a.Type).ToDictionary(a => a.Key, a => a.ToList()).ForEach(fa =>
            {
                // multible foreign addresses?
                if (fa.Value.Count > 1)
                {
                    modelState.AddModelError($"Addresses", $"The field is invalid. Only one address with 'SubType' = '{ AddressConsts.ABROAD }' and 'Type' = '{ fa.Key }' is allowed.");
                }

                // other sub types defined?
                if (Model.Addresses.Any(a => a.Type == fa.Key && a.SubType != AddressConsts.ABROAD))
                {
                    modelState.AddModelError($"Addresses", $"The field is invalid. When 'SubType' has value '{ AddressConsts.ABROAD }' no other types of { fa.Key } addresses can be defined.");
                }
            });

            // if address list includes moving address other type of visiting addresses cannot exist
            if (Model.Addresses.Any(a => a.SubType == AddressConsts.MULTIPOINT))
            {
                if (Model.Addresses.Any(a => a.Type == AddressConsts.LOCATION && a.SubType != AddressConsts.MULTIPOINT))
                {
                    modelState.AddModelError("Addresses", $"The field is invalid. When 'SubType' has value '{ AddressConsts.MULTIPOINT }' no other types of { AddressConsts.LOCATION } addresses can be defined.");
                }
            }
            addresses.Validate(modelState);
        }