URLSchemaclass

new URLSchema({ one = "URL", title = "URL", base, schemes = HTTP_SCHEMES, input = "url", max = 512, ...options }: URLSchemaOptions)
ParamType
optionsURLSchemaOptions
Options for URLSchema. required
    .valuestring
Default string value used when the input is undefined. readonly
    .maxnumber
Maximum allowed character length. Defaults to 512 readonly
    .matchRegExp
Regular expression the sanitized string must match. readonly
    .case"upper"
"lower"
Force the result to "upper" or "lower" case. readonly
    .inputStringInputType
HTML <input /> type="" hint for downstream UIs. Defaults to "url" readonly
    .baseURL
URLString
Base URL that relative URLs are resolved against. readonly
    .schemesURISchemes
Whitelist of allowed URL schemes. Defaults to HTTP_SCHEMES readonly
Return
URLSchema
Schema that defines a valid absolute or relative URL string.
PropertyType
.baseURLString
Base URL that relative URLs are resolved against, or undefined when not set. readonly
.schemesURISchemes
Whitelist of allowed URL schemes, e.g. ["https:", "http:"]. required readonly

Schema that defines a valid absolute or relative URL string.

  • Checks the URL scheme against a whitelist (always), and resolves relative URLs against base when set.
  • URLs are limited to 512 characters, but generally these won't be data: URIs so this is a reasonable limit.

Examples

const schema = new URLSchema({ base: "https://example.com" });
	schema.validate("/page") // "https://example.com/page"