superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

pattern (regex) does not return consistent output when using the global modifier

Open brendonboshell opened this issue 2 years ago • 1 comments

In this example:

const s = require("superstruct");

const Query = s.object({
  url: s.pattern(s.string(), /^example/g),
});

const testQuery = {
  url: "example",
};

console.log(s.is(testQuery, Query), s.is(testQuery, Query));

It returns the following output:

% node index.js
true false

I would expect each call to is to return the same output. I suspect this is because of the stateful behaviour of regular expressions.

brendonboshell avatar Jul 18 '23 16:07 brendonboshell

I just opened a PR with a fix https://github.com/ianstormtaylor/superstruct/pull/1295

brleinad avatar Aug 04 '25 22:08 brleinad