hapijs / hapijs/joi

Joi.string().pattern() fails when dealing with different contexts

Open
#2,668 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### Support plan

* *is this issue currently blocking your project?* (yes/no): Not at present
* *is this issue affecting a production system?* (yes/no): No

#### Context

* *node version*: v14.15.5
* *module version with issue*: v17.4.2
* *last module version without issue*: N/A
* *environment* (e.g. node, browser, native): Node & Browser
* *used with* (e.g. hapi application, another framework, standalone, ...): Universal (SSR) Nuxt Application
* *any other relevant information*:

#### What are you trying to achieve or the steps to reproduce?

Due to different global contexts, .pattern() fails in certain scenarios, I.e. Server-side rendering using Nuxt.
However, this could be achieved just as simply using a subwindow with `window.open()`

I.e.
```js
const a = window.open();
console.log(a.RegExp == RegExp); // false

const b = /123/;
console.log(b instanceof a.RegExp); // false
```

https://github.com/sideway/joi/blob/63b324497f03d9d39612b404da157e81d9c5aea9/lib/types/string.js#L531
Joi's current assertion is that `regex instanceof RegExp`, but as seen above this fails in niche (but valid, and not altogether uncommon) usecases. While not as safe, other alternatives could be to assert `regex?.constructor.name === "RegExp"` or to produce a new regex in the validating context `RegExp(regex)`.
Which approach is most desirable is your call, naturally.

#### What was the result you got?

`regex must be a RegExp` error.

#### What result did you expect?

To have a functional validation schema using .regex() across contexts.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.