validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

Suggestion: modular CSS regex checks

Open ayala-io opened this issue 6 years ago • 3 comments

While working on PR https://github.com/validatorjs/validator.js/pull/1159, I found myself constantly refining how I checked for the CSS <number> value and often having to replace the same pattern in multiple places of the larger regex pattern for the isHSL() check. I realized I should have assembled the regex pattern in a modular way but then I thought this can be done at a more global level to support other CSS string validations (like isRgbColor()).

As a possible "feature request" or development direction, maybe it would be good to make a collection of common regex patterns that we can use to build more complex patterns. This is particularly applicable to CSS checks for isHSL() and isRgbColor() and other CSS-based checks which use the same <number>, <percentage>, and other value formats defined in the specification (see MDN documentation).

Now I'm not an expert in JS, so I'm not sure what would be the best way to achieve a modular regex pattern. A quick Google search led me to this: https://stackoverflow.com/questions/185510/how-can-i-concatenate-regex-literals-in-javascript which I suppose could be one way to do it.

ayala-io avatar Feb 23 '20 15:02 ayala-io

There's something that @hamzahejja did here, I don't know if it goes in line with what you are talking about? And how can we use it in the cases you mentioned?

profnandaa avatar Feb 25 '20 05:02 profnandaa

Thank you for pointing that out. I'll keep that in mind moving forward. I think that would serve as part of what I was suggesting to finally "glue" together the final regex pattern composed of re-usable, modular patterns.

The re-usable patterns would be a set of globally available regex patterns (or I guess you can say sub-patterns) concerning a CSS <number> or <percentage> or <integer>, or any other CSS types/properties (there are some validators like isInt() as a candidate for code reuse but I'm not sure if it would validate a CSS-based <integer>). Then when coding validators such as isHSL(), isRgbColor(), and potentially other new CSS validators defined in validator.js, one can use the available sub-patterns and combine them or augment them via @hamzahejja's function.

This of course depends on the need for more CSS validators, which could be discussed here.

ayala-io avatar Feb 25 '20 09:02 ayala-io

I like the thinking 👍

./na

On Tue, Feb 25, 2020 at 12:26 PM Chris [email protected] wrote:

Thank you for pointing that out. I'll keep that in mind moving forward. I think that would serve as part of what I was suggesting to finally "glue" together the final regex pattern composed of re-usable, modular patterns.

The re-usable patterns would be a set of globally available regex patterns (or I guess you can say sub-patterns) concerning a CSS https://developer.mozilla.org/en-US/docs/Web/CSS/number or https://developer.mozilla.org/en-US/docs/Web/CSS/percentage or https://developer.mozilla.org/en-US/docs/Web/CSS/integer, or any other CSS types/properties (there are some validators like isInt() as a candidate for code reuse but I'm not sure if it validate a CSS-based ). Then when coding validators such as isHSL(), isRgbColor(), and potentially other new CSS validators defined in validator.js, one can use the available sub-patterns and combine them or augment them via @hamzahejja https://github.com/hamzahejja's function.

This of course depends on the need it for more CSS validators, which could be discussed here.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/validatorjs/validator.js/issues/1252?email_source=notifications&email_token=AAB7ZEKIXQPH4FKZ5INE2SDRETP2ZA5CNFSM4KZ3LXSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM3GQDY#issuecomment-590768143, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB7ZEIWRNKQ525C7XMDUH3RETP2ZANCNFSM4KZ3LXSA .

-- Sent from a tiny device while on the move.

profnandaa avatar Feb 25 '20 13:02 profnandaa