cermati/satpam

[Nice To Have] Support string in rules values

Closed this issue · 4 comments

dwncy commented

Hi, I've been thinking that it will be good if support string instead of array when the rules just once

for example:

const rules = {
  name: 'required',
  officeEmail: 'email',
  phone: ['required', 'numeric']
};

@darwin301194 I don't think it's idiomatic, since it will change the rule type from Array to String. Feel free to re-open the issue 🐳

I don't know if this is relevant to this issue. In the past I've used laravel several times though only for very small apps. They provide the validation as a string and then separate between validations using |. It somehow feels more natural maybe since it's my first experience learn about validations.

Here's the reference in case you're interested in,
https://laravel.com/docs/5.3/validation

@d3nd0h sorry for late response. Yes I used laravel a few years ago, after digging a bit, I found out that they were exploding the validation rules by | and I believe they still do this. I 100% agree that it feels more natural to read and type

const rules = {
  name: 'required',
  email: 'required|email'
};

but there will be 3 ways to define validation rules (there are 2 now Array<String> or Array<String|Object>), by providing this feature it will let devs to define rules like this (CMIIW)

const rules = {
  name: 'required',
  email: ['required', 'email'], 
  address: 'required|minLength:20' // Errr a bit ugly
};

https://github.com/laravel/framework/blob/5.3/src/Illuminate/Validation/Validator.php#L246

Yeah adding too many style would make the code more ugly I guess :) So let's keep it this way for now.