adonisjs / validator

Schema based validator for AdonisJS
MIT License
115 stars 40 forks source link

add lowerCase and upperCase mutate options to string #157

Closed orcuntuna closed 1 year ago

orcuntuna commented 1 year ago

Proposed changes

Added lowerCase and upperCase mutate options to string validation.

Why?

For example, the email address is not case-sensitive. Also, the e-mail address is a unique value. I know rules.email() has already lowerCase sanitization but that's only an example. Username can be given as an example instead of email address.

email: schema.string({ trim: true }, [ rules.email(), rules.unique({ table: 'users', column: 'email' }) ])

The unique validation above will be unnecessary for these 2 values:

example@example.com
Example@example.com

We can validate this just by add the lowercase property to string options:

email: schema.string({ trim: true, lowerCase: true }, [ rules.email(), rules.unique({ table: 'users', column: 'email' }) ])

Unique control is now really functional.

Types of changes

What types of changes does your code introduce?

Put an x in the boxes that apply

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

Further comments

I made a type check to don't use both lowerCase and upperCase at the same time but type controls are constantly duplicated in the project. (In validator.ts, schema/index.ts and etc.) I think this can be improved.

orcuntuna commented 1 year ago

@thetutlage

RomainLanz commented 1 year ago

The options on schema.string() are deprecated.

You are better off creating a custom rule to mutate the value.