ansman/validate.js

When there is a 'g' modifier in RegExp, format validator returns errors every even run

Closed this issue · 1 comments

var constraints = { foo: { format: /.*/ig } };
validate({foo: "bar"}, constraints); // => undefined
validate({foo: "bar"}, constraints); // => { foo: ['Foo is invalid'] }
validate({foo: "bar"}, constraints); // => undefined
validate({foo: "bar"}, constraints); // => { foo: ['Foo is invalid'] }
...
constraints = { foo: { format: /.*/i } };
validate({foo: "bar"}, constraints); // => undefined
validate({foo: "bar"}, constraints); // => undefined

This is because regexp with the flag g are stateful. Do this and it will work:

var constraints = { foo: { format: { pattern: ".*", flags: "ig" }}};
validate({foo: "bar"}, constraints); // => undefined
validate({foo: "bar"}, constraints); // => undefined