public override OneOf <ParseSuccess, (ErrorDescription description, dynamic bestParseAttempt)> Parse(
            Type type, string value, string path)
        {
            if (type != typeof(string) && type != typeof(MailAddress))
            {
                throw new InvalidOperationException(
                          $"Fields tagged with {typeof(StronglyApiedEmailAddressAttribute).FullName} "
                          + $"must be {typeof(string).FullName} or {typeof(MailAddress).FullName}, "
                          + $" but the given type was {type.FullName}");
            }

            string      trimmedValue = value.Trim();
            MailAddress parsedValue;

            try
            {
                parsedValue = new MailAddress(trimmedValue);
            }
            catch
            {
                return(ErrorDescription.InvalidEmailAddress(trimmedValue, path), trimmedValue);
            }

            if (type == typeof(string))
            {
                //RFC 1035, section 3.1 + RFC 5321, section 2.3.11:
                //Domain part isn't case sensitive, but user part may be - it's up to the individual mail providers.
                return(ParseSuccess.From(string.Format("{0}@{1}", parsedValue.User, parsedValue.Host.ToLower())));
            }

            else
            {
                return(ParseSuccess.From(parsedValue));
            }
        }
Esempio n. 2
0
        public override OneOf <ParseSuccess, (ErrorDescription description, dynamic bestParseAttempt)> Parse(
            Type type, string value, string path)
        {
            string trimmedValue = value.Trim();

            if (type != typeof(DateTime))
            {
                if (type == typeof(DateTime?))
                {
                    if (string.IsNullOrWhiteSpace(trimmedValue))
                    {
                        return(ParseSuccess.From(null));
                    }
                }
                else
                {
                    throw new InvalidOperationException(
                              $"Fields tagged with {typeof(StronglyApiedDateTimeAttribute).FullName} "
                              + $"must be a DateTime, "
                              + $"but the given type was {type.FullName}");
                }
            }

            if (exactFormat is null)
            {
                if (DateTime.TryParse(trimmedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result))
                {
                    return(ParseSuccess.From(result));
                }
                else
                {
                    return(ErrorDescription.InvalidLooseTimestamp(trimmedValue, path), default);
        public override OneOf <ParseSuccess, (ErrorDescription description, dynamic bestParseAttempt)> Parse(
            Type type, string value, string path)
        {
            if (type != typeof(string))
            {
                throw new InvalidOperationException(
                          $"Fields tagged with {typeof(StronglyApiedStringAttribute).FullName} "
                          + $"must be a {typeof(string).FullName}, "
                          + $"but the given type was {type.FullName}");
            }

            string trimmedValue = value.Trim();

            if (trimmedValue.Length < minLength)
            {
                return(ErrorDescription.StringTooShort(minLength, trimmedValue, path), trimmedValue);
            }

            if (trimmedValue.Length > maxLength)
            {
                return(ErrorDescription.StringTooLong(maxLength, trimmedValue, path), trimmedValue);
            }

            return(ParseSuccess.From(trimmedValue));
        }
        public override OneOf <ParseSuccess, (ErrorDescription description, dynamic bestParseAttempt)> Parse(Type type, string value, string path)
        {
            if (type != typeof(long) && type != typeof(long?))
            {
                throw new InvalidOperationException(
                          $"Fields tagged with {typeof(StronglyApiedLongAttribute).FullName} "
                          + $"must be a {typeof(long).FullName},"
                          + $"but the given type was {type.FullName}");
            }

            if (string.IsNullOrEmpty(value))
            {
                if (optional)
                {
                    return(ParseSuccess.From(null));
                }
                else
                {
                    return(ErrorDescription.InvalidInt64(value, path), default);