protected override bool PreValidate(
            ValidationContext <SampleRequestDto> context, ValidationResult result)
        {
            var contextData = new ValidationContextData(context.RootContextData);

            contextData.CountryIsoCode =
                context.InstanceToValidate?.Address?.CountryIsoCode;

            return(true);
        }
Ejemplo n.º 2
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var phoneNumber = (string)context.PropertyValue;

            if (string.IsNullOrEmpty(phoneNumber))
            {
                return(true);
            }

            var contextData = new ValidationContextData(context.ParentContext.RootContextData);

            var country = TryFindCountry(contextData.CountryIsoCode);

            if (country == null)
            {
                // without a country we cannot validate a phone number
                return(true);
            }

            context.MessageFormatter.AppendArgument("Country", country.Name);

            return(country.PhoneNumberFormat.Matches(phoneNumber));
        }