# Library of string validators and sanitizers

**URL:** <https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252>\
**Category:** Community\
**Tags:** package\
**Created:** [March 1, 2022, 6:26pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252 "2022-03-01T18:26:19Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![iskyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iskyd/32/44797_2.png) [@iskyd](https://discourse.julialang.org/u/iskyd)\
**Post date:** [March 1, 2022, 6:26pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/1 "2022-03-01T18:26:19Z")

</div>

Hi everyone,

I was inspired by [validator.js](https://github.com/validatorjs/validator.js) that I’ve used a lot in javascript. I didn’t find anything similar in Julia, so I started developing [validator.jl](https://github.com/iskyd/validator.jl). I think that this could be useful considering also the expansion of Julia.  
So I’m here to ask you what do you think and if this could be useful.  
I’ve just begun the development and it will be a long journey. If anyone is interested in contributing please do!

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [March 1, 2022, 6:55pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/2 "2022-03-01T18:55:19Z")

</div>

I think you could one one file with all the functions properly documented instead of file per function.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 1, 2022, 6:55pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/3 "2022-03-01T18:55:26Z")

</div>

Your code is overly restricted on the types it accepts

```julia
julia> a = "12345"
"12345"

julia> z = @view a[2:3]
"23"

julia> typeof(z)
SubString{String}

julia> function isEmpty(str::String, ignore_whitespace::Bool=false)::Bool 
           return (ignore_whitespace == true ? length(strip(str)) : length(str)) === 0
       end
isEmpty (generic function with 2 methods)

julia> isEmpty(z)
ERROR: MethodError: no method matching isEmpty(::SubString{String})
Closest candidates are:
  isEmpty(::String) at REPL[6]:1
  isEmpty(::String, ::Bool) at REPL[6]:1
Stacktrace:
 [1] top-level scope
   @ REPL[7]:1

julia> function isEmpty2(str, ignore_whitespace=false)
           return (ignore_whitespace == true ? length(strip(str)) : length(str)) === 0
       end
isEmpty2 (generic function with 2 methods)

julia> isEmpty2(z)
false

```

although perhaps that is too permissive

```julia
julia> isEmpty2(2)
false

```

```julia
julia> function isEmpty3(str::AbstractString, ignore_whitespace=false)
           return (ignore_whitespace == true ? length(strip(str)) : length(str)) === 0
       end
isEmpty3 (generic function with 2 methods)

julia> isEmpty3(2)
ERROR: MethodError: no method matching isEmpty3(::Int64)
Closest candidates are:
  isEmpty3(::AbstractString) at REPL[12]:1
  isEmpty3(::AbstractString, ::Any) at REPL[12]:1
Stacktrace:
 [1] top-level scope
   @ REPL[15]:1

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 1, 2022, 7:10pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/4 "2022-03-01T19:10:25Z")

</div>

[https://github.com/iskyd/validator.jl/blob/7fcbcd8a0838a756f3edd163f45159fab93ded35/src/isAscii.jl#L3](https://github.com/iskyd/validator.jl/blob/7fcbcd8a0838a756f3edd163f45159fab93ded35/src/isAscii.jl#L3)

don’t use non-const globals, also there’s already:

```julia

help?> isascii("123")
  isascii(c::Union{AbstractChar,AbstractString}) -> Bool

  Test whether a character belongs to the ASCII character set, or whether this is true for all elements of a string.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> isascii('a')
  true
  
  julia> isascii('α')
  false
  
  julia> isascii("abc")
  true
  
  julia> isascii("αβγ")
  false

```

* * *

You also need to make your pkg named `Validator.jl`.

[https://github.com/iskyd/validator.jl/blob/7fcbcd8a0838a756f3edd163f45159fab93ded35/src/isAfter.jl#L5](https://github.com/iskyd/validator.jl/blob/7fcbcd8a0838a756f3edd163f45159fab93ded35/src/isAfter.jl#L5)

is this also string validation?

* * *

In general, for performance, don’t use regex matching to check if a string contains something

---

<div class="post-metadata">

**Author:** ![iskyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iskyd/32/44797_2.png) [@iskyd](https://discourse.julialang.org/u/iskyd)\
**Post date:** [March 1, 2022, 7:16pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/5 "2022-03-01T19:16:43Z")

</div>

What’s the advantage of doing that?

---

<div class="post-metadata">

**Author:** ![iskyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iskyd/32/44797_2.png) [@iskyd](https://discourse.julialang.org/u/iskyd)\
**Post date:** [March 1, 2022, 7:17pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/6 "2022-03-01T19:17:10Z")

</div>

Thanks for the suggestion. Using AbstractString is for sure a better idea!

---

<div class="post-metadata">

**Author:** ![iskyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iskyd/32/44797_2.png) [@iskyd](https://discourse.julialang.org/u/iskyd)\
**Post date:** [March 1, 2022, 7:20pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/7 "2022-03-01T19:20:22Z")

</div>

Thanks for pointing out ascii function and for pointing out non-const global regex declared.  
This package is inspired from validator.js so I’m trying to port all functionality, isAfter included. Not sure why it was added in validator.js but I don’t think it is a bad idea to include it. What do you think?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 1, 2022, 7:22pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/8 "2022-03-01T19:22:30Z")

</div>

Julia is very much not Javascript and the community has a strong opposition against stuff close to “single function package” on the spectrum. If all the functions are just utilities that can be done in Base Julia, idiomatically, I personally don’t think it’s worth making a package.

But this is not to say I want to stop you, it’s totally fine, but at the same time, I don’t think people want Julia packages to be like Javascript in terms of organization and functionality fragmentation.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 1, 2022, 7:26pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/9 "2022-03-01T19:26:35Z")

</div>

[https://github.com/iskyd/validator.jl/blob/7fcbcd8a0838a756f3edd163f45159fab93ded35/src/isEmpty.jl#L3](https://github.com/iskyd/validator.jl/blob/7fcbcd8a0838a756f3edd163f45159fab93ded35/src/isEmpty.jl#L3)

there’s already `isempty()` function, and to ignore white space, you just need `isempty(strip(x))`

for country code, there’s:  
[https://github.com/JuliaFinance/Countries.jl](https://github.com/JuliaFinance/Countries.jl) but I guess they don’t have BIC check?

[https://github.com/iskyd/validator.jl/blob/7fcbcd8a0838a756f3edd163f45159fab93ded35/src/isBIC.jl#L10](https://github.com/iskyd/validator.jl/blob/7fcbcd8a0838a756f3edd163f45159fab93ded35/src/isBIC.jl#L10)

you can use `contains()` for this type of pattern:

```julia
julia> contains("apple", r"app.e")
true

```

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 2, 2022, 6:38am UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/10 "2022-03-02T06:38:54Z")

</div>

I didn’t want to be all negative and “we don’t do that here” but I don’t think there is much of a use case as a package.

It’s the sort of thing I would copy & paste code from if I needed a specific function and knew it was in there.

I have some packages like that myself, e.g. just examples of ccall to crib from

---

<div class="post-metadata">

**Author:** ![iskyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iskyd/32/44797_2.png) [@iskyd](https://discourse.julialang.org/u/iskyd)\
**Post date:** [March 2, 2022, 9:11am UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/11 "2022-03-02T09:11:34Z")

</div>

What’s the benefit of copy-paste if there is a library to do that? I agree there are some use-cases when you just need a simple function. Not all Julia fields (thinking of data science) needs something like this. But thinking for example about web dev things change. As a web dev I don’t think there is a single project where I didnd’t use any validators (not talking just about email validation).

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 2, 2022, 9:21am UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/12 "2022-03-02T09:21:08Z")

</div>

I don’t mean to say there is no value in it.

although tbh validator.js looks more like a way for people to boost their GitHub score

“how do I validate a German car registration number” is not something that comes up much in my life 🙂

---

<div class="post-metadata">

**Author:** ![iskyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iskyd/32/44797_2.png) [@iskyd](https://discourse.julialang.org/u/iskyd)\
**Post date:** [March 2, 2022, 9:58am UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/13 "2022-03-02T09:58:46Z")

</div>

It could be that it has no value, that’s why I’m trying to understand all your ideas.  
validator.js has more than [6 million weekly downloads](https://www.npmjs.com/package/validator), so it seems to be used in js world (that of course could be totally different from Julia, that’s why I’m asking).

It’s funny to read about license plate cause I’ve recently worked on a computer vision project where plate validation was needed.

BTW thanks for all the suggestions!

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 2, 2022, 10:03am UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/14 "2022-03-02T10:03:14Z")

</div>

[Colors has 26m downloads per week](https://www.npmjs.com/package/colors)

And [we know what happened there](https://heimdalsecurity.com/blog/the-npm-libraries-colors-and-faker-were-corrupted/)

but again, go for it ! if nothing else you will learn some stuff 🙂

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [March 2, 2022, 10:07am UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/15 "2022-03-02T10:07:30Z")

</div>

A few comnents to your package:

In general more code is always good. But I’m not sure the scope of the package makes a lot of sense. What exactly is it validating? Every possible string format? Social security numbers? License plates? That is way too broad a scope for a package.

Some minor comments

- Stylistically, modules are usually CamelCase, and functions are snake\_case in Julia (or lower case in one word)
- There is already an `isascii` and `isempty` in Julia Base for strings. Having another, different implementation is confusing.
- Be careful of string indexing like you do in `isBIC.jl` - it can throw errors if the input is not ASCII strings. Instead, use functions like `nextind`, or validate the string is ASCII before indexing. You can read more in the Julia documentation on strings.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 3, 2022, 4:23pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/16 "2022-03-03T16:23:00Z")

</div>

> [@iskyd](#):
>
> What’s the benefit of copy-paste if there is a library to do that?

Not Julia, but applicable beyond it’s domain: [https://www.youtube.com/watch?v=PAAkCSZUG1c&t=568s](https://www.youtube.com/watch?v=PAAkCSZUG1c&t=568s)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 3, 2022, 5:02pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/17 "2022-03-03T17:02:14Z")

</div>

> [@jling](#):
>
> there’s already `isempty()` function, and to ignore white space, you just need `isempty(strip(x))`

`all(isspace, x)` is much faster than `isempty(strip(x))`.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 3, 2022, 5:18pm UTC](https://discourse.julialang.org/t/library-of-string-validators-and-sanitizers/77252/18 "2022-03-03T17:18:33Z")

</div>

Yes indeed, but that’s besides the point.
