コード例 #1
0
        private static bool ValidateFormat(string format, string value)
        {
            switch (format)
            {
            case Constants.Formats.Uuid:
            {
#if NET35
                return(UuidRegex.IsMatch(value));
#else
                return(Guid.TryParseExact(value, "D", out _));
#endif
            }

            case Constants.Formats.Color:
            {
                return(ColorHelpers.IsValid(value));
            }

            case Constants.Formats.Hostname:
            case Constants.Formats.Draft3Hostname:
            {
                // http://stackoverflow.com/questions/1418423/the-hostname-regex
                return(HostnameRegex.IsMatch(value));
            }

            case Constants.Formats.IPv4:
            case Constants.Formats.Draft3IPv4:
            {
                return(FormatHelpers.ValidateIPv4(value));
            }

            case Constants.Formats.IPv6:
            {
                return(FormatHelpers.ValidateIPv6(value));
            }

            case Constants.Formats.Email:
            {
                return(EmailHelpers.Validate(value, true));
            }

            case Constants.Formats.Uri:
            {
                return(Uri.IsWellFormedUriString(value, UriKind.Absolute));
            }

            case Constants.Formats.UriReference:
            {
                return(FormatHelpers.ValidateUriReference(value));
            }

            case Constants.Formats.Duration:
            {
                return(FormatHelpers.ValidateDuration(value));
            }

            case Constants.Formats.UriTemplate:
            {
                return(FormatHelpers.ValidateUriTemplate(value));
            }

            case Constants.Formats.JsonPointer:
            {
                return(FormatHelpers.ValidateJsonPointer(value));
            }

            case Constants.Formats.Date:
            {
                return(DateTime.TryParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime _));
            }

            case Constants.Formats.Time:
            {
                return(DateTime.TryParseExact(value, "HH:mm:ss.FFFFFFFK", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime _));
            }

            case Constants.Formats.DateTime:
            {
                // RFC 3339 states that the T and Z characters in the "date-time" format are case insensitive.
                if (value.IndexOfAny(CaseInsensitiveDateTimeChars) != -1)
                {
                    value = value.ToUpperInvariant();
                }
                return(DateTime.TryParseExact(value, @"yyyy-MM-dd\THH:mm:ss.FFFFFFFK", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime _));
            }

            case Constants.Formats.UtcMilliseconds:
            {
                return(double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out double _));
            }

            case Constants.Formats.Regex:
            {
                try
                {
                    new Regex(value);
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }

            default:
            {
                return(true);
            }
            }
        }
コード例 #2
0
 static bool ValidateUriReference(string value) =>
 FormatHelpers.ValidateUriReference(value);