Unosquare
    Show / Hide Table of Contents

    Class Validate

    Provides validation methods for method arguments.

    Inheritance
    Object
    Validate
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: EmbedIO.Utilities
    Syntax
    public static class Validate

    Methods

    HttpHeaderName(String, String)

    Ensures that a langword_csharp_string argument is valid as an HTTP header name as defined by RFC7230, Section 3.2.

    The rules for valid HTTP header names are defined by RFC7230 to be the same as for RFC2616 tokens; for further information, please see the documentation for the Rfc2616Token(String, String, String) method.

    Declaration
    public static string HttpHeaderName(string argumentName, string value)
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    String value

    The value to validate.

    Returns
    Type Description
    String

    value, if it is a valid HTTP header name.

    HttpHeaderValue(String, String)

    Ensures that a langword_csharp_string argument is valid as an HTTP header value as defined by RFC7230, Section 3.2.

    Only visible ASCII visible characters, plus space and horizontal tab, are allowed.

    null is allowed, but it is turned into the empty string.

    Obsolete line folding (see Section 3.2.4) is NOT allowed, even if it is still allowed by Microsoft's .

    Declaration
    public static string HttpHeaderValue(string argumentName, string value)
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    String value

    The value to validate.

    Returns
    Type Description
    String

    value, if it is a valid HTTP header value, with spaces and horizontal tabs trimmed on both ends..

    LocalPath(String, String, Boolean)

    Ensures that the value of an argument is a valid local path and, optionally, gets the corresponding full path.

    Declaration
    public static string LocalPath(string argumentName, string value, bool getFullPath)
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    String value

    The value to validate.

    Boolean getFullPath

    true to get the full path, false to leave the path as is..

    Returns
    Type Description
    String

    The local path, or the full path if getFullPath is true.

    MimeType(String, String, Boolean)

    Ensures that a langword_csharp_string argument is valid as MIME type or media range as defined by RFC7231, Section 5,3.2.

    Declaration
    public static string MimeType(string argumentName, string value, bool acceptMediaRange)
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    String value

    The value to validate.

    Boolean acceptMediaRange

    If true, media ranges (i.e. strings of the form / and type/*) are considered valid; otherwise, they are rejected as invalid.

    Returns
    Type Description
    String

    value, if it is a valid MIME type or media range.

    NotNull<T>(String, T)

    Ensures that an argument is not null.

    Declaration
    public static T NotNull<T>(string argumentName, T value)
        where T : class
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    T value

    The value to validate.

    Returns
    Type Description
    T

    value if not null.

    Type Parameters
    Name Description
    T

    The type of the argument to validate.

    NotNullOrEmpty(String, String)

    Ensures that a langword_csharp_string argument is neither null nor the empty string.

    Declaration
    public static string NotNullOrEmpty(string argumentName, string value)
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    String value

    The value to validate.

    Returns
    Type Description
    String

    value if neither null nor the empty string.

    Rfc2616Token(String, String, String)

    Ensures that a langword_csharp_string argument is valid as a token as defined by RFC2616, Section 2.2.

    RFC2616 tokens are used, for example, as:

    • cookie names, as stated in RFC6265, Section 4.1.1;
    • WebSocket protocol names, as stated in RFC6455, Section 4.3.

    Only a restricted set of characters are allowed in tokens, including:

    • upper- and lower-case letters of the English alphabet;
    • decimal digits;
    • the following non-alphanumeric characters: !, #, $, %, &, ', *, +, -, ., ^, _, `, |, ~.

    Declaration
    public static string Rfc2616Token(string argumentName, string value, string entity = "Token")
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    String value

    The value to validate.

    String entity

    What is being validated. This string is put at the start of the exception message when validation fails. Default is "Token", as in "Token contains one or more invalid characters."

    Returns
    Type Description
    String

    value, if it is a valid token.

    Route(String, String, Boolean)

    Ensures that the value of an argument is a valid route.

    Declaration
    public static string Route(string argumentName, string value, bool isBaseRoute)
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    String value

    The value to validate.

    Boolean isBaseRoute

    true if the argument must be a base route; false if the argument must be a non-base route.

    Returns
    Type Description
    String

    value, if it is a valid route.

    See Also
    IsValid(String, Boolean)

    Url(String, String, Uri, Boolean)

    Ensures that a valid URL, either absolute or relative to the given baseUri, can be constructed from a langword_csharp_string argument and returns the absolute URL obtained by combining baseUri and value.

    Declaration
    public static string Url(string argumentName, string value, Uri baseUri, bool enforceHttp = false)
    Parameters
    Type Name Description
    String argumentName

    Name of the argument.

    String value

    The value.

    Uri baseUri

    The base URI for relative URLs.

    Boolean enforceHttp

    Ensure that the scheme of the resulting URL is either http or https.

    Returns
    Type Description
    String

    The string representation of the constructed URL.

    See Also
    Url(String, String, UriKind, Boolean)

    Url(String, String, UriKind, Boolean)

    Ensures that a valid URL can be constructed from a langword_csharp_string argument.

    Declaration
    public static string Url(string argumentName, string value, UriKind uriKind = null, bool enforceHttp = false)
    Parameters
    Type Name Description
    String argumentName

    Name of the argument.

    String value

    The value.

    UriKind uriKind

    Specifies whether value is a relative URL, absolute URL, or is indeterminate.

    Boolean enforceHttp

    Ensure that, if value is an absolute URL, its scheme is either http or https.

    Returns
    Type Description
    String

    The string representation of the constructed URL.

    See Also
    Url(String, String, Uri, Boolean)

    UrlPath(String, String, Boolean)

    Ensures that the value of an argument is a valid URL path and normalizes it.

    Declaration
    public static string UrlPath(string argumentName, string value, bool isBasePath)
    Parameters
    Type Name Description
    String argumentName

    The name of the argument to validate.

    String value

    The value to validate.

    Boolean isBasePath

    If set to truetrue, the returned path is ensured to end in a slash (/) character; otherwise, the returned path is ensured to not end in a slash character unless it is "/".

    Returns
    Type Description
    String

    The normalized URL path.

    See Also
    Normalize(String, Boolean)

    Comments

    Back to top Copyright © 2017-2019 Unosquare