ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
6.96k stars 223 forks source link

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

Open brendonboshell opened 1 year ago

brendonboshell commented 1 year ago

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.