Validator

url

Parse with new URL() and optionally restrict by scheme. Empty values pass.

Live demo — any scheme

errors: null

Live demo — HTTPS only

errors: null

Usage

import { FormControl } from '@angular/forms';
import { WrValidators } from 'ngwr/validators';

const link = new FormControl('', [WrValidators.url()]);

const secure = new FormControl('', [
  WrValidators.url({ protocols: ['https'] }),
]);
secure.setValue('http://example.com');
secure.errors; // { url: { allowed: ['https'] } }

Why ngwr provides this

Hand-rolled URL regexes via Validators.pattern are notoriously wrong. This defers to the platform URL parser and can restrict protocols. A scheme is required by default; pass url({ requireProtocol: false }) to also accept bare domains like ngwr.dev (assuming https).

API

NameDescriptionTypeDefault
signatureFactory — call it to get a ValidatorFn.(opts?) => ValidatorFn
options.protocolsWhitelist of accepted schemes. Trailing : optional. Case-insensitive.readonly string[]any scheme
options.requireProtocolWhen false, bare domains like ngwr.dev are accepted (parsed as if prefixed with https://).booleantrue
error keyOn unparseable input: { url: true }. On scheme mismatch: { url: { allowed: [...] } }. Empty value passes.{ url: true | { allowed: string[] } }

See also