userfrosting / UserFrosting

Modern PHP user login and management framework
https://www.userfrosting.com
Other
1.63k stars 368 forks source link

[Bug][Fortress] YAML validator with no attributes passes null parameter, causes argument type error at runtime #1250

Open StrykeSlammerII opened 4 months ago

StrykeSlammerII commented 4 months ago

Chat discussion at https://chat.userfrosting.com/channel/support?msg=4x7WkcL72ehSs9WXq

YAML validators with 0 attributes cause the error UserFrosting\Fortress\Adapter\JqueryValidationAdapter::transformValidator(): Argument #3 ($validator) must be of type array, null given when the page is loaded.

JqueryValidationArrayAdapter line 70 calls transformValidator(), line 107 in the same class.

example YAML:

species:
    validators:
        required:

is equivalent to JSON

[
  'species' => [
    'validators' => [
      'required' => null
    ]
  ]
]

Spotted in UF5.0, still exists in 5.1

Docs do not (yet) address this edge case. Is it more correct to allow validators with no attributes, or to require that all validators have at least one attribute? 'label' or 'message' attributes seem applicable for most use cases.

In 5.1, $transformed = match ($validatorName) { block does not pass $validator to every option, so there may be room to allow it to be null. Then perhaps escape it with $validator = $validator ?? array() at beginning of method? [whatever the correct syntax would be for that ?? operator]

lcharette commented 4 months ago

Then perhaps escape it with $validator = $validator ?? array() at beginning of method?

I like that idea. To avoid this issue in other, all Fortress methods currently tested agains't json schema should also be tested agains't YAML schemas.