Add a built-in `uuid` validator (`yii\validators\UuidValidator`)
Validator::$builtInValidators ships 22 validators, but none for UUID. UUID and ULID identifiers and uuid columns are common now (framework/db/pgsql/Schema.php already maps the Postgres uuid type), yet validating user or API input means falling back to match with a hand-written pattern. Laravel (uuid, ulid) and Symfony both have a dedicated rule.
Proposal
Add framework/validators/UuidValidator.php and register it as 'uuid' in Validator::$builtInValidators:
['id', 'uuid'], // any well-formed UUID
['id', 'uuid', 'version' => 4], // require v4Options:
version(int|int[]|null) require a specific UUID version (1/3/4/5/7),nullaccepts any well-formed UUID.allowNil(bool, defaulttrue) accept the nil UUID00000000-0000-0000-0000-000000000000.- standard
message,skipOnEmpty, etc. inherited from the baseValidator.
The check is a hex-format match (8-4-4-4-12), so it can mirror client-side in framework/assets/yii.validation.js through clientValidateAttribute() / getClientOptions(), the same way EmailValidator and IpValidator already do.
Questions
- UUID only, or add a sibling
UlidValidator('ulid') in the same PR? - Client-side validation now, or as a follow-up?
- Preference on the
versionoption shape (singleintvs a list)?
Happy to send a PR with tests (and the JS counterpart if wanted).
Source: yiisoft/yii2