[whatwg/url] API mechanism for reporting validity errors (Issue #811)

### What is the issue with the URL Standard?

The spec specifically calls out validity errors while parsing and recommends that implementations report those in some way. The URL API does not surface those in any way and leaves it up to the host implementation to report validity errors out of band.

It would likely be useful for the API to provide some reasonable means of handling validity state...

For instance, a few approaches come to mind:

A. Adding an `valid` or `invalid` property to `URL`. The value would be boolean. It would not report the specific validation error but would simply indicate whether or not a validation error exists.

```
const url = new URL('file:foobar');
console.log(url.valid); // false
```

B. Adding a `validationErrors` property that is an array of validation errors. These can be numeric codes or strings that represent the specific collection of validation errors defined in the spec. A `null` value for this property would indicate no validation errors. If a value is provided it must be an array. This would allow both simple checking for valid/invalid and specific reporting for individual validation errors.

```
const url = new URL('file:foobar');
console.log(url.validationErrors); // ['special-scheme-missing-following-solidus']
```

C. Adding an opt-in strict parsing mode that converts validation errors into thrown exceptions.

```
const url = new URL.Strict('file:foobar');  // throws!
```

Of these options, I generally prefer `A` as I do not really think there's much practical reason to break down exactly why the URL is invalid as much as simply calling it out.


-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/811
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/url/issues/811@github.com>

Received on Sunday, 14 January 2024 20:09:09 UTC