yiisoft / validator

Yii validator library
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
112 stars 39 forks source link

Missing StringValue rule #594

Closed arrilot closed 1 year ago

arrilot commented 1 year ago

This very basic rule is pretty much required when we use validator in a context like https://github.com/yiisoft/request-model Imagine we want to decode JSON body and check that particular JSON fields are strings

There are BoolValue, TrueValue and IntegerValue rules, but StringValue seems to be missing. There is also a workaround with an empty Length rule but using it for validation variable type is kinda strange.

arogachev commented 1 year ago

Should it allow \Stringable as well?

arrilot commented 1 year ago

I don't have a strong opinion on that matter but I guess no:

  1. e.g. Length rule uses is_string. Why would StringValue rule adhere Stringable then?
  2. Symfony validator uses simple is_string for Type('string') aswell https://github.com/symfony/validator/blob/6.2/Constraints/TypeValidator.php#L33
vjik commented 1 year ago

Should it allow \Stringable as well?

Maybe add option allowStringable with false as default?

arogachev commented 1 year ago

@vjik And in the Length too?

vjik commented 1 year ago

@vjik And in the Length too?

I would add to Length when there is a need for it.

arogachev commented 1 year ago

As for StringValue::$allowStringable - suggest to do it in a separate PR, maybe discuss more.