Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format validator with custom regex not consistent #423

Closed
oskarrough opened this issue Dec 21, 2016 · 1 comment
Closed

Format validator with custom regex not consistent #423

oskarrough opened this issue Dec 21, 2016 · 1 comment

Comments

@oskarrough
Copy link

As the twiddle shows it seems like using a custom 'format' validator with this youtube regex only validates every second time (even if valid).

I don't believe the youtube-regex is at fault but I honestly don't know.

Environment

  • Ember Version: 2.10.0
  • Ember CLI Version: 2.10.0
  • Ember CP Validations Version: 1.3.4

Steps to Reproduce

https://ember-twiddle.com/f310e9371cab8145d85180137bd8992d?openFiles=models.video.js%2C

@oskarrough
Copy link
Author

So with the help of @offirgolan it seems the real issue here is a bug in the regex engine.

The basics are a /regex/ with the g modifier doesn't reset correctly, so multiple .test() calls alternate between true and false if everyone should be true, every other calls successfully resets it.

See here http://stackoverflow.com/a/3891672/273406.

In my case, using a custom validator helped.

import BaseValidator from 'ember-cp-validations/validators/base';
import youtubeRegex from 'npm:youtube-regex';

const YoutubeUrl = BaseValidator.extend({
	validate(value) {
		const valid = youtubeRegex().test(value);
		if (!valid) {
			return 'Enter a full YouTube URL';
		}
		return true;
	}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant