WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.4k stars 4.16k forks source link

"enum" without "type" in block.json throws notices and warnings in PHP #57942

Open Lovor01 opened 8 months ago

Lovor01 commented 8 months ago

Description

If enum is used for variable value and type is not used (docs for attributes say: The attribute definition will contain, at a minimum, either a type or an enum.), PHP throws a lot of Warnings and several Notices

Step-by-step reproduction instructions

In block.json, use enum for one property, without type, e.g.:

...
    "attributes": {
        "level": {
            "enum": [0, 1, 2, 3, 4, 5, 6],
            "default": 2
        }
    },
...

Compile block, enable debugging in wp-config.php: define( 'WP_DEBUG', true );

Open site URL with block you created, you will get similar warnings and notices:

Notice: Function rest_validate_value_from_schema was called incorrectly. The "type" schema keyword for level is required. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\functions.php on line 6031

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2149

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2164

Notice: Function rest_validate_value_from_schema was called incorrectly. The "type" schema keyword for level can only be one of the built-in types: array, object, string, number, integer, boolean, and null. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\functions.php on line 6031

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2173

Notice: Function rest_sanitize_value_from_schema was called incorrectly. The "type" schema keyword for level is required. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\functions.php on line 6031

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2751

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2761

Notice: Function rest_sanitize_value_from_schema was called incorrectly. The "type" schema keyword for level can only be one of the built-in types: array, object, string, number, integer, boolean, and null. Please see Debugging in WordPress for more information. (This message was added in version 5.5.0.) in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\functions.php on line 6031

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2770

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2787

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2814

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2818

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2822

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2826

Warning: Undefined array key "type" in C:\Wamp.NET\sites\dancelib\wordpress\wp-includes\rest-api.php on line 2862

When "type" is used along with "enum" in block.json, warnings and notices disappear. However, in that case, attribute might not be enum anymore.

Screenshots, screen recording, code snippet

No response

Environment info

WordPress 6.4.2 Custom theme FF developer 122 Windows 10 Desktop, local environment

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

talldan commented 8 months ago

When "type" is used along with "enum" in block.json, warnings and notices disappear. However, in that case, attribute might not be enum anymore.

Could you go into more detail on this? If you're referring to mixing types, it can be supported, for some core blocks templateLock is declared like this:

"templateLock": {
    "type": [ "string", "boolean" ],
    "enum": [ "all", "insert", "contentOnly", false ]
}

The docs could be updated to be more accurate.

gziolo commented 8 months ago

Potentially, the error could trigger here:

https://github.com/WordPress/wordpress-develop/blob/3600105163a45e62f07da3cb5607846f3e4bb4fd/src/wp-includes/class-wp-block-type.php#L497-L504

The line where the error gets printed:

https://github.com/WordPress/wordpress-develop/blob/3600105163a45e62f07da3cb5607846f3e4bb4fd/src/wp-includes/rest-api.php#L2144-L2147

In effect, one way to go about it would be to relax the requirement to include type when enum is defined.

Lovor01 commented 8 months ago

When "type" is used along with "enum" in block.json, warnings and notices disappear. However, in that case, attribute might not be enum anymore.

Could you go into more detail on this? If you're referring to mixing types, it can be supported, for some core blocks templateLock is declared like this:

"templateLock": {
  "type": [ "string", "boolean" ],
  "enum": [ "all", "insert", "contentOnly", false ]
}

The docs could be updated to be more accurate.

I am not referring to mixing types. I wrote that declaring attribute as "enum" (without using "type" - that's what I understood from docs) issued a bunch of php errors. I provided example. Enum is array of numbers.

talldan commented 8 months ago

@Lovor01 I understand all that, but I'm referring to the quoted text ... you wrote this bit as well:

When "type" is used along with "enum" in block.json, warnings and notices disappear. However, in that case, attribute might not be enum anymore.

Which I don't understand, would you be able to help clarify what you mean in those specific sentences?

Lovor01 commented 8 months ago

What I meant by that is that I am not familiar with internal functioning of attribute types. Enum type probably has some built-in validation (am I wrong?) by which it differs from normal type, otherwise I do not see the point of having enum. So if I use enum which is consisting of just numbers, like in my example, if I set type to number along with enum, the validation could be disabled in that case and I could set any number inside this attribute? I think the only point of having enum is validation, isn't it? And if you set enum, then type is irrelevant, like it says in docs, one or the other.

@Lovor01 I understand all that, but I'm referring to the quoted text ... you wrote this bit as well:

When "type" is used along with "enum" in block.json, warnings and notices disappear. However, in that case, attribute might not be enum anymore.

Which I don't understand, would you be able to help clarify what you mean in those specific sentences?

talldan commented 8 months ago

What I meant by that is that I am not familiar with internal functioning of attribute types. Enum type probably has some built-in validation (am I wrong?) by which it differs from normal type, otherwise I do not see the point of having enum. So if I use enum which is consisting of just numbers, like in my example, if I set type to number along with enum, the validation could be disabled in that case and I could set any number inside this attribute? I think the only point of having enum is validation, isn't it? And if you set enum, then type is irrelevant, like it says in docs, one or the other.

Thanks for explaining. Yes, I agree with that 👍