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

isAlpha enhancement #1286

Merged
merged 11 commits into from
Nov 15, 2020
Merged

Conversation

mum-never-proud
Copy link
Contributor

@mum-never-proud mum-never-proud commented Apr 23, 2020

resolve #1282

Copy link
Member

@profnandaa profnandaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This so far looks good to me, thanks for getting this quick.

Just one thing you forgot, could you update the README too.

@mum-never-proud
Copy link
Contributor Author

@profnandaa thanks for the review, but I see one problem with the above implementation.

since we are allowing the user to pass strings, there are chances that the user might pass some invalid regex characters and the code might break.

we may also experience regex weird behavior, one of the examples is try adding
'h.e.-llo'.replace(/[ -\/]/g, '') paste it in the dev tools you will see that it will also remove dots from the word

so my proposal for strings is to go with the below code

ignore.split('').forEach(ignoredCharacter => str = str.split(ignoredCharacter))

with little tweaks, this code should work but at least it's safe and I hope it gives you the rough idea on what it's doing

} else if (typeof ignore === 'string') {
str = str.replace(new RegExp("[".concat(ignore, "]"), 'g'), '');

@mum-never-proud
Copy link
Contributor Author

ping @profnandaa

@profnandaa
Copy link
Member

@mum-never-proud -- I see, nice catch. What if we limited the ignore chars that we support? Pragmatically, I think we should support only the known in-between characters for words, i.e. space, ' and -; Have I left anything?

@mum-never-proud
Copy link
Contributor Author

@profnandaa but again we are narrowing down the use cases, some may raise an issue saying he needs to ignore a back tick or @

it's better to support all the characters in order to avoid reworking for additions

your thoughts?

@mum-never-proud
Copy link
Contributor Author

pong @profnandaa

@profnandaa
Copy link
Member

@mum-never-proud - makes sense. How about if just support a string with a list of characters to ignore; but not regex for now?

@mum-never-proud
Copy link
Contributor Author

@profnandaa but anyhow we will be using regex to check right?

@mum-never-proud
Copy link
Contributor Author

pong @profnandaa

@profnandaa
Copy link
Member

@mum-never-proud -- sure, still use regex to check, but don't take regex option from users.

@mum-never-proud
Copy link
Contributor Author

mum-never-proud commented May 29, 2020

@profnandaa i think we can allow both string and regex, i guess the key solution here will be to escape the regex for string which seemed to work

let me know your thoughts

ps: let me know if you are fin with the approach, so that i can do one final optimization of not modifying the params directly, a bad practice

@profnandaa
Copy link
Member

@mum-never-proud -- I'm okay with this, thanks.

@mum-never-proud
Copy link
Contributor Author

@profnandaa done,

Copy link
Member

@profnandaa profnandaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM You can update the README too?

@mum-never-proud
Copy link
Contributor Author

mum-never-proud commented May 29, 2020

cool, thanks!

mind providing text?

kinda stuck here

options is optional, valid option is ignore which can either be a String or RegExp to ignore certain characters

anything better than this?

@profnandaa

@profnandaa
Copy link
Member

I could re-jig it to be similar to the rest, something like:

options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.

@mum-never-proud
Copy link
Contributor Author

@profnandaa done!

Copy link
Member

@profnandaa profnandaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM 🎉

@profnandaa
Copy link
Member

In case I have missed anything, I will appreciate a second look from @ezkemboi @tux-tn or @rubiin

@profnandaa
Copy link
Member

ping @tux-tn -- can review please before @mum-never-proud fixes the merge conflicts...

assertString(_str);

let str = _str;
const { ignore } = options;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am with the opinion that ignore characters should be optional.
This actually fails if a user does not pass ignore values (It is not guaranteed that a user needs to have some values ignored).
Also, if I pass options as args: [{ ignore: /[\s/-]/g }], it will fail or I do pass an args: ['en-US', { }] it also fails.
I am open to discussion on way forward.

Cc. @profnandaa

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm technically i don't see a possibility of failing for any of the above args, may be i can add those

const { ignore } = options;
if (ignore) {

the above line says that ignore is optional which means, args: ['en-US', { }] should pass

as far as about this [{ ignore: /[\s/-]/g }] we are already asserting first argument to be string

let me know if it makes sense @profnandaa @ezkemboi

Copy link
Member

@ezkemboi ezkemboi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just check my comment @mum-never-proud and resolve conflicts.

@@ -1,8 +1,22 @@
import assertString from './util/assertString';
import { alpha } from './alpha';

export default function isAlpha(str, locale = 'en-US') {
assertString(str);
export default function isAlpha(_str, locale = 'en-US', options = {}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just curious why change str to _str and later do

let str = _str

I thought we could pass str as before and do:

assertString(str);

But, all in all, it looks good to me.

Copy link
Contributor Author

@mum-never-proud mum-never-proud Sep 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezkemboi ah it's not good practice to directly change the arguments, just following best practices, will resolve the conflict in sometime

@profnandaa
Copy link
Member

@mum-never-proud -- good to see you back, been a minute :) Ping me once you've resolved the conflicts.

@mum-never-proud
Copy link
Contributor Author

@mum-never-proud -- good to see you back, been a minute :) Ping me once you've resolved the conflicts.

heyy, man I totally forgot about this yes I will do today buddy

@profnandaa

@ezkemboi
Copy link
Member

ezkemboi commented Nov 5, 2020

@mum-never-proud, still waiting for you to resolve conflicts to get this one landing!!!

@mum-never-proud
Copy link
Contributor Author

mum-never-proud commented Nov 5, 2020 via email

@mum-never-proud
Copy link
Contributor Author

folks finally laptop is back today i will merge it @profnandaa @ezkemboi

@rubiin
Copy link
Member

rubiin commented Nov 9, 2020

I think we can release a new version after the pr @profnandaa

@codecov
Copy link

codecov bot commented Nov 9, 2020

Codecov Report

Merging #1286 (9edb2ca) into master (be8874d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1286   +/-   ##
=======================================
  Coverage   99.92%   99.92%           
=======================================
  Files          96       96           
  Lines        1277     1285    +8     
=======================================
+ Hits         1276     1284    +8     
  Misses          1        1           
Impacted Files Coverage Δ
src/lib/isAlpha.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update be8874d...d313ed6. Read the comment docs.

@mum-never-proud
Copy link
Contributor Author

i guess a lot has changed since I created this PR, let me know if there is something I am missing? @profnandaa @ezkemboi

@rubiin
Copy link
Member

rubiin commented Nov 10, 2020

i guess a lot has changed since I created this PR, let me know if there is something I am missing? @profnandaa @ezkemboi

I reviewed and the pr looks okay from my side

@ezkemboi
Copy link
Member

@profnandaa this is good to land.

@profnandaa profnandaa merged commit b569296 into validatorjs:master Nov 15, 2020
@patrikgerdin
Copy link

Hey, I'm getting an error in typescript (but it is working and validates)

body('field').isAlpha('en-US', {ignore: ' '})

(property) IsAlphaOptions.ignore?: string[] | undefined
Type 'string' is not assignable to type 'string[] | undefined'.ts(2322)

A string[] does not work

body('field').isAlpha('en-US', {ignore: [' ']})

Error: ignore should be instance of a String or RegExp

Am I missing something?

@profnandaa
Copy link
Member

profnandaa commented Dec 14, 2020 via email

@patrikgerdin
Copy link

patrikgerdin commented Dec 14, 2020

Was looking at the docs here https://github.com/validatorjs/validator.js#validators, hard to see what is the key(s) there.

In options.d.ts

export interface IsAlphaOptions { ignore?: string[]; }

Should be just a string ?

Nice that it works now otherwise.

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

Successfully merging this pull request may close these issues.

Is there a way to include isAlpha with spaces yet?
5 participants