Add validation constraints to user.role.*

Created on 4 May 2024, 14 days ago
Updated 16 May 2024, 2 days ago

Problem/Motivation

User role entity is not yet fully validatable:

./vendor/bin/drush config:inspect --filter-keys=user.role.anonymous --detail --list-constraints --fields=key,validatability,constraints    
➜  🤖 Analyzing…

 ------------------------------------------------ ------------- --------------------------------------------------------------------------------------------- 
  Key                                              Validatable   Validation constraints                                                                       
 ------------------------------------------------ ------------- --------------------------------------------------------------------------------------------- 
  user.role.anonymous                              66%           ValidKeys: '<infer>'                                                                         
   user.role.anonymous:                            Validatable   ValidKeys: '<infer>'                                                                         
   user.role.anonymous:_core                       Validatable   ValidKeys:                                                                                   
                                                                   - default_config_hash                                                                      
   user.role.anonymous:_core.default_config_hash   Validatable   NotNull: {  }                                                                                
                                                                 Regex: '/^[a-zA-Z0-9\-_]+$/'                                                                 
                                                                 Length: 43                                                                                   
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:dependencies                Validatable   ValidKeys: '<infer>'                                                                         
   user.role.anonymous:dependencies.config         NOT           ❌ @todo Add validation constraints to ancestor type: config_dependencies                    
   user.role.anonymous:dependencies.config.0       Validatable   NotBlank: {  }                                                                               
                                                                 ConfigExists: {  }                                                                           
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:dependencies.module         NOT           ❌ @todo Add validation constraints to ancestor type: config_dependencies                    
   user.role.anonymous:dependencies.module.0       Validatable   NotBlank: {  }                                                                               
                                                                 ExtensionName: {  }                                                                          
                                                                 ExtensionExists: module                                                                      
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:dependencies.module.1       Validatable   NotBlank: {  }                                                                               
                                                                 ExtensionName: {  }                                                                          
                                                                 ExtensionExists: module                                                                      
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:dependencies.module.2       Validatable   NotBlank: {  }                                                                               
                                                                 ExtensionName: {  }                                                                          
                                                                 ExtensionExists: module                                                                      
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:dependencies.module.3       Validatable   NotBlank: {  }                                                                               
                                                                 ExtensionName: {  }                                                                          
                                                                 ExtensionExists: module                                                                      
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:dependencies.module.4       Validatable   NotBlank: {  }                                                                               
                                                                 ExtensionName: {  }                                                                          
                                                                 ExtensionExists: module                                                                      
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:dependencies.module.5       Validatable   NotBlank: {  }                                                                               
                                                                 ExtensionName: {  }                                                                          
                                                                 ExtensionExists: module                                                                      
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:dependencies.module.6       Validatable   NotBlank: {  }                                                                               
                                                                 ExtensionName: {  }                                                                          
                                                                 ExtensionExists: module                                                                      
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:id                          Validatable   Regex:                                                                                       
                                                                   pattern: '/^[a-z0-9_]+$/'                                                                  
                                                                   message: 'The %value machine name is not valid.'                                           
                                                                 Length:                                                                                      
                                                                   max: 166                                                                                   
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:is_admin                    Validatable   ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:label                       Validatable   Regex:                                                                                       
                                                                   pattern: '/([^\PC])/u'                                                                     
                                                                   match: false                                                                               
                                                                   message: 'Labels are not allowed to span multiple lines or contain control characters.'    
                                                                 NotBlank: {  }                                                                               
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:langcode                    Validatable   NotNull: {  }                                                                                
                                                                 Choice:                                                                                      
                                                                   callback: 'Drupal\Core\TypedData\Plugin\DataType\LanguageReference::getAllValidLangcodes'  
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:permissions                 NOT           ⚠️  @todo Add validation constraints to config entity type: user.role.*                      
   user.role.anonymous:permissions.0               NOT           ⚠️  @todo Add validation constraints to config entity type: user.role.*                      
   user.role.anonymous:permissions.1               NOT           ⚠️  @todo Add validation constraints to config entity type: user.role.*                      
   user.role.anonymous:permissions.2               NOT           ⚠️  @todo Add validation constraints to config entity type: user.role.*                      
   user.role.anonymous:permissions.3               NOT           ⚠️  @todo Add validation constraints to config entity type: user.role.*                      
   user.role.anonymous:permissions.4               NOT           ⚠️  @todo Add validation constraints to config entity type: user.role.*                      
   user.role.anonymous:permissions.5               NOT           ⚠️  @todo Add validation constraints to config entity type: user.role.*                      
   user.role.anonymous:permissions.6               NOT           ⚠️  @todo Add validation constraints to config entity type: user.role.*                      
   user.role.anonymous:status                      Validatable   ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:uuid                        Validatable   Uuid: {  }                                                                                   
                                                                 ↣ PrimitiveType: {  }                                                                        
   user.role.anonymous:weight                      Validatable   Range:                                                                                       
                                                                   min: -2147483648                                                                           
                                                                   max: 2147483647                                                                            
                                                                 FullyValidatable: null                                                                       
                                                                 ↣ PrimitiveType: {  }                                                                        
 ------------------------------------------------ ------------- --------------------------------------------------------------------------------------------- 

Steps to reproduce

  1. Get a local git clone of Drupal core 11.x.
  2. composer require drupal/config_inspector — or manually install https://www.drupal.org/project/config_inspector/releases/2.1.5 or newer (which supports Drupal 11!)
  3. composer require drush/drush
  4. vendor/bin/drush config:inspect --filter-keys=user.role.anonymous --detail --list-constraints

Proposed resolution

Add validation constraints to missing properties.

This requires looking at the existing code and admin UI (if any) to understand which values could be considered valid. Eventually this needs to be reviewed by the relevant subsystem maintainer.

For examples, search *.schema.yml files for the string constraints: 😊

Reach out to @borisson_ or @wimleers in the #distributions-and-recipes.

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

More validation 🚀

Release notes snippet

None.

📌 Task
Status

Needs work

Version

11.0 🔥

Component
User module 

Last updated 2 days ago

Created by

🇮🇳India narendraR Jaipur, India

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @narendraR
  • Merge request !7910User role validation → (Open) created by narendraR
  • Pipeline finished with Failed
    14 days ago
    Total: 737s
    #164071
  • Pipeline finished with Failed
    13 days ago
    Total: 593s
    #164612
  • Pipeline finished with Failed
    13 days ago
    Total: 488s
    #164650
  • Pipeline finished with Failed
    12 days ago
    Total: 504s
    #165321
  • Pipeline finished with Failed
    12 days ago
    Total: 600s
    #165437
  • Pipeline finished with Success
    12 days ago
    Total: 498s
    #165470
  • Status changed to Needs review 12 days ago
  • 🇮🇳India narendraR Jaipur, India
  • Status changed to RTBC 11 days ago
  • 🇺🇸United States smustgrave

    Using configuration inspector on a standard install I see

    So believe this one is good.

  • Status changed to Needs work 10 days ago
  • 🇧🇪Belgium Wim Leers Ghent 🇧🇪🇪🇺

    I don't see any justification for making is_admin and weight optional values? I kinda get weight being optional … but not is_admin.

    Plus, if they're optional, then that should be reflected on the class properties too, and that's not the case:

      /**
       * The weight of this role in administrative listings.
       *
       * @var int
       */
      protected $weight;
    

    +

      /**
       * An indicator whether the role has all permissions.
       *
       * @var bool
       */
      protected $is_admin;
    
  • First commit to issue fork.
  • Pipeline finished with Failed
    9 days ago
    Total: 575s
    #168986
  • First commit to issue fork.
  • Pipeline finished with Failed
    9 days ago
    Total: 643s
    #169163
  • Pipeline finished with Success
    8 days ago
    #169201
  • Pipeline finished with Success
    8 days ago
    Total: 575s
    #169225
  • Status changed to Needs review 8 days ago
  • 🇺🇸United States mtift Minnesota, USA
  • Status changed to Needs work 2 days ago
  • 🇧🇪Belgium Wim Leers Ghent 🇧🇪🇪🇺
Production build 0.67.2 2024